Skip to content

Commit 8d58000

Browse files
authored
refactor(builder): clean up unused code (#7835)
1 parent 88e5ff1 commit 8d58000

File tree

9 files changed

+48
-125
lines changed

9 files changed

+48
-125
lines changed

.changeset/grumpy-jars-reply.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modern-js/builder': patch
3+
---
4+
5+
refactor(builder): clean up unused code

packages/cli/builder/src/createBuilder.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ export async function parseConfig(
113113
rsbuildPlugins.push(
114114
rsbuildRscPlugin({
115115
appDir: options.cwd,
116-
isRspack: true,
117116
rscClientRuntimePath,
118117
rscServerRuntimePath,
119118
internalDirectory,

packages/cli/builder/src/rsc/plugins/rsbuild-rsc-plugin.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ const checkReactVersionAtLeast19 = async (appDir: string) => {
4040

4141
export const rsbuildRscPlugin = ({
4242
appDir,
43-
isRspack = true,
4443
rscClientRuntimePath,
4544
rscServerRuntimePath,
4645
internalDirectory,
4746
}: {
4847
appDir: string;
49-
isRspack?: boolean;
5048
rscClientRuntimePath?: string;
5149
rscServerRuntimePath?: string;
5250
internalDirectory?: string;

packages/cli/builder/src/rsc/plugins/rspack-rsc-client-plugin.ts

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,10 @@ export class RspackRscClientPlugin {
8181

8282
const addClientReferencesChunks = (
8383
compilation: Rspack.Compilation,
84-
entryModule: Rspack.Module,
8584
callback: (err: any | null) => void,
8685
) => {
8786
const promises = [];
88-
[...this.clientReferencesMap.keys()].forEach((resourcePath, index) => {
87+
[...this.clientReferencesMap.keys()].forEach(resourcePath => {
8988
const entries = compilation.entries.entries();
9089

9190
for (const [entryName, entry] of entries) {
@@ -159,47 +158,44 @@ export class RspackRscClientPlugin {
159158

160159
for (const entryModule of entryModules) {
161160
if (entryModule) {
162-
addClientReferencesChunks(compilation, entryModule, callback);
161+
addClientReferencesChunks(compilation, callback);
163162
}
164163
}
165164
},
166165
);
167166

168-
compiler.hooks.compilation.tap(
169-
RspackRscClientPlugin.name,
170-
(compilation, { normalModuleFactory }) => {
171-
class EntryNameRuntimeModule extends RuntimeModule {
172-
private entryName: string;
173-
constructor(entryName: string) {
174-
super('entry-name', 10); // Set a higher stage to ensure priority execution
175-
this.entryName = entryName;
176-
}
167+
compiler.hooks.compilation.tap(RspackRscClientPlugin.name, compilation => {
168+
class EntryNameRuntimeModule extends RuntimeModule {
169+
private entryName: string;
170+
constructor(entryName: string) {
171+
super('entry-name', 10); // Set a higher stage to ensure priority execution
172+
this.entryName = entryName;
173+
}
177174

178-
generate() {
179-
return `window.__MODERN_JS_ENTRY_NAME="${this.entryName}";`;
180-
}
175+
generate() {
176+
return `window.__MODERN_JS_ENTRY_NAME="${this.entryName}";`;
181177
}
178+
}
182179

183-
compilation.hooks.runtimeRequirementInTree
184-
.for(RuntimeGlobals.ensureChunk)
185-
.tap(RspackRscClientPlugin.name, (chunk, set) => {
186-
Array.from(compilation.entrypoints.entries()).forEach(
187-
([entryName, entrypoint]) => {
188-
if (entrypoint.chunks.includes(chunk)) {
189-
compilation.addRuntimeModule(
190-
chunk,
191-
new EntryNameRuntimeModule(entryName),
192-
);
193-
}
194-
},
195-
);
196-
});
197-
},
198-
);
180+
compilation.hooks.runtimeRequirementInTree
181+
.for(RuntimeGlobals.ensureChunk)
182+
.tap(RspackRscClientPlugin.name, chunk => {
183+
Array.from(compilation.entrypoints.entries()).forEach(
184+
([entryName, entrypoint]) => {
185+
if (entrypoint.chunks.includes(chunk)) {
186+
compilation.addRuntimeModule(
187+
chunk,
188+
new EntryNameRuntimeModule(entryName),
189+
);
190+
}
191+
},
192+
);
193+
});
194+
});
199195

200196
compiler.hooks.thisCompilation.tap(
201197
RspackRscClientPlugin.name,
202-
(compilation, { normalModuleFactory }) => {
198+
compilation => {
203199
this.styles = sharedData.get('styles') as Set<string>;
204200
this.clientReferencesMap = sharedData.get(
205201
'clientReferencesMap',

packages/cli/builder/src/rsc/plugins/rspack-rsc-server-plugin.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -433,21 +433,18 @@ export class RscServerPlugin {
433433
}
434434
});
435435

436-
compiler.hooks.thisCompilation.tap(
437-
RscServerPlugin.name,
438-
(compilation, { normalModuleFactory }) => {
439-
compilation.hooks.needAdditionalPass.tap(
440-
RscServerPlugin.name,
441-
() => !(needsAdditionalPass = !needsAdditionalPass),
442-
);
436+
compiler.hooks.thisCompilation.tap(RscServerPlugin.name, compilation => {
437+
compilation.hooks.needAdditionalPass.tap(
438+
RscServerPlugin.name,
439+
() => !(needsAdditionalPass = !needsAdditionalPass),
440+
);
443441

444-
compilation.hooks.processAssets.tap(RscServerPlugin.name, () => {
445-
compilation.emitAsset(
446-
this.serverManifestFilename,
447-
new RawSource(JSON.stringify(this.serverManifest, null, 2), false),
448-
);
449-
});
450-
},
451-
);
442+
compilation.hooks.processAssets.tap(RscServerPlugin.name, () => {
443+
compilation.emitAsset(
444+
this.serverManifestFilename,
445+
new RawSource(JSON.stringify(this.serverManifest, null, 2), false),
446+
);
447+
});
448+
});
452449
}
453450
}

packages/cli/builder/src/rsc/rsc-ssr-loader.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export default async function rscSsrLoader(
1818
) {
1919
this.cacheable(true);
2020
const callback = this.async();
21-
const { entryPath2Name } = this.getOptions();
2221
const ast = await parseSource(source);
2322
const hasDeclareServerDirective = await isServerModule(ast);
2423
const resourcePath = this.resourcePath;

packages/cli/builder/src/shared/utils.ts

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
1-
import type {
2-
NormalizedEnvironmentConfig,
3-
RsbuildContext,
4-
RsbuildTarget,
5-
RspackChain,
6-
} from '@rsbuild/core';
1+
import type { NormalizedEnvironmentConfig, RsbuildTarget } from '@rsbuild/core';
72
import browserslist from 'browserslist';
83

94
export const RUNTIME_CHUNK_NAME = 'builder-runtime';
105

116
export const SERVICE_WORKER_ENVIRONMENT_NAME = 'workerSSR';
127

13-
export const JS_REGEX = /\.(?:js|mjs|cjs|jsx)$/;
14-
15-
export const TS_REGEX = /\.(?:ts|mts|cts|tsx)$/;
16-
17-
export const SCRIPT_REGEX = /\.(?:js|jsx|mjs|cjs|ts|tsx|mts|cts)$/;
18-
198
export const NODE_MODULES_REGEX = /[\\/]node_modules[\\/]/;
209

21-
export function isServerEnvironment(
22-
target: RsbuildTarget,
23-
environment: string,
24-
) {
25-
return target === 'node' || environment === SERVICE_WORKER_ENVIRONMENT_NAME;
26-
}
27-
2810
export const castArray = <T>(arr?: T | T[]): T[] => {
2911
if (arr === undefined) {
3012
return [];
@@ -53,58 +35,6 @@ async function getBrowserslist(path: string): Promise<string[] | null> {
5335
return null;
5436
}
5537

56-
export const getUseBuiltIns = (
57-
config: NormalizedEnvironmentConfig,
58-
): false | 'usage' | 'entry' => {
59-
const { polyfill } = config.output;
60-
if (polyfill === 'off') {
61-
return false;
62-
}
63-
return polyfill;
64-
};
65-
66-
export function applyScriptCondition({
67-
rule,
68-
chain,
69-
config,
70-
context,
71-
includes,
72-
excludes,
73-
}: {
74-
rule: RspackChain.Rule;
75-
chain: RspackChain;
76-
config: NormalizedEnvironmentConfig;
77-
context: RsbuildContext;
78-
includes: (string | RegExp)[];
79-
excludes: (string | RegExp)[];
80-
}): void {
81-
// compile all folders in app directory, exclude node_modules
82-
// which can be removed next version of rspack
83-
rule.include.add({
84-
and: [context.rootPath, { not: NODE_MODULES_REGEX }],
85-
});
86-
87-
// Always compile TS and JSX files.
88-
// Otherwise, it will lead to compilation errors and incorrect output.
89-
rule.include.add(/\.(?:ts|tsx|jsx|mts|cts)$/);
90-
91-
// The Rsbuild runtime code is es2017 by default,
92-
// transform the runtime code if user target < es2017
93-
const target = castArray(chain.get('target'));
94-
const legacyTarget = ['es5', 'es6', 'es2015', 'es2016'];
95-
if (legacyTarget.some(item => target.includes(item))) {
96-
rule.include.add(/[\\/]@rsbuild[\\/]core[\\/]dist[\\/]/);
97-
}
98-
99-
for (const condition of [...includes, ...(config.source.include || [])]) {
100-
rule.include.add(condition);
101-
}
102-
103-
for (const condition of [...excludes, ...(config.source.exclude || [])]) {
104-
rule.exclude.add(condition);
105-
}
106-
}
107-
10838
export const isHtmlDisabled = (
10939
config: NormalizedEnvironmentConfig,
11040
target: RsbuildTarget,

packages/cli/builder/src/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import type {
88
HtmlConfig,
99
OutputConfig,
1010
Polyfill,
11-
RequestHandler,
1211
RsbuildConfig,
13-
RsbuildPlugin,
1412
RsbuildPluginAPI,
1513
RsbuildPlugins,
1614
RsbuildTarget,

packages/cli/builder/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"compilerOptions": {
44
"module": "ES2020",
55
"outDir": "./dist",
6-
"baseUrl": "./",
6+
"noUnusedLocals": true,
7+
"noUnusedParameters": true,
78
"rootDir": "src",
89
"moduleResolution": "Bundler"
910
},

0 commit comments

Comments
 (0)