Skip to content

Commit 30db6e8

Browse files
authored
fix: update node builtin modules (#604)
1 parent 9cd1456 commit 30db6e8

File tree

4 files changed

+25
-64
lines changed

4 files changed

+25
-64
lines changed

e2e/dom/fixtures/test/node.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { describe, expect, it } from '@rstest/core';
2+
// @ts-expect-error node builtin module
3+
import _http_common from '_http_common';
4+
5+
describe('node built-in modules', () => {
6+
it('should load node built-in modules correctly', () => {
7+
expect(_http_common).toBeDefined();
8+
});
9+
});

e2e/dom/index.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ describe('happy-dom', () => {
3131
await expectExecSuccess();
3232
});
3333

34+
it('should load node built-in modules correctly', async () => {
35+
const { expectExecSuccess } = await runCli('test/node', 'happy-dom');
36+
await expectExecSuccess();
37+
});
38+
3439
it('should run test correctly with custom externals', async () => {
3540
const { expectExecSuccess } = await runCli(appFilters, 'happy-dom', {
3641
args: externalConfigArgs,

packages/core/src/core/plugins/external.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { isBuiltin } from 'node:module';
12
import type { RsbuildPlugin, Rspack } from '@rsbuild/core';
23
import type { RstestContext } from '../../types';
3-
import { castArray, NODE_BUILTINS } from '../../utils';
4+
import { ADDITIONAL_NODE_BUILTINS, castArray } from '../../utils';
45

56
const autoExternalNodeModules: (
67
data: Rspack.ExternalItemFunctionData,
@@ -59,13 +60,15 @@ function autoExternalNodeBuiltin(
5960
return;
6061
}
6162

62-
const isNodeBuiltin = NODE_BUILTINS.some((builtin) => {
63-
if (typeof builtin === 'string') {
64-
return builtin === request;
65-
}
63+
const isNodeBuiltin =
64+
isBuiltin(request) ||
65+
ADDITIONAL_NODE_BUILTINS.some((builtin) => {
66+
if (typeof builtin === 'string') {
67+
return builtin === request;
68+
}
6669

67-
return builtin.test(request);
68-
});
70+
return builtin.test(request);
71+
});
6972

7073
if (isNodeBuiltin) {
7174
callback(

packages/core/src/utils/helper.ts

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -170,64 +170,8 @@ export const needFlagExperimentalDetectModule = (): boolean => {
170170
return false;
171171
};
172172

173-
// Ported from https://github.com/webpack/webpack/blob/21b28a82f7a6ec677752e1c8fb722a830a2adf69/lib/node/NodeTargetPlugin.js.
174-
export const NODE_BUILTINS: (string | RegExp)[] = [
175-
'assert',
176-
'assert/strict',
177-
'async_hooks',
178-
'buffer',
179-
'child_process',
180-
'cluster',
181-
'console',
182-
'constants',
183-
'crypto',
184-
'dgram',
185-
'diagnostics_channel',
186-
'dns',
187-
'dns/promises',
188-
'domain',
189-
'events',
190-
'fs',
191-
'fs/promises',
192-
'http',
193-
'http2',
194-
'https',
195-
'inspector',
196-
'inspector/promises',
197-
'module',
198-
'net',
199-
'os',
200-
'path',
201-
'path/posix',
202-
'path/win32',
203-
'perf_hooks',
204-
'process',
205-
'punycode',
206-
'querystring',
207-
'readline',
208-
'readline/promises',
209-
'repl',
210-
'stream',
211-
'stream/consumers',
212-
'stream/promises',
213-
'stream/web',
214-
'string_decoder',
215-
'sys',
216-
'timers',
217-
'timers/promises',
218-
'tls',
219-
'trace_events',
220-
'tty',
221-
'url',
222-
'util',
223-
'util/types',
224-
'v8',
225-
'vm',
226-
'wasi',
227-
'worker_threads',
228-
'zlib',
173+
export const ADDITIONAL_NODE_BUILTINS: (string | RegExp)[] = [
229174
/^node:/,
230-
231175
// cspell:word pnpapi
232176
// Yarn PnP adds pnpapi as "builtin"
233177
'pnpapi',

0 commit comments

Comments
 (0)