Skip to content

Commit

Permalink
fix: add pluginEmitRouteFile in uni-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Dec 19, 2023
1 parent cc3dc5e commit 787f95b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
1 change: 1 addition & 0 deletions packages/builder/uni-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@babel/core": "^7.23.2",
"@babel/preset-react": "^7.22.15",
"@babel/types": "^7.23.0",
"@modern-js/utils": "workspace:*",
"@modern-js/server": "workspace:*",
"@modern-js/prod-server": "workspace:*",
"@pmmmwh/react-refresh-webpack-plugin": "0.5.10",
Expand Down
2 changes: 2 additions & 0 deletions packages/builder/uni-builder/src/shared/parseCommonConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { pluginCheckSyntax } from '@rsbuild/plugin-check-syntax';
import { pluginCssMinimizer } from '@rsbuild/plugin-css-minimizer';
import { pluginPostcssLegacy } from './plugins/postcssLegacy';
import { pluginDevtool } from './plugins/devtools';
import { pluginEmitRouteFile } from './plugins/emitRouteFile';

const GLOBAL_CSS_REGEX = /\.global\.\w+$/;

Expand Down Expand Up @@ -237,6 +238,7 @@ export async function parseCommonConfig<B = 'rspack' | 'webpack'>(
pluginDevtool({
disableSourceMap: uniBuilderConfig.output?.disableSourceMap,
}),
pluginEmitRouteFile(),
];

const checkSyntaxOptions = uniBuilderConfig.security?.checkSyntax;
Expand Down
34 changes: 34 additions & 0 deletions packages/builder/uni-builder/src/shared/plugins/emitRouteFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { join } from 'path';
import { type RsbuildPlugin, isFileExists } from '@rsbuild/shared';

/**
* generate a basic route.json for modern.js server
*/
export const pluginEmitRouteFile = (): RsbuildPlugin => ({
name: 'uni-builder:emit-route-file',

setup(api) {
api.onBeforeStartDevServer(async () => {
const { fs, ROUTE_SPEC_FILE } = await import('@modern-js/utils');
const routeFilePath = join(api.context.distPath, ROUTE_SPEC_FILE);
const htmlPaths = api.getHTMLPaths();

const routesInfo = Object.entries(htmlPaths).map(
([entryName, filename], index) => ({
urlPath: index === 0 ? '/' : `/${entryName}`,
entryName,
entryPath: filename,
isSPA: true,
}),
);

// if the framework has already generate a route.json, do nothing
if (!(await isFileExists(routeFilePath)) && routesInfo.length) {
await fs.outputFile(
routeFilePath,
JSON.stringify({ routes: routesInfo }, null, 2),
);
}
});
},
});
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 0 additions & 29 deletions tests/e2e/builder/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type {
BuilderConfig as UniBuilderConfig,
CreateUniBuilderOptions,
StartDevServerOptions,
RsbuildPlugin,
} from '@modern-js/uni-builder';

type CreateBuilderOptions = Omit<
Expand Down Expand Up @@ -142,33 +141,6 @@ const updateConfigForTest = <BuilderType>(
}
};

// generate a basic route.json for test (modern.js server required)
const pluginEmitRoute = (): RsbuildPlugin => ({
name: 'plugin-emit-route',

setup(api) {
api.onBeforeStartDevServer(async () => {
const { fs, ROUTE_SPEC_FILE } = await import('@modern-js/utils');
const routeFilePath = join(api.context.distPath, ROUTE_SPEC_FILE);
const htmlPaths = api.getHTMLPaths();

const routesInfo = Object.entries(htmlPaths).map(
([entryName, filename], index) => ({
urlPath: index === 0 ? '/' : `/${entryName}`,
entryName,
entryPath: filename,
isSPA: true,
}),
);

await fs.outputFile(
routeFilePath,
JSON.stringify({ routes: routesInfo }, null, 2),
);
});
},
});

export async function dev<BuilderType = 'webpack'>({
serverOptions,
builderConfig = {},
Expand All @@ -185,7 +157,6 @@ export async function dev<BuilderType = 'webpack'>({

const builder = await createUniBuilder(options, builderConfig);

builder.addPlugins([pluginEmitRoute()]);
return builder.startDevServer({
serverOptions,
});
Expand Down

0 comments on commit 787f95b

Please sign in to comment.