Compare commits

...

7 Commits

Author SHA1 Message Date
ClarkXia 00a9fa1989
Merge e5edb831a4 into 1a08895dc5 2025-07-04 13:10:21 +08:00
ClarkXia e5edb831a4 chore: changelog 2024-07-31 10:32:49 +08:00
ClarkXia d775cdbc29 Merge branch 'release/next' into fix/optimize-route-config 2024-07-30 19:08:55 +08:00
ClarkXia 4b465456d6 fix: ts type 2024-07-12 10:02:53 +08:00
ClarkXia 8aa692864f feat: support remove asset mainfest in entry file 2024-07-11 20:25:36 +08:00
ClarkXia a8788f95a9 fix: optimize code 2024-07-10 18:03:36 +08:00
ClarkXia a305920aca fix: optimize the route code generated by framework 2024-07-10 17:37:15 +08:00
9 changed files with 88 additions and 43 deletions

View File

@ -0,0 +1,5 @@
---
'@ice/app': patch
---
fix: optimize the route code generate by framework

View File

@ -0,0 +1,7 @@
---
'@ice/webpack-config': patch
'@ice/rspack-config': patch
'@ice/shared-config': patch
---
fix: support remove the manifest info from bundle

View File

@ -28,7 +28,9 @@ const getDefaultTaskConfig = ({ rootDir, command }): Config => {
'universal-env': envReplacement, 'universal-env': envReplacement,
'@uni/env': envReplacement, '@uni/env': envReplacement,
}, },
assetsManifest: true, assetsManifest: {
inject: true,
},
fastRefresh: command === 'start', fastRefresh: command === 'start',
logging: process.env.WEBPACK_LOGGING || defaultLogging, logging: process.env.WEBPACK_LOGGING || defaultLogging,
minify: command === 'build', minify: command === 'build',

View File

@ -89,39 +89,10 @@ export function getRoutesDefinition(options: GetDefinationOptions) {
routeImports.push(`import * as ${routeSpecifier} from '${formatPath(componentPath)}';`); routeImports.push(`import * as ${routeSpecifier} from '${formatPath(componentPath)}';`);
loadStatement = routeSpecifier; loadStatement = routeSpecifier;
} }
const component = `Component: () => WrapRouteComponent({ // Keep the string in a single line for the code style.
routeId: '${id}', const lazyStatment = `lazyLoadComponent(${lazy ? 'componentModule' : loadStatement}, { routeId: '${id}', ${layout ? `layout: ${layout}, ` : ''}renderMode, requestContext })`;
isLayout: ${layout}, const routeProperties: string[] = [`...getRouteProps({ path: '${formatPath(routePath || '')}', componentName: '${componentName}', ${index ? `index: ${index}, ` : ''}id: '${id}',${layout ? `layout: ${layout}, ` : ''}exports: ${JSON.stringify(exports)} }),`];
routeExports: ${lazy ? 'componentModule' : loadStatement}, routeProperties.push(`async lazy() {${lazy ? `const componentModule = await ${loadStatement}` : ''}; return ${lazyStatment};},`);
})`;
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,');
}
if (children) { if (children) {
const res = getRoutesDefinition({ const res = getRoutesDefinition({
manifest: children, manifest: children,

View File

@ -1,5 +1,53 @@
import * as React from 'react';
import { createRouteLoader, WrapRouteComponent, RouteErrorComponent } from '@ice/runtime'; import { createRouteLoader, WrapRouteComponent, RouteErrorComponent } from '@ice/runtime';
import type { CreateRoutes } 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' : ''; -%> <%- routeImports.length ? routeImports.join('\n') + '\n\n' : ''; -%>
const createRoutes: CreateRoutes = ({ const createRoutes: CreateRoutes = ({
requestContext, requestContext,

View File

@ -149,7 +149,7 @@ const getConfig: GetConfig = async (options) => {
module: true, module: true,
}, minimizerOptions); }, minimizerOptions);
const builtinFeatures: BuiltinFeatures = { const builtinFeatures: BuiltinFeatures = {
assetsManifest, assetsManifest: Boolean(assetsManifest),
}; };
let splitChunksStrategy = null; let splitChunksStrategy = null;
// Use builtin splitChunks strategy by default. // Use builtin splitChunks strategy by default.

View File

@ -179,7 +179,7 @@ export interface Config {
performance?: Performance; performance?: Performance;
assetsManifest?: boolean; assetsManifest?: boolean | { inject: boolean };
concatenateModules?: boolean; concatenateModules?: boolean;

View File

@ -298,6 +298,7 @@ export function getWebpackConfig(options: GetWebpackConfigOptions): Configuratio
assetsManifest && new AssetsManifestPlugin({ assetsManifest && new AssetsManifestPlugin({
fileName: 'assets-manifest.json', fileName: 'assets-manifest.json',
outputDir: path.join(rootDir, runtimeTmpDir), outputDir: path.join(rootDir, runtimeTmpDir),
inject: typeof assetsManifest === 'boolean' ? false : assetsManifest?.inject,
}), }),
analyzer && new BundleAnalyzerPlugin(), analyzer && new BundleAnalyzerPlugin(),
tsCheckerOptions && new ForkTsCheckerPlugin(tsCheckerOptions), tsCheckerOptions && new ForkTsCheckerPlugin(tsCheckerOptions),

View File

@ -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 { export default class AssetsManifestPlugin {
private fileName: string; private fileName: string;
private outputDir: string; private outputDir: string;
private inject: boolean;
public constructor(options) { public constructor(options: PluginOptions) {
this.fileName = options.fileName || 'assets-manifest.json'; this.fileName = options.fileName || 'assets-manifest.json';
this.outputDir = options.outputDir || './'; this.outputDir = options.outputDir || './';
this.inject = options.inject;
} }
public createAssets(compilation: Compilation) { public createAssets(compilation: Compilation) {
@ -77,12 +86,14 @@ export default class AssetsManifestPlugin {
// Emit asset manifest for server compile. // Emit asset manifest for server compile.
compilation.emitAsset(this.fileName, new webpack.sources.RawSource(output)); compilation.emitAsset(this.fileName, new webpack.sources.RawSource(output));
// Inject assets manifest to entry file. // Inject assets manifest to entry file.
entryFiles.forEach((entryFile) => { if (this.inject) {
compilation.assets[entryFile] = new webpack.sources.ConcatSource( entryFiles.forEach((entryFile) => {
new webpack.sources.RawSource(String(`window.__ICE_ASSETS_MANIFEST__=${output};\n`)), compilation.assets[entryFile] = new webpack.sources.ConcatSource(
compilation.assets[entryFile], new webpack.sources.RawSource(String(`window.__ICE_ASSETS_MANIFEST__=${output};\n`)),
); compilation.assets[entryFile],
}); );
});
}
} }
public apply(compiler: Compiler) { public apply(compiler: Compiler) {