Skip to content

Commit c18e53d

Browse files
bharatkashyapsiriwatknpoliviertassinarialexfauquette
authored
[docs-infra] Fix broken sandboxes with relative module imports (#42767)
Signed-off-by: Bharat Kashyap <[email protected]> Co-authored-by: Siriwat K <[email protected]> Co-authored-by: Olivier Tassinari <[email protected]> Co-authored-by: alex <[email protected]>
1 parent ad40084 commit c18e53d

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

docs/src/modules/sandbox/CodeSandbox.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import addHiddenInput from 'docs/src/modules/utils/addHiddenInput';
44
import SandboxDependencies from 'docs/src/modules/sandbox/Dependencies';
55
import * as CRA from 'docs/src/modules/sandbox/CreateReactApp';
66
import getFileExtension from 'docs/src/modules/sandbox/FileExtension';
7+
import flattenRelativeImports from 'docs/src/modules/sandbox/FlattenRelativeImports';
78
import { DemoData, CodeVariant, CodeStyling } from 'docs/src/modules/sandbox/types';
89

910
function compress(object: any) {
@@ -45,8 +46,24 @@ function createReactApp(demoData: DemoData) {
4546
content: CRA.getRootIndex(demoData),
4647
},
4748
[`src/Demo.${ext}`]: {
48-
content: demoData.raw,
49+
content: flattenRelativeImports(
50+
demoData.raw,
51+
demoData.relativeModules?.map((file) => file.module),
52+
),
4953
},
54+
// Spread the relative modules
55+
...(demoData.relativeModules &&
56+
// Transform the relative modules array into an object
57+
demoData.relativeModules.reduce(
58+
(acc, curr) => ({
59+
...acc,
60+
// Remove the path and keep the filename
61+
[`src/${curr.module.replace(/^.*[\\/]/g, '')}`]: {
62+
content: curr.raw,
63+
},
64+
}),
65+
{},
66+
)),
5067
...(demoData.codeVariant === 'TS' && {
5168
'tsconfig.json': {
5269
content: CRA.getTsconfig(),
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function flattenRelativeImports(rawCode: string, modulePaths: string[] = []) {
2+
let newCode = rawCode;
3+
modulePaths.forEach((path: string) => {
4+
const pathWithoutExtension = path.replace(/\.[a-z]*$/g, '');
5+
// Move the relative import to the current directory
6+
const newPath = `./${pathWithoutExtension.replace(/^.*[\\/]/g, '')}`;
7+
newCode = newCode.replace(pathWithoutExtension, newPath);
8+
});
9+
return newCode;
10+
}

docs/src/modules/sandbox/StackBlitz.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CODE_VARIANTS } from 'docs/src/modules/constants';
33
import SandboxDependencies from 'docs/src/modules/sandbox/Dependencies';
44
import * as CRA from 'docs/src/modules/sandbox/CreateReactApp';
55
import getFileExtension from 'docs/src/modules/sandbox/FileExtension';
6+
import flattenRelativeImports from 'docs/src/modules/sandbox/FlattenRelativeImports';
67
import { DemoData } from 'docs/src/modules/sandbox/types';
78

89
function createReactApp(demoData: DemoData) {
@@ -12,7 +13,21 @@ function createReactApp(demoData: DemoData) {
1213
const files: Record<string, string> = {
1314
'index.html': CRA.getHtml(demoData),
1415
[`index.${ext}`]: CRA.getRootIndex(demoData),
15-
[`Demo.${ext}`]: demoData.raw,
16+
[`Demo.${ext}`]: flattenRelativeImports(
17+
demoData.raw,
18+
demoData.relativeModules?.map((file) => file.module),
19+
),
20+
// Spread the relative modules
21+
...(demoData.relativeModules &&
22+
// Transform the relative modules array into an object
23+
demoData.relativeModules.reduce(
24+
(acc, curr) => ({
25+
...acc,
26+
// Remove the path and keep the filename
27+
[`${curr.module.replace(/^.*[\\/]/g, '')}`]: curr.raw,
28+
}),
29+
{},
30+
)),
1631
...(demoData.codeVariant === 'TS' && {
1732
'tsconfig.json': CRA.getTsconfig(),
1833
}),

docs/src/modules/sandbox/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import type { MuiProductId } from 'docs/src/modules/utils/getProductInfoFromUrl'
22

33
export type CodeStyling = 'Tailwind' | 'MUI System';
44
export type CodeVariant = 'TS' | 'JS';
5+
6+
type RelativeModule = {
7+
module: string;
8+
raw: string;
9+
};
510
export interface DemoData {
611
title: string;
712
language: string;
@@ -10,4 +15,5 @@ export interface DemoData {
1015
githubLocation: string;
1116
productId?: Exclude<MuiProductId, 'null'>;
1217
codeStyling: CodeStyling;
18+
relativeModules?: RelativeModule[];
1319
}

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)