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
9 changes: 9 additions & 0 deletions e2e/dom/fixtures/test/node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, expect, it } from '@rstest/core';
// @ts-expect-error node builtin module
import _http_common from '_http_common';

describe('node built-in modules', () => {
it('should load node built-in modules correctly', () => {
expect(_http_common).toBeDefined();
});
});
5 changes: 5 additions & 0 deletions e2e/dom/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ describe('happy-dom', () => {
await expectExecSuccess();
});

it('should load node built-in modules correctly', async () => {
const { expectExecSuccess } = await runCli('test/node', 'happy-dom');
await expectExecSuccess();
});

it('should run test correctly with custom externals', async () => {
const { expectExecSuccess } = await runCli(appFilters, 'happy-dom', {
args: externalConfigArgs,
Expand Down
17 changes: 10 additions & 7 deletions packages/core/src/core/plugins/external.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isBuiltin } from 'node:module';
import type { RsbuildPlugin, Rspack } from '@rsbuild/core';
import type { RstestContext } from '../../types';
import { castArray, NODE_BUILTINS } from '../../utils';
import { ADDITIONAL_NODE_BUILTINS, castArray } from '../../utils';

const autoExternalNodeModules: (
data: Rspack.ExternalItemFunctionData,
Expand Down Expand Up @@ -59,13 +60,15 @@ function autoExternalNodeBuiltin(
return;
}

const isNodeBuiltin = NODE_BUILTINS.some((builtin) => {
if (typeof builtin === 'string') {
return builtin === request;
}
const isNodeBuiltin =
isBuiltin(request) ||
ADDITIONAL_NODE_BUILTINS.some((builtin) => {
if (typeof builtin === 'string') {
return builtin === request;
}

return builtin.test(request);
});
return builtin.test(request);
});

if (isNodeBuiltin) {
callback(
Expand Down
58 changes: 1 addition & 57 deletions packages/core/src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,64 +170,8 @@ export const needFlagExperimentalDetectModule = (): boolean => {
return false;
};

// Ported from https://github.com/webpack/webpack/blob/21b28a82f7a6ec677752e1c8fb722a830a2adf69/lib/node/NodeTargetPlugin.js.
export const NODE_BUILTINS: (string | RegExp)[] = [
'assert',
'assert/strict',
'async_hooks',
'buffer',
'child_process',
'cluster',
'console',
'constants',
'crypto',
'dgram',
'diagnostics_channel',
'dns',
'dns/promises',
'domain',
'events',
'fs',
'fs/promises',
'http',
'http2',
'https',
'inspector',
'inspector/promises',
'module',
'net',
'os',
'path',
'path/posix',
'path/win32',
'perf_hooks',
'process',
'punycode',
'querystring',
'readline',
'readline/promises',
'repl',
'stream',
'stream/consumers',
'stream/promises',
'stream/web',
'string_decoder',
'sys',
'timers',
'timers/promises',
'tls',
'trace_events',
'tty',
'url',
'util',
'util/types',
'v8',
'vm',
'wasi',
'worker_threads',
'zlib',
export const ADDITIONAL_NODE_BUILTINS: (string | RegExp)[] = [
/^node:/,

// cspell:word pnpapi
// Yarn PnP adds pnpapi as "builtin"
'pnpapi',
Expand Down
Loading