Skip to content

Commit c48f338

Browse files
authored
fix(core): swizzle --eject js should not copy theme .d.ts files (#7776)
1 parent c3d2e0d commit c48f338

File tree

10 files changed

+48
-58
lines changed

10 files changed

+48
-58
lines changed

packages/docusaurus/src/commands/swizzle/__tests__/__fixtures__/theme/JsComponent/index.css

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/docusaurus/src/commands/swizzle/__tests__/__fixtures__/theme/JsComponent/index.d.ts

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/docusaurus/src/commands/swizzle/__tests__/__fixtures__/theme/JsComponent/index.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/docusaurus/src/commands/swizzle/__tests__/__snapshots__/index.test.ts.snap

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,18 @@ exports[`swizzle eject ComponentInFolder JS: ComponentInFolder/Sibling.css 1`] =
77
"
88
`;
99

10-
exports[`swizzle eject ComponentInFolder JS: ComponentInFolder/Sibling.tsx 1`] = `
11-
"import React from 'react';
12-
13-
export default function Sibling() {
14-
return <div>Sibling</div>;
15-
}
16-
"
17-
`;
18-
1910
exports[`swizzle eject ComponentInFolder JS: ComponentInFolder/index.css 1`] = `
2011
".testClass {
2112
background: black;
2213
}
2314
"
2415
`;
2516

26-
exports[`swizzle eject ComponentInFolder JS: ComponentInFolder/index.tsx 1`] = `
27-
"import React from 'react';
28-
29-
export default function ComponentInFolder() {
30-
return <div>ComponentInFolder</div>;
31-
}
32-
"
33-
`;
34-
3517
exports[`swizzle eject ComponentInFolder JS: theme dir tree 1`] = `
3618
"theme
3719
└── ComponentInFolder
3820
├── Sibling.css
39-
├── Sibling.tsx
40-
├── index.css
41-
└── index.tsx"
21+
└── index.css"
4222
`;
4323

4424
exports[`swizzle eject ComponentInFolder TS: ComponentInFolder/Sibling.css 1`] = `
@@ -89,15 +69,6 @@ exports[`swizzle eject ComponentInFolder/ComponentInSubFolder JS: ComponentInFol
8969
"
9070
`;
9171

92-
exports[`swizzle eject ComponentInFolder/ComponentInSubFolder JS: ComponentInFolder/ComponentInSubFolder/index.tsx 1`] = `
93-
"import React from 'react';
94-
95-
export default function ComponentInSubFolder() {
96-
return <div>ComponentInSubFolder</div>;
97-
}
98-
"
99-
`;
100-
10172
exports[`swizzle eject ComponentInFolder/ComponentInSubFolder JS: ComponentInFolder/ComponentInSubFolder/styles.css 1`] = `
10273
".testClass {
10374
background: black;
@@ -117,7 +88,6 @@ exports[`swizzle eject ComponentInFolder/ComponentInSubFolder JS: theme dir tree
11788
└── ComponentInFolder
11889
└── ComponentInSubFolder
11990
├── index.css
120-
├── index.tsx
12191
├── styles.css
12292
└── styles.module.css"
12393
`;
@@ -169,20 +139,10 @@ exports[`swizzle eject ComponentInFolder/Sibling JS: ComponentInFolder/Sibling.c
169139
"
170140
`;
171141

172-
exports[`swizzle eject ComponentInFolder/Sibling JS: ComponentInFolder/Sibling.tsx 1`] = `
173-
"import React from 'react';
174-
175-
export default function Sibling() {
176-
return <div>Sibling</div>;
177-
}
178-
"
179-
`;
180-
181142
exports[`swizzle eject ComponentInFolder/Sibling JS: theme dir tree 1`] = `
182143
"theme
183144
└── ComponentInFolder
184-
├── Sibling.css
185-
└── Sibling.tsx"
145+
└── Sibling.css"
186146
`;
187147

188148
exports[`swizzle eject ComponentInFolder/Sibling TS: ComponentInFolder/Sibling.css 1`] = `
@@ -215,19 +175,9 @@ exports[`swizzle eject FirstLevelComponent JS: FirstLevelComponent.css 1`] = `
215175
"
216176
`;
217177

218-
exports[`swizzle eject FirstLevelComponent JS: FirstLevelComponent.tsx 1`] = `
219-
"import React from 'react';
220-
221-
export default function FirstLevelComponent() {
222-
return <div>First level component</div>;
223-
}
224-
"
225-
`;
226-
227178
exports[`swizzle eject FirstLevelComponent JS: theme dir tree 1`] = `
228179
"theme
229-
├── FirstLevelComponent.css
230-
└── FirstLevelComponent.tsx"
180+
└── FirstLevelComponent.css"
231181
`;
232182

233183
exports[`swizzle eject FirstLevelComponent TS: FirstLevelComponent.css 1`] = `

packages/docusaurus/src/commands/swizzle/__tests__/actions.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ function stableCreatedFiles(
2424
}
2525

2626
describe('eject', () => {
27-
async function testEject(action: SwizzleAction, componentName: string) {
27+
async function testEject(
28+
action: SwizzleAction,
29+
componentName: string,
30+
{typescript}: {typescript: boolean} = {typescript: true},
31+
) {
2832
const siteDir = await createTempSiteDir();
2933
const siteThemePath = path.join(siteDir, 'src/theme');
3034
const result = await eject({
3135
siteDir,
3236
componentName,
3337
themePath: ThemePath,
38+
typescript,
3439
});
3540
return {
3641
siteDir,
@@ -53,6 +58,22 @@ describe('eject', () => {
5358
`);
5459
});
5560

61+
it(`eject ${Components.JsComponent} JS`, async () => {
62+
const result = await testEject('eject', Components.JsComponent, {
63+
typescript: false,
64+
});
65+
expect(result.createdFiles).toEqual([
66+
'JsComponent/index.css',
67+
'JsComponent/index.js',
68+
]);
69+
expect(result.tree).toMatchInlineSnapshot(`
70+
"theme
71+
└── JsComponent
72+
├── index.css
73+
└── index.js"
74+
`);
75+
});
76+
5677
it(`eject ${Components.ComponentInSubFolder}`, async () => {
5778
const result = await testEject('eject', Components.ComponentInSubFolder);
5879
expect(result.createdFiles).toEqual([

packages/docusaurus/src/commands/swizzle/__tests__/components.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('readComponentNames', () => {
1919
Components.ComponentInSubFolder,
2020
Components.Sibling,
2121
Components.FirstLevelComponent,
22+
Components.JsComponent,
2223
Components.NoIndexComp1,
2324
Components.NoIndexComp2,
2425
Components.NoIndexSubComp,
@@ -69,6 +70,7 @@ describe('getThemeComponents', () => {
6970
Components.ComponentInSubFolder,
7071
Components.Sibling,
7172
Components.FirstLevelComponent,
73+
Components.JsComponent,
7274
Components.NoIndex,
7375
Components.NoIndexComp1,
7476
Components.NoIndexComp2,

packages/docusaurus/src/commands/swizzle/__tests__/testUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const Components = {
1616
Sibling: 'ComponentInFolder/Sibling',
1717
ComponentInFolder: 'ComponentInFolder',
1818
FirstLevelComponent: 'FirstLevelComponent',
19+
JsComponent: 'JsComponent',
1920
NoIndex: 'NoIndex',
2021
NoIndexComp1: 'NoIndex/NoIndexComp1',
2122
NoIndexComp2: 'NoIndex/NoIndexComp2',

packages/docusaurus/src/commands/swizzle/actions.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type ActionParams = {
3333
siteDir: string;
3434
themePath: string;
3535
componentName: string;
36+
typescript: boolean;
3637
};
3738

3839
export type ActionResult = {
@@ -49,6 +50,7 @@ export async function eject({
4950
siteDir,
5051
themePath,
5152
componentName,
53+
typescript,
5254
}: ActionParams): Promise<ActionResult> {
5355
const fromPath = path.join(themePath, componentName);
5456
const isDirectory = await isDir(fromPath);
@@ -60,7 +62,12 @@ export async function eject({
6062
const globPatternPosix = posixPath(globPattern);
6163

6264
const filesToCopy = await Globby(globPatternPosix, {
63-
ignore: ['**/*.{story,stories,test,tests}.{js,jsx,ts,tsx}'],
65+
ignore: _.compact([
66+
'**/*.{story,stories,test,tests}.{js,jsx,ts,tsx}',
67+
// When ejecting JS components, we want to avoid emitting TS files
68+
// In particular the .d.ts files that theme build output contains
69+
typescript ? null : '**/*.{d.ts,ts,tsx}',
70+
]),
6471
});
6572

6673
if (filesToCopy.length === 0) {
@@ -103,7 +110,6 @@ export async function wrap({
103110
typescript,
104111
importType = 'original',
105112
}: ActionParams & {
106-
typescript: boolean;
107113
importType?: 'original' | 'init';
108114
}): Promise<ActionResult> {
109115
const isDirectory = await isDir(path.join(themePath, themeComponentName));

packages/docusaurus/src/commands/swizzle/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Created wrapper of name=${componentName} from name=${themeName} in path=${result
145145
siteDir,
146146
themePath,
147147
componentName,
148+
typescript,
148149
});
149150
logger.success`
150151
Ejected name=${componentName} from name=${themeName} to path=${result.createdFiles}

website/_dogfooding/testSwizzleThemeClassic.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const classicThemePathBase = path.join(
3030
'../../packages/docusaurus-theme-classic',
3131
);
3232

33-
const themePath = swizzleConfig
33+
const themePath = typescript
3434
? path.join(classicThemePathBase, 'src/theme')
3535
: path.join(classicThemePathBase, 'lib/theme');
3636

@@ -98,13 +98,13 @@ for (const componentName of componentNames) {
9898
siteDir: toPath,
9999
themePath,
100100
componentName,
101+
typescript,
101102
};
102103
switch (action) {
103104
case 'wrap':
104105
return wrap({
105106
...baseParams,
106107
importType: 'init', // For these tests, "theme-original" imports are causing an expected infinite loop
107-
typescript,
108108
});
109109
case 'eject':
110110
return eject(baseParams);

0 commit comments

Comments
 (0)