Skip to content
Merged
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/grumpy-jars-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modern-js/builder': patch
---

refactor(builder): clean up unused code
1 change: 0 additions & 1 deletion packages/cli/builder/src/createBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export async function parseConfig(
rsbuildPlugins.push(
rsbuildRscPlugin({
appDir: options.cwd,
isRspack: true,
rscClientRuntimePath,
rscServerRuntimePath,
internalDirectory,
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/builder/src/rsc/plugins/rsbuild-rsc-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
60 changes: 28 additions & 32 deletions packages/cli/builder/src/rsc/plugins/rspack-rsc-client-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<string>;
this.clientReferencesMap = sharedData.get(
'clientReferencesMap',
Expand Down
27 changes: 12 additions & 15 deletions packages/cli/builder/src/rsc/plugins/rspack-rsc-server-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
});
});
}
}
1 change: 0 additions & 1 deletion packages/cli/builder/src/rsc/rsc-ssr-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
72 changes: 1 addition & 71 deletions packages/cli/builder/src/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -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 = <T>(arr?: T | T[]): T[] => {
if (arr === undefined) {
return [];
Expand Down Expand Up @@ -53,58 +35,6 @@ async function getBrowserslist(path: string): Promise<string[] | null> {
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,
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import type {
HtmlConfig,
OutputConfig,
Polyfill,
RequestHandler,
RsbuildConfig,
RsbuildPlugin,
RsbuildPluginAPI,
RsbuildPlugins,
RsbuildTarget,
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/builder/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"module": "ES2020",
"outDir": "./dist",
"baseUrl": "./",
"noUnusedLocals": true,
"noUnusedParameters": true,
"rootDir": "src",
"moduleResolution": "Bundler"
},
Expand Down