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: optimize the route code generated by framework #6943

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/fresh-roses-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/app': patch
---

fix: optimize the route code generate by framework
7 changes: 7 additions & 0 deletions .changeset/gentle-turtles-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@ice/webpack-config': patch
'@ice/rspack-config': patch
'@ice/shared-config': patch
---

fix: support remove the manifest info from bundle
4 changes: 3 additions & 1 deletion packages/ice/src/plugins/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const getDefaultTaskConfig = ({ rootDir, command }): Config => {
'universal-env': envReplacement,
'@uni/env': envReplacement,
},
assetsManifest: true,
assetsManifest: {
inject: true,
},
fastRefresh: command === 'start',
logging: process.env.WEBPACK_LOGGING || defaultLogging,
minify: command === 'build',
Expand Down
37 changes: 4 additions & 33 deletions packages/ice/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,39 +89,10 @@ export function getRoutesDefinition(options: GetDefinationOptions) {
routeImports.push(`import * as ${routeSpecifier} from '${formatPath(componentPath)}';`);
loadStatement = routeSpecifier;
}
const component = `Component: () => WrapRouteComponent({
routeId: '${id}',
isLayout: ${layout},
routeExports: ${lazy ? 'componentModule' : loadStatement},
})`;
const loader = `loader: createRouteLoader({
routeId: '${id}',
requestContext,
renderMode,
module: ${lazy ? 'componentModule' : loadStatement},
})`;
const routeProperties: string[] = [
`path: '${formatPath(routePath || '')}',`,
`async lazy() {
${lazy ? `const componentModule = await ${loadStatement}` : ''};
return {
${lazy ? '...componentModule' : `...${loadStatement}`},
${component},
${loader},
};
},`,
// Empty errorElement to avoid default ui provided by react-router.
'errorElement: <RouteErrorComponent />,',
`componentName: '${componentName}',`,
`index: ${index},`,
`id: '${id}',`,
'exact: true,',
`exports: ${JSON.stringify(exports)},`,
].filter(Boolean);

if (layout) {
routeProperties.push('layout: true,');
}
// Keep the string in a single line for the code style.
const lazyStatment = `lazyLoadComponent(${lazy ? 'componentModule' : loadStatement}, { routeId: '${id}', ${layout ? `layout: ${layout}, ` : ''}renderMode, requestContext })`;
const routeProperties: string[] = [`...getRouteProps({ path: '${formatPath(routePath || '')}', componentName: '${componentName}', ${index ? `index: ${index}, ` : ''}id: '${id}',${layout ? `layout: ${layout}, ` : ''}exports: ${JSON.stringify(exports)} }),`];
routeProperties.push(`async lazy() {${lazy ? `const componentModule = await ${loadStatement}` : ''}; return ${lazyStatment};},`);
if (children) {
const res = getRoutesDefinition({
manifest: children,
Expand Down
48 changes: 48 additions & 0 deletions packages/ice/templates/core/routes.tsx.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
import * as React from 'react';
import { createRouteLoader, WrapRouteComponent, RouteErrorComponent } from '@ice/runtime';
import type { CreateRoutes } from '@ice/runtime';
import type { ComponentModule, RenderMode, RequestContext } from '@ice/runtime/types';

interface RouteOptions {
routeId: string;
layout?: boolean;
renderMode: RenderMode;
requestContext?: RequestContext;
}
function lazyLoadComponent(componentModule: ComponentModule, options: RouteOptions) {
const { routeId, layout, renderMode, requestContext } = options;
return {
...componentModule,
Component: () => WrapRouteComponent({
routeId: routeId,
isLayout: Boolean(layout),
routeExports: componentModule,
}),
loader: createRouteLoader({
routeId: options.routeId,
requestContext,
renderMode,
module: componentModule,
}),
};
}
interface GetRoutePropsOptions {
path: string;
componentName: string;
index?: any;
id: string;
exports: string[];
layout?: boolean;
}
function getRouteProps(options: GetRoutePropsOptions) {
const { path, componentName, index, id, exports, layout } = options;
return {
path,
componentName,
errorElement: <RouteErrorComponent />,
index,
id,
exports,
layout: Boolean(layout),
exact: true,
};
}

<%- routeImports.length ? routeImports.join('\n') + '\n\n' : ''; -%>
const createRoutes: CreateRoutes = ({
requestContext,
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const getConfig: GetConfig = async (options) => {
module: true,
}, minimizerOptions);
const builtinFeatures: BuiltinFeatures = {
assetsManifest,
assetsManifest: Boolean(assetsManifest),
};
let splitChunksStrategy = null;
// Use builtin splitChunks strategy by default.
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export interface Config {

performance?: Performance;

assetsManifest?: boolean;
assetsManifest?: boolean | { inject: boolean };

concatenateModules?: boolean;

Expand Down
1 change: 1 addition & 0 deletions packages/webpack-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ export function getWebpackConfig(options: GetWebpackConfigOptions): Configuratio
assetsManifest && new AssetsManifestPlugin({
fileName: 'assets-manifest.json',
outputDir: path.join(rootDir, runtimeTmpDir),
inject: typeof assetsManifest === 'boolean' ? false : assetsManifest?.inject,
}),
analyzer && new BundleAnalyzerPlugin(),
tsCheckerOptions && new ForkTsCheckerPlugin(tsCheckerOptions),
Expand Down
25 changes: 18 additions & 7 deletions packages/webpack-config/src/webpackPlugins/AssetsManifestPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ function filterAssets(assets: Assets): string[] {
);
}

interface PluginOptions {
fileName?: string;
outputDir?: string;
// Inject assets manifest to entry file.
inject?: boolean;
}

export default class AssetsManifestPlugin {
private fileName: string;
private outputDir: string;
private inject: boolean;

public constructor(options) {
public constructor(options: PluginOptions) {
this.fileName = options.fileName || 'assets-manifest.json';
this.outputDir = options.outputDir || './';
this.inject = options.inject;
}

public createAssets(compilation: Compilation) {
Expand Down Expand Up @@ -77,12 +86,14 @@ export default class AssetsManifestPlugin {
// Emit asset manifest for server compile.
compilation.emitAsset(this.fileName, new webpack.sources.RawSource(output));
// Inject assets manifest to entry file.
entryFiles.forEach((entryFile) => {
compilation.assets[entryFile] = new webpack.sources.ConcatSource(
new webpack.sources.RawSource(String(`window.__ICE_ASSETS_MANIFEST__=${output};\n`)),
compilation.assets[entryFile],
);
});
if (this.inject) {
entryFiles.forEach((entryFile) => {
compilation.assets[entryFile] = new webpack.sources.ConcatSource(
new webpack.sources.RawSource(String(`window.__ICE_ASSETS_MANIFEST__=${output};\n`)),
compilation.assets[entryFile],
);
});
}
}

public apply(compiler: Compiler) {
Expand Down
Loading