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

Reduce amount of chunks for the runtime #1301

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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