Skip to content

Commit dc0da34

Browse files
torresgol10slorber
andauthored
refactor(create-docusaurus): replace lodash with native implementation (#11653)
Co-authored-by: sebastien <[email protected]>
1 parent 73e0382 commit dc0da34

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

packages/create-docusaurus/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"commander": "^5.1.0",
2828
"execa": "^5.1.1",
2929
"fs-extra": "^11.1.1",
30-
"lodash": "^4.17.21",
3130
"prompts": "^2.4.2",
3231
"semver": "^7.5.4",
3332
"supports-color": "^9.4.0",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {siteNameToPackageName} from '../utils';
9+
10+
describe('siteNameToPackageName', () => {
11+
it('converts simple cases', () => {
12+
const testCases: [string, string][] = [
13+
['Foo Bar', 'foo-bar'],
14+
['fooBar', 'foo-bar'],
15+
['__FOO_BAR__', 'foo-bar'],
16+
['XMLHttpRequest', 'xml-http-request'],
17+
['sitemapXML', 'sitemap-xml'],
18+
['XMLHttp', 'xml-http'],
19+
['xml-http', 'xml-http'],
20+
];
21+
22+
testCases.forEach(([input, expected]) => {
23+
expect(siteNameToPackageName(input)).toEqual(expected);
24+
});
25+
});
26+
27+
it('converts ñ', () => {
28+
expect(siteNameToPackageName('mañanaFoo')).toEqual('ma-ana-foo');
29+
});
30+
31+
it('converts __', () => {
32+
expect(siteNameToPackageName('foo__bar')).toEqual('foo-bar');
33+
});
34+
35+
it('skips 🔥', () => {
36+
expect(siteNameToPackageName('🔥')).toEqual('🔥');
37+
});
38+
39+
it('skips !!!', () => {
40+
expect(siteNameToPackageName('!!!')).toEqual('!!!');
41+
});
42+
});

packages/create-docusaurus/src/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import fs from 'fs-extra';
99
import {fileURLToPath} from 'url';
1010
import path from 'path';
11-
import _ from 'lodash';
1211
import {logger} from '@docusaurus/logger';
1312
import execa from 'execa';
1413
import prompts, {type Choice} from 'prompts';
@@ -17,6 +16,7 @@ import supportsColor from 'supports-color';
1716
// TODO remove dependency on large @docusaurus/utils
1817
// would be better to have a new smaller @docusaurus/utils-cli package
1918
import {askPreferredLanguage} from '@docusaurus/utils';
19+
import {siteNameToPackageName} from './utils.js';
2020

2121
type LanguagesOptions = {
2222
javascript?: boolean;
@@ -164,7 +164,15 @@ async function readTemplates(): Promise<Template[]> {
164164
);
165165

166166
// Classic should be first in list!
167-
return _.sortBy(templates, (t) => t.name !== recommendedTemplate);
167+
return templates.sort((a, b) => {
168+
if (a.name === recommendedTemplate) {
169+
return -1;
170+
}
171+
if (b.name === recommendedTemplate) {
172+
return 1;
173+
}
174+
return 0;
175+
});
168176
}
169177

170178
async function copyTemplate(
@@ -562,7 +570,7 @@ export default async function init(
562570
// Update package.json info.
563571
try {
564572
await updatePkg(path.join(dest, 'package.json'), {
565-
name: _.kebabCase(siteName),
573+
name: siteNameToPackageName(siteName),
566574
version: '0.0.0',
567575
private: true,
568576
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
/**
9+
* We use a simple kebab-case-like conversion
10+
* It's not perfect, but good enough
11+
* We don't want to depend on lodash in this package
12+
* See https://github.com/facebook/docusaurus/pull/11653
13+
* @param siteName
14+
*/
15+
export function siteNameToPackageName(siteName: string): string {
16+
const match = siteName.match(
17+
/[A-Z]{2,}(?=[A-Z][a-z]+\d*|\b|_)|[A-Z]?[a-z]+\d*|[A-Z]|\d+/g,
18+
);
19+
if (match) {
20+
return match.map((x) => x.toLowerCase()).join('-');
21+
}
22+
return siteName;
23+
}

0 commit comments

Comments
 (0)