Skip to content

Commit 3c005ca

Browse files
authored
perf: replace compiled imports with require (#6308)
1 parent 9742a0d commit 3c005ca

23 files changed

+132
-86
lines changed

packages/core/rslib.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const externals: Rspack.Configuration['externals'] = [
4444
for (const [name, test] of entries) {
4545
if (request === name) {
4646
throw new Error(
47-
`"${name}" is not allowed to be imported, use "../compiled/${name}/index.js" instead.`,
47+
`"${name}" is not allowed to be imported, use "requireCompiledPackage" instead.`,
4848
);
4949
}
5050
if (test.test(request)) {

packages/core/src/helpers/format.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { StatsError } from '@rspack/core';
2-
import color from '../../compiled/picocolors/index.js';
2+
import { color } from './vendors';
33

44
const formatFileName = (fileName: string) => {
55
// add default column add lines for linking

packages/core/src/helpers/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { posix } from 'node:path';
22
import { URL } from 'node:url';
33
import deepmerge from 'deepmerge';
4-
import color from '../../compiled/picocolors/index.js';
54
import RspackChain from '../../compiled/rspack-chain';
65
import { DEFAULT_ASSET_PREFIX } from '../constants';
76
import type {
@@ -11,12 +10,13 @@ import type {
1110
RsbuildTarget,
1211
Rspack,
1312
} from '../types';
13+
import { color } from './vendors';
1414

1515
export * from './fs';
1616
export * from './path';
1717
export * from './stats';
18-
19-
export { color, RspackChain };
18+
export * from './vendors';
19+
export { RspackChain };
2020

2121
// Lazy compilation was stabilized in Rspack v1.5.0
2222
export const rspackMinVersion = '1.5.0';

packages/core/src/helpers/stats.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import color from '../../compiled/picocolors/index.js';
21
import { logger } from '../logger';
32
import type { ActionType, RsbuildStats, Rspack } from '../types';
43
import { isMultiCompiler } from './';
54
import { formatStatsError } from './format';
5+
import { color } from './vendors';
66

77
function formatErrorMessage(errors: string[]) {
88
if (!errors.length) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { createRequire } from 'node:module';
2+
import { COMPILED_PATH } from '../constants';
3+
4+
const require = createRequire(import.meta.url);
5+
6+
type CompiledPackages = {
7+
ws: typeof import('../../compiled/ws').default;
8+
cors: typeof import('../../compiled/cors').default;
9+
sirv: typeof import('../../compiled/sirv');
10+
rslog: typeof import('../../compiled/rslog');
11+
memfs: typeof import('../../compiled/memfs');
12+
mrmime: typeof import('../../compiled/mrmime');
13+
connect: typeof import('../../compiled/connect').default;
14+
chokidar: typeof import('../../compiled/chokidar').default;
15+
tinyglobby: typeof import('../../compiled/tinyglobby');
16+
picocolors: typeof import('../../compiled/picocolors').default;
17+
'html-rspack-plugin': typeof import('../../compiled/html-rspack-plugin').default;
18+
'http-proxy-middleware': typeof import('../../compiled/http-proxy-middleware');
19+
'webpack-bundle-analyzer': typeof import('../../compiled/webpack-bundle-analyzer');
20+
'rspack-manifest-plugin': typeof import('../../compiled/rspack-manifest-plugin');
21+
'launch-editor-middleware': typeof import('../../compiled/launch-editor-middleware');
22+
'@jridgewell/remapping': typeof import('../../compiled/@jridgewell/remapping');
23+
'@jridgewell/trace-mapping': typeof import('../../compiled/@jridgewell/trace-mapping');
24+
};
25+
26+
/**
27+
* Load compiled package from `compiled` folder.
28+
* use `require()` as compiled packages are CommonJS modules.
29+
* https://github.com/nodejs/node/issues/59913
30+
* @param name
31+
* @returns
32+
*/
33+
export const requireCompiledPackage = <T extends keyof CompiledPackages>(
34+
name: T,
35+
): CompiledPackages[T] => require(`${COMPILED_PATH}/${name}/index.js`);
36+
37+
export const color: typeof import('../../compiled/picocolors').default =
38+
requireCompiledPackage('picocolors');

packages/core/src/loader/transformLoader.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import type { LoaderDefinition, RawSourceMap } from '@rspack/core';
2+
import { requireCompiledPackage } from '../helpers';
23
import type { EnvironmentContext } from '../types';
34

45
export type TransformLoaderOptions = {
56
id: string;
67
getEnvironment: () => EnvironmentContext;
78
};
89

9-
const mergeSourceMap = async (
10+
const mergeSourceMap = (
1011
originalSourceMap?: RawSourceMap | string,
1112
generatedSourceMap?: RawSourceMap | string | null,
12-
): Promise<RawSourceMap | string | undefined> => {
13+
): RawSourceMap | string | undefined => {
1314
if (!originalSourceMap || !generatedSourceMap) {
1415
return generatedSourceMap ?? originalSourceMap;
1516
}
1617

17-
const { default: remapping } = await import(
18-
'../../compiled/@jridgewell/remapping/index.js'
19-
);
18+
const remapping = requireCompiledPackage('@jridgewell/remapping');
2019
return remapping([generatedSourceMap, originalSourceMap], () => null);
2120
};
2221

@@ -65,7 +64,7 @@ const transformLoader: LoaderDefinition<TransformLoaderOptions> =
6564
return;
6665
}
6766

68-
const mergedMap = await mergeSourceMap(map, result.map);
67+
const mergedMap = mergeSourceMap(map, result.map);
6968
callback(null, result.code, mergedMap);
7069
} catch (error) {
7170
if (error instanceof Error) {

packages/core/src/logger.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
* This convention helps distinguish between normal operations
1313
* and important alerts that require attention.
1414
*/
15-
import { type Logger, logger } from '../compiled/rslog/index.js';
16-
import { color } from './helpers';
15+
import type { Logger } from '../compiled/rslog/index.js';
16+
import { color, requireCompiledPackage } from './helpers/vendors';
17+
18+
const logger: Logger = requireCompiledPackage('rslog').logger;
1719

1820
export const isDebug = (): boolean => {
1921
if (!process.env.DEBUG) {

packages/core/src/pluginHelper.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/**
22
* This file is used to get/set the global instance for html-plugin and css-extract plugin.
33
*/
4-
import { createRequire } from 'node:module';
4+
5+
import { requireCompiledPackage } from './helpers';
56
import { rspack } from './rspack';
67
import type { HtmlRspackPlugin } from './types';
78

8-
const require = createRequire(import.meta.url);
9-
109
let htmlPlugin: typeof HtmlRspackPlugin;
1110

1211
/**
@@ -20,7 +19,7 @@ export const setHTMLPlugin = (plugin: typeof HtmlRspackPlugin): void => {
2019

2120
export const getHTMLPlugin = (): typeof HtmlRspackPlugin => {
2221
if (!htmlPlugin) {
23-
htmlPlugin = require('../compiled/html-rspack-plugin/index.js');
22+
htmlPlugin = requireCompiledPackage('html-rspack-plugin');
2423
}
2524
return htmlPlugin;
2625
};

packages/core/src/plugins/appIcon.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getPublicPathFromCompiler,
99
isURL,
1010
pick,
11+
requireCompiledPackage,
1112
} from '../helpers';
1213
import type { AppIconItem, HtmlBasicTag, RsbuildPlugin } from '../types';
1314

@@ -91,7 +92,7 @@ export const pluginAppIcon = (): RsbuildPlugin => ({
9192
return;
9293
}
9394

94-
const { lookup } = await import('../../compiled/mrmime/index.js');
95+
const { lookup } = requireCompiledPackage('mrmime');
9596

9697
const distDir = config.output.distPath.image;
9798
const manifestFile = appIcon.filename ?? 'manifest.webmanifest';

packages/core/src/plugins/bundleAnalyzer.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { requireCompiledPackage } from '../helpers';
12
import type {
23
NormalizedEnvironmentConfig,
34
RsbuildConfig,
@@ -36,15 +37,15 @@ export function pluginBundleAnalyzer(): RsbuildPlugin {
3637
},
3738
});
3839

39-
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment }) => {
40+
api.modifyBundlerChain((chain, { CHAIN_ID, environment }) => {
4041
const { config } = environment;
4142

4243
if (!isUseAnalyzer(config)) {
4344
return;
4445
}
4546

46-
const { default: BundleAnalyzer } = await import(
47-
'../../compiled/webpack-bundle-analyzer/index.js'
47+
const BundleAnalyzer = requireCompiledPackage(
48+
'webpack-bundle-analyzer',
4849
);
4950

5051
chain

0 commit comments

Comments
 (0)