Skip to content

Commit

Permalink
Reduce amount of chunks for the runtime (#1301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Nov 7, 2022
1 parent 248cb1d commit 7b165e1
Show file tree
Hide file tree
Showing 2 changed files with 580 additions and 213 deletions.
25 changes: 8 additions & 17 deletions packages/toolpad-app/src/runtime/loadModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,21 @@ import { transform, TransformResult } from 'sucrase';
import { codeFrameColumns } from '@babel/code-frame';
import { findImports, isAbsoluteUrl } from '../utils/strings';
import { errorFrom } from '../utils/errors';
import muiMaterialExports from './muiExports';

async function resolveValues(input: Map<string, Promise<unknown>>): Promise<Map<string, unknown>> {
const resolved = await Promise.all(input.values());
return new Map(Array.from(input.keys(), (key, i) => [key, resolved[i]]));
}

async function createRequire(urlImports: string[]) {
const modules = await resolveValues(
new Map<string, Promise<any>>([
// These import('...') are passed static strings so that webpack knows how to resolve them at build time.
// Don't change
['react', import('react')],
['dayjs', import('dayjs')],
['react-dom', import('react-dom')],
['@mui/toolpad-core', import(`@mui/toolpad-core`)],

['@mui/icons-material', import('@mui/icons-material')],

...muiMaterialExports,

...urlImports.map((url) => [url, import(/* webpackIgnore: true */ url)] as const),
]),
);
const [{ default: muiMaterialExports }, urlModules] = await Promise.all([
import('./muiExports'),
resolveValues(
new Map(urlImports.map((url) => [url, import(/* webpackIgnore: true */ url)] as const)),
),
]);

const modules: Map<string, any> = new Map([...muiMaterialExports, ...urlModules]);

const require = (moduleId: string): unknown => {
let esModule = modules.get(moduleId);
Expand Down
Loading

0 comments on commit 7b165e1

Please sign in to comment.