Skip to content

Commit

Permalink
Fix: reduce bundle size by remove runtime module (#6850)
Browse files Browse the repository at this point in the history
* fix: optimize runtime size

* chore: changeset

* chore: changeset

* Delete .changeset/late-dryers-invent.md
  • Loading branch information
ClarkXia authored Apr 15, 2024
1 parent 46c1d24 commit d5c378b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
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 @@ -353,10 +353,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

0 comments on commit d5c378b

Please sign in to comment.