Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs-infra] Fix broken sandboxes with relative module imports #42767

Merged
merged 10 commits into from
Aug 9, 2024
19 changes: 18 additions & 1 deletion docs/src/modules/sandbox/CodeSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import addHiddenInput from 'docs/src/modules/utils/addHiddenInput';
import SandboxDependencies from 'docs/src/modules/sandbox/Dependencies';
import * as CRA from 'docs/src/modules/sandbox/CreateReactApp';
import getFileExtension from 'docs/src/modules/sandbox/FileExtension';
import flattenRelativeImports from 'docs/src/modules/sandbox/FlattenRelativeImports';
import { DemoData, CodeVariant, CodeStyling } from 'docs/src/modules/sandbox/types';

function compress(object: any) {
Expand Down Expand Up @@ -45,8 +46,24 @@ function createReactApp(demoData: DemoData) {
content: CRA.getRootIndex(demoData),
},
[`src/Demo.${ext}`]: {
content: demoData.raw,
content: flattenRelativeImports(
demoData.raw,
demoData.relativeModules?.map((file) => file.module),
),
},
// Spread the relative modules
...(demoData.relativeModules &&
// Transform the relative modules array into an object
demoData.relativeModules.reduce(
(acc, curr) => ({
...acc,
// Remove the path and keep the filename
[`src/${curr.module.replace(/^.*[\\/]/g, '')}`]: {
content: curr.raw,
},
}),
{},
)),
...(demoData.codeVariant === 'TS' && {
'tsconfig.json': {
content: CRA.getTsconfig(),
Expand Down
10 changes: 10 additions & 0 deletions docs/src/modules/sandbox/FlattenRelativeImports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function flattenRelativeImports(rawCode: string, modulePaths: string[] = []) {
let newCode = rawCode;
modulePaths.forEach((path: string) => {
const pathWithoutExtension = path.replace(/\.[a-z]*$/g, '');
// Move the relative import to the current directory
const newPath = `./${pathWithoutExtension.replace(/^.*[\\/]/g, '')}`;
newCode = newCode.replace(pathWithoutExtension, newPath);
});
return newCode;
}
17 changes: 16 additions & 1 deletion docs/src/modules/sandbox/StackBlitz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CODE_VARIANTS } from 'docs/src/modules/constants';
import SandboxDependencies from 'docs/src/modules/sandbox/Dependencies';
import * as CRA from 'docs/src/modules/sandbox/CreateReactApp';
import getFileExtension from 'docs/src/modules/sandbox/FileExtension';
import flattenRelativeImports from 'docs/src/modules/sandbox/FlattenRelativeImports';
import { DemoData } from 'docs/src/modules/sandbox/types';

function createReactApp(demoData: DemoData) {
Expand All @@ -12,7 +13,21 @@ function createReactApp(demoData: DemoData) {
const files: Record<string, string> = {
'index.html': CRA.getHtml(demoData),
[`index.${ext}`]: CRA.getRootIndex(demoData),
[`Demo.${ext}`]: demoData.raw,
[`Demo.${ext}`]: flattenRelativeImports(
demoData.raw,
demoData.relativeModules?.map((file) => file.module),
),
// Spread the relative modules
...(demoData.relativeModules &&
// Transform the relative modules array into an object
demoData.relativeModules.reduce(
(acc, curr) => ({
...acc,
// Remove the path and keep the filename
[`${curr.module.replace(/^.*[\\/]/g, '')}`]: curr.raw,
}),
{},
)),
...(demoData.codeVariant === 'TS' && {
'tsconfig.json': CRA.getTsconfig(),
}),
Expand Down
6 changes: 6 additions & 0 deletions docs/src/modules/sandbox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import type { MuiProductId } from 'docs/src/modules/utils/getProductInfoFromUrl'

export type CodeStyling = 'Tailwind' | 'MUI System';
export type CodeVariant = 'TS' | 'JS';

type RelativeModule = {
module: string;
raw: string;
};
export interface DemoData {
title: string;
language: string;
Expand All @@ -10,4 +15,5 @@ export interface DemoData {
githubLocation: string;
productId?: Exclude<MuiProductId, 'null'>;
codeStyling: CodeStyling;
relativeModules?: RelativeModule[];
}
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.