From bdc1d3db89f0e3356c48c1712e3e72f85019b76c Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 6 Nov 2025 13:38:47 +0800 Subject: [PATCH] refactor(builder): clean up unused code --- .changeset/grumpy-jars-reply.md | 5 ++ packages/cli/builder/src/createBuilder.ts | 1 - .../src/rsc/plugins/rsbuild-rsc-plugin.ts | 2 - .../rsc/plugins/rspack-rsc-client-plugin.ts | 60 ++++++++-------- .../rsc/plugins/rspack-rsc-server-plugin.ts | 27 ++++--- .../cli/builder/src/rsc/rsc-ssr-loader.ts | 1 - packages/cli/builder/src/shared/utils.ts | 72 +------------------ packages/cli/builder/src/types.ts | 2 - packages/cli/builder/tsconfig.json | 3 +- 9 files changed, 48 insertions(+), 125 deletions(-) create mode 100644 .changeset/grumpy-jars-reply.md diff --git a/.changeset/grumpy-jars-reply.md b/.changeset/grumpy-jars-reply.md new file mode 100644 index 000000000000..7fcc3b1f2182 --- /dev/null +++ b/.changeset/grumpy-jars-reply.md @@ -0,0 +1,5 @@ +--- +'@modern-js/builder': patch +--- + +refactor(builder): clean up unused code diff --git a/packages/cli/builder/src/createBuilder.ts b/packages/cli/builder/src/createBuilder.ts index 14a146e6efca..d7253518aeb6 100644 --- a/packages/cli/builder/src/createBuilder.ts +++ b/packages/cli/builder/src/createBuilder.ts @@ -113,7 +113,6 @@ export async function parseConfig( rsbuildPlugins.push( rsbuildRscPlugin({ appDir: options.cwd, - isRspack: true, rscClientRuntimePath, rscServerRuntimePath, internalDirectory, diff --git a/packages/cli/builder/src/rsc/plugins/rsbuild-rsc-plugin.ts b/packages/cli/builder/src/rsc/plugins/rsbuild-rsc-plugin.ts index 1e187cd71e82..f23c375800fd 100644 --- a/packages/cli/builder/src/rsc/plugins/rsbuild-rsc-plugin.ts +++ b/packages/cli/builder/src/rsc/plugins/rsbuild-rsc-plugin.ts @@ -40,13 +40,11 @@ const checkReactVersionAtLeast19 = async (appDir: string) => { export const rsbuildRscPlugin = ({ appDir, - isRspack = true, rscClientRuntimePath, rscServerRuntimePath, internalDirectory, }: { appDir: string; - isRspack?: boolean; rscClientRuntimePath?: string; rscServerRuntimePath?: string; internalDirectory?: string; diff --git a/packages/cli/builder/src/rsc/plugins/rspack-rsc-client-plugin.ts b/packages/cli/builder/src/rsc/plugins/rspack-rsc-client-plugin.ts index 654181b1f583..8c19fc9078ef 100644 --- a/packages/cli/builder/src/rsc/plugins/rspack-rsc-client-plugin.ts +++ b/packages/cli/builder/src/rsc/plugins/rspack-rsc-client-plugin.ts @@ -81,11 +81,10 @@ export class RspackRscClientPlugin { const addClientReferencesChunks = ( compilation: Rspack.Compilation, - entryModule: Rspack.Module, callback: (err: any | null) => void, ) => { const promises = []; - [...this.clientReferencesMap.keys()].forEach((resourcePath, index) => { + [...this.clientReferencesMap.keys()].forEach(resourcePath => { const entries = compilation.entries.entries(); for (const [entryName, entry] of entries) { @@ -159,47 +158,44 @@ export class RspackRscClientPlugin { for (const entryModule of entryModules) { if (entryModule) { - addClientReferencesChunks(compilation, entryModule, callback); + addClientReferencesChunks(compilation, callback); } } }, ); - compiler.hooks.compilation.tap( - RspackRscClientPlugin.name, - (compilation, { normalModuleFactory }) => { - class EntryNameRuntimeModule extends RuntimeModule { - private entryName: string; - constructor(entryName: string) { - super('entry-name', 10); // Set a higher stage to ensure priority execution - this.entryName = entryName; - } + compiler.hooks.compilation.tap(RspackRscClientPlugin.name, compilation => { + class EntryNameRuntimeModule extends RuntimeModule { + private entryName: string; + constructor(entryName: string) { + super('entry-name', 10); // Set a higher stage to ensure priority execution + this.entryName = entryName; + } - generate() { - return `window.__MODERN_JS_ENTRY_NAME="${this.entryName}";`; - } + generate() { + return `window.__MODERN_JS_ENTRY_NAME="${this.entryName}";`; } + } - compilation.hooks.runtimeRequirementInTree - .for(RuntimeGlobals.ensureChunk) - .tap(RspackRscClientPlugin.name, (chunk, set) => { - Array.from(compilation.entrypoints.entries()).forEach( - ([entryName, entrypoint]) => { - if (entrypoint.chunks.includes(chunk)) { - compilation.addRuntimeModule( - chunk, - new EntryNameRuntimeModule(entryName), - ); - } - }, - ); - }); - }, - ); + compilation.hooks.runtimeRequirementInTree + .for(RuntimeGlobals.ensureChunk) + .tap(RspackRscClientPlugin.name, chunk => { + Array.from(compilation.entrypoints.entries()).forEach( + ([entryName, entrypoint]) => { + if (entrypoint.chunks.includes(chunk)) { + compilation.addRuntimeModule( + chunk, + new EntryNameRuntimeModule(entryName), + ); + } + }, + ); + }); + }); compiler.hooks.thisCompilation.tap( RspackRscClientPlugin.name, - (compilation, { normalModuleFactory }) => { + compilation => { this.styles = sharedData.get('styles') as Set; this.clientReferencesMap = sharedData.get( 'clientReferencesMap', diff --git a/packages/cli/builder/src/rsc/plugins/rspack-rsc-server-plugin.ts b/packages/cli/builder/src/rsc/plugins/rspack-rsc-server-plugin.ts index 19bcf0c0606c..a109f90f102e 100644 --- a/packages/cli/builder/src/rsc/plugins/rspack-rsc-server-plugin.ts +++ b/packages/cli/builder/src/rsc/plugins/rspack-rsc-server-plugin.ts @@ -433,21 +433,18 @@ export class RscServerPlugin { } }); - compiler.hooks.thisCompilation.tap( - RscServerPlugin.name, - (compilation, { normalModuleFactory }) => { - compilation.hooks.needAdditionalPass.tap( - RscServerPlugin.name, - () => !(needsAdditionalPass = !needsAdditionalPass), - ); + compiler.hooks.thisCompilation.tap(RscServerPlugin.name, compilation => { + compilation.hooks.needAdditionalPass.tap( + RscServerPlugin.name, + () => !(needsAdditionalPass = !needsAdditionalPass), + ); - compilation.hooks.processAssets.tap(RscServerPlugin.name, () => { - compilation.emitAsset( - this.serverManifestFilename, - new RawSource(JSON.stringify(this.serverManifest, null, 2), false), - ); - }); - }, - ); + compilation.hooks.processAssets.tap(RscServerPlugin.name, () => { + compilation.emitAsset( + this.serverManifestFilename, + new RawSource(JSON.stringify(this.serverManifest, null, 2), false), + ); + }); + }); } } diff --git a/packages/cli/builder/src/rsc/rsc-ssr-loader.ts b/packages/cli/builder/src/rsc/rsc-ssr-loader.ts index 7185f9b7d826..0bdc52b4f1a1 100644 --- a/packages/cli/builder/src/rsc/rsc-ssr-loader.ts +++ b/packages/cli/builder/src/rsc/rsc-ssr-loader.ts @@ -18,7 +18,6 @@ export default async function rscSsrLoader( ) { this.cacheable(true); const callback = this.async(); - const { entryPath2Name } = this.getOptions(); const ast = await parseSource(source); const hasDeclareServerDirective = await isServerModule(ast); const resourcePath = this.resourcePath; diff --git a/packages/cli/builder/src/shared/utils.ts b/packages/cli/builder/src/shared/utils.ts index c9ce9707f195..f3089f790ad1 100644 --- a/packages/cli/builder/src/shared/utils.ts +++ b/packages/cli/builder/src/shared/utils.ts @@ -1,30 +1,12 @@ -import type { - NormalizedEnvironmentConfig, - RsbuildContext, - RsbuildTarget, - RspackChain, -} from '@rsbuild/core'; +import type { NormalizedEnvironmentConfig, RsbuildTarget } from '@rsbuild/core'; import browserslist from 'browserslist'; export const RUNTIME_CHUNK_NAME = 'builder-runtime'; export const SERVICE_WORKER_ENVIRONMENT_NAME = 'workerSSR'; -export const JS_REGEX = /\.(?:js|mjs|cjs|jsx)$/; - -export const TS_REGEX = /\.(?:ts|mts|cts|tsx)$/; - -export const SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/; - export const NODE_MODULES_REGEX = /[\\/]node_modules[\\/]/; -export function isServerEnvironment( - target: RsbuildTarget, - environment: string, -) { - return target === 'node' || environment === SERVICE_WORKER_ENVIRONMENT_NAME; -} - export const castArray = (arr?: T | T[]): T[] => { if (arr === undefined) { return []; @@ -53,58 +35,6 @@ async function getBrowserslist(path: string): Promise { return null; } -export const getUseBuiltIns = ( - config: NormalizedEnvironmentConfig, -): false | 'usage' | 'entry' => { - const { polyfill } = config.output; - if (polyfill === 'off') { - return false; - } - return polyfill; -}; - -export function applyScriptCondition({ - rule, - chain, - config, - context, - includes, - excludes, -}: { - rule: RspackChain.Rule; - chain: RspackChain; - config: NormalizedEnvironmentConfig; - context: RsbuildContext; - includes: (string | RegExp)[]; - excludes: (string | RegExp)[]; -}): void { - // compile all folders in app directory, exclude node_modules - // which can be removed next version of rspack - rule.include.add({ - and: [context.rootPath, { not: NODE_MODULES_REGEX }], - }); - - // Always compile TS and JSX files. - // Otherwise, it will lead to compilation errors and incorrect output. - rule.include.add(/\.(?:ts|tsx|jsx|mts|cts)$/); - - // The Rsbuild runtime code is es2017 by default, - // transform the runtime code if user target < es2017 - const target = castArray(chain.get('target')); - const legacyTarget = ['es5', 'es6', 'es2015', 'es2016']; - if (legacyTarget.some(item => target.includes(item))) { - rule.include.add(/[\\/]@rsbuild[\\/]core[\\/]dist[\\/]/); - } - - for (const condition of [...includes, ...(config.source.include || [])]) { - rule.include.add(condition); - } - - for (const condition of [...excludes, ...(config.source.exclude || [])]) { - rule.exclude.add(condition); - } -} - export const isHtmlDisabled = ( config: NormalizedEnvironmentConfig, target: RsbuildTarget, diff --git a/packages/cli/builder/src/types.ts b/packages/cli/builder/src/types.ts index a39185107a09..e50725df2542 100644 --- a/packages/cli/builder/src/types.ts +++ b/packages/cli/builder/src/types.ts @@ -8,9 +8,7 @@ import type { HtmlConfig, OutputConfig, Polyfill, - RequestHandler, RsbuildConfig, - RsbuildPlugin, RsbuildPluginAPI, RsbuildPlugins, RsbuildTarget, diff --git a/packages/cli/builder/tsconfig.json b/packages/cli/builder/tsconfig.json index a733e6f393f1..ced361f9138a 100644 --- a/packages/cli/builder/tsconfig.json +++ b/packages/cli/builder/tsconfig.json @@ -3,7 +3,8 @@ "compilerOptions": { "module": "ES2020", "outDir": "./dist", - "baseUrl": "./", + "noUnusedLocals": true, + "noUnusedParameters": true, "rootDir": "src", "moduleResolution": "Bundler" },