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

Fix: reduce bundle size by remove runtime module #6850

Merged
merged 5 commits into from
Apr 15, 2024
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
6 changes: 6 additions & 0 deletions .changeset/cuddly-jars-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ice/runtime': patch
'@ice/app': patch
---

fix: reduce bundle size by remove runtime module
2 changes: 1 addition & 1 deletion packages/ice/src/createService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,10 @@ async function createService({ rootDir, command, commandArgs }: CreateServiceOpt
);

const appConfig: AppConfig = (await getAppConfig()).default;

updateRuntimeEnv(appConfig, {
disableRouter,
// The optimization for runtime size should only be enabled in production mode.
routesConfig: command !== 'build' || routesInfo.routesExports.length > 0,
dataLoader: command !== 'build' || loaderExports,
});

Expand Down
17 changes: 11 additions & 6 deletions packages/ice/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ export async function generateRoutesInfo(
}
});

const routesExports = [];
const configExport = generateRouteConfig(routes, 'pageConfig', (str, imports) => {
routesExports.push(...imports);
return `${str}
export default {
${imports.map(([routeId, importKey]) => `'${routeId}': ${importKey},`).join('\n ')}
};`;
});

return {
routesCount,
routeManifest,
Expand All @@ -46,12 +55,8 @@ export default {
${imports.map(([routeId, importKey]) => `'${routeId}': ${importKey},`).join('\n ')}
};` : '';
}),
routesConfig: generateRouteConfig(routes, 'pageConfig', (str, imports) => {
return `${str}
export default {
${imports.map(([routeId, importKey]) => `'${routeId}': ${importKey},`).join('\n ')}
};`;
}),
routesConfig: configExport,
routesExports,
};
}

Expand Down
9 changes: 8 additions & 1 deletion packages/ice/src/utils/runtimeEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface Envs {
}
interface EnvOptions {
disableRouter: boolean;
routesConfig: boolean;
dataLoader: boolean;
}

Expand Down Expand Up @@ -54,13 +55,15 @@ export async function setEnv(
// Set to false for compatibility with the old version.
process.env.ICE_CORE_REMOVE_DATA_LOADER = 'false';

process.env.ICE_CORE_REMOVE_ROUTES_CONFIG = 'false';

// set ssr and ssg env to false, for remove dead code in CSR.
process.env.ICE_CORE_SSG = 'false';
process.env.ICE_CORE_SSR = 'false';
}

export const updateRuntimeEnv = (appConfig: AppConfig, options: EnvOptions) => {
const { disableRouter, dataLoader } = options;
const { disableRouter, dataLoader, routesConfig } = options;
if (!appConfig?.app?.errorBoundary) {
process.env.ICE_CORE_ERROR_BOUNDARY = 'false';
}
Expand All @@ -70,6 +73,9 @@ export const updateRuntimeEnv = (appConfig: AppConfig, options: EnvOptions) => {
if (!dataLoader) {
process.env.ICE_CORE_REMOVE_DATA_LOADER = 'true';
}
if (!routesConfig) {
process.env.ICE_CORE_REMOVE_ROUTES_CONFIG = 'true';
}
};

export function getCoreEnvKeys() {
Expand All @@ -79,6 +85,7 @@ export function getCoreEnvKeys() {
'ICE_CORE_ERROR_BOUNDARY',
'ICE_CORE_INITIAL_DATA',
'ICE_CORE_DEV_PORT',
'ICE_CORE_REMOVE_ROUTES_CONFIG',
'ICE_CORE_REMOVE_DATA_LOADER',
];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime/src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export function createRouteLoader(options: RouteLoaderOptions): LoaderFunction {
const loaderData = {
pageConfig: pageConfig ? pageConfig({}) : {},
};
if (import.meta.renderer === 'client') {
if (import.meta.renderer === 'client' && process.env.ICE_CORE_REMOVE_ROUTES_CONFIG !== 'true') {
await updateRoutesConfig(loaderData);
}
return loaderData;
Expand Down Expand Up @@ -242,7 +242,7 @@ export function createRouteLoader(options: RouteLoaderOptions): LoaderFunction {
pageConfig: routeConfig,
};
// Update routes config when render mode is CSR.
if (import.meta.renderer === 'client') {
if (import.meta.renderer === 'client' && process.env.ICE_CORE_REMOVE_ROUTES_CONFIG !== 'true') {
await updateRoutesConfig(loaderData);
}
return loaderData;
Expand Down
Loading