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
24 changes: 23 additions & 1 deletion docs/src/modules/sandbox/CodeSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,29 @@ function openSandbox({ files, codeVariant, initialFile }: any) {

function createReactApp(demoData: DemoData) {
const ext = getFileExtension(demoData.codeVariant);
const { title, githubLocation: description } = demoData;
const { title, relativeModules = [], githubLocation: description } = demoData;

if (relativeModules) {
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
relativeModules.forEach(({ module, raw: content }) => {
// remove exports from relative module
content = content.replace(/export( )*(default)*( )*\w+;|export default|export/gm, '');
// replace import statement with relative module content
// the module might be imported with or without extension, so we need to cover all cases
// E.g.: /import .* from '(.\/top100Films.js|.\/top100Films)';/
const extensions = ['', '.js', '.jsx', '.ts', '.tsx', '.css', '.json'];
const patterns = extensions
.map((ex) => {
if (module.endsWith(ex)) {
return module.replace(ex, '');
}
return '';
})
.filter(Boolean)
.join('|');
const importPattern = new RegExp(`import .* from '(${patterns})';`);
demoData.raw = demoData.raw.replace(importPattern, content);
});
}

const files: Record<string, object> = {
oliviertassinari marked this conversation as resolved.
Show resolved Hide resolved
'public/index.html': {
Expand Down
24 changes: 23 additions & 1 deletion docs/src/modules/sandbox/StackBlitz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,29 @@ import { DemoData } from 'docs/src/modules/sandbox/types';

function createReactApp(demoData: DemoData) {
const ext = getFileExtension(demoData.codeVariant);
const { title, githubLocation: description } = demoData;
const { title, relativeModules = [], githubLocation: description } = demoData;

if (relativeModules) {
relativeModules.forEach(({ module, raw: content }) => {
// remove exports from relative module
content = content.replace(/export( )*(default)*( )*\w+;|export default|export/gm, '');
// replace import statement with relative module content
// the module might be imported with or without extension, so we need to cover all cases
// E.g.: /import .* from '(.\/top100Films.js|.\/top100Films)';/
const extensions = ['', '.js', '.jsx', '.ts', '.tsx', '.css', '.json'];
const patterns = extensions
.map((ex) => {
if (module.endsWith(ex)) {
return module.replace(ex, '');
}
return '';
})
.filter(Boolean)
.join('|');
const importPattern = new RegExp(`import .* from '(${patterns})';`);
demoData.raw = demoData.raw.replace(importPattern, content);
});
}

const files: Record<string, string> = {
'index.html': CRA.getHtml(demoData),
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[];
}
Loading