diff --git a/packages/core/src/core/plugins/external.ts b/packages/core/src/core/plugins/external.ts index 3f45c2d6..3cbc7095 100644 --- a/packages/core/src/core/plugins/external.ts +++ b/packages/core/src/core/plugins/external.ts @@ -1,4 +1,4 @@ -import { isBuiltin } from 'node:module'; +import { builtinModules } from 'node:module'; import type { RsbuildPlugin, Rspack } from '@rsbuild/core'; import type { RstestContext } from '../../types'; import { ADDITIONAL_NODE_BUILTINS, castArray } from '../../utils'; @@ -10,7 +10,7 @@ const autoExternalNodeModules: ( result?: Rspack.ExternalItemValue, type?: Rspack.ExternalsType, ) => void, -) => void = ({ context, request, dependencyType, getResolve }, callback) => { +) => void = ({ context, request, getResolve }, callback) => { if (!request) { return callback(); } @@ -21,11 +21,7 @@ const autoExternalNodeModules: ( } const doExternal = (externalPath: string = request) => { - callback( - undefined, - externalPath, - dependencyType === 'commonjs' ? 'commonjs' : 'import', - ); + callback(undefined, externalPath, 'commonjs-import'); }; const resolver = getResolve?.(); @@ -48,7 +44,7 @@ const autoExternalNodeModules: ( }; function autoExternalNodeBuiltin( - { request, dependencyType }: Rspack.ExternalItemFunctionData, + { request }: Rspack.ExternalItemFunctionData, callback: ( err?: Error, result?: Rspack.ExternalItemValue, @@ -60,22 +56,16 @@ function autoExternalNodeBuiltin( return; } - const isNodeBuiltin = - isBuiltin(request) || - ADDITIONAL_NODE_BUILTINS.some((builtin) => { - if (typeof builtin === 'string') { - return builtin === request; - } + const isNodeBuiltin = ADDITIONAL_NODE_BUILTINS.some((builtin) => { + if (typeof builtin === 'string') { + return builtin === request; + } - return builtin.test(request); - }); + return builtin.test(request); + }); if (isNodeBuiltin) { - callback( - undefined, - request, - dependencyType === 'commonjs' ? 'commonjs' : 'module-import', - ); + callback(undefined, request, 'commonjs-import'); } else { callback(); } @@ -107,9 +97,11 @@ export const pluginExternal: (context: RstestContext) => RsbuildPlugin = ( '@rstest/core': 'global @rstest/core', }); + config.externalsType ??= 'commonjs-import'; + config.externalsPresets ??= {}; config.externalsPresets.node = false; - config.externals.push(autoExternalNodeBuiltin); + config.externals.push(...builtinModules, autoExternalNodeBuiltin); }, }, }); diff --git a/packages/core/tests/core/__snapshots__/rsbuild.test.ts.snap b/packages/core/tests/core/__snapshots__/rsbuild.test.ts.snap index 7c8778aa..7988bc33 100644 --- a/packages/core/tests/core/__snapshots__/rsbuild.test.ts.snap +++ b/packages/core/tests/core/__snapshots__/rsbuild.test.ts.snap @@ -20,11 +20,84 @@ exports[`prepareRsbuild > should generate rspack config correctly (jsdom) 1`] = { "@rstest/core": "global @rstest/core", }, + "_http_agent", + "_http_client", + "_http_common", + "_http_incoming", + "_http_outgoing", + "_http_server", + "_stream_duplex", + "_stream_passthrough", + "_stream_readable", + "_stream_transform", + "_stream_wrap", + "_stream_writable", + "_tls_common", + "_tls_wrap", + "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", + "node:sea", + "node:sqlite", + "node:test", + "node:test/reporters", [Function], ], "externalsPresets": { "node": false, }, + "externalsType": "commonjs-import", "ignoreWarnings": [ /Module not found/, ], @@ -544,11 +617,84 @@ exports[`prepareRsbuild > should generate rspack config correctly (node) 1`] = ` "@rstest/core": "global @rstest/core", }, [Function], + "_http_agent", + "_http_client", + "_http_common", + "_http_incoming", + "_http_outgoing", + "_http_server", + "_stream_duplex", + "_stream_passthrough", + "_stream_readable", + "_stream_transform", + "_stream_wrap", + "_stream_writable", + "_tls_common", + "_tls_wrap", + "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", + "node:sea", + "node:sqlite", + "node:test", + "node:test/reporters", [Function], ], "externalsPresets": { "node": false, }, + "externalsType": "commonjs-import", "ignoreWarnings": [ /Module not found/, ], @@ -1059,11 +1205,84 @@ exports[`prepareRsbuild > should generate rspack config correctly in watch mode "@rstest/core": "global @rstest/core", }, [Function], + "_http_agent", + "_http_client", + "_http_common", + "_http_incoming", + "_http_outgoing", + "_http_server", + "_stream_duplex", + "_stream_passthrough", + "_stream_readable", + "_stream_transform", + "_stream_wrap", + "_stream_writable", + "_tls_common", + "_tls_wrap", + "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", + "node:sea", + "node:sqlite", + "node:test", + "node:test/reporters", [Function], ], "externalsPresets": { "node": false, }, + "externalsType": "commonjs-import", "ignoreWarnings": [ /Module not found/, ], @@ -1581,11 +1800,84 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects { "@rstest/core": "global @rstest/core", }, + "_http_agent", + "_http_client", + "_http_common", + "_http_incoming", + "_http_outgoing", + "_http_server", + "_stream_duplex", + "_stream_passthrough", + "_stream_readable", + "_stream_transform", + "_stream_wrap", + "_stream_writable", + "_tls_common", + "_tls_wrap", + "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", + "node:sea", + "node:sqlite", + "node:test", + "node:test/reporters", [Function], ], "externalsPresets": { "node": false, }, + "externalsType": "commonjs-import", "ignoreWarnings": [ /Module not found/, ], @@ -2105,11 +2397,84 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects "@rstest/core": "global @rstest/core", }, [Function], + "_http_agent", + "_http_client", + "_http_common", + "_http_incoming", + "_http_outgoing", + "_http_server", + "_stream_duplex", + "_stream_passthrough", + "_stream_readable", + "_stream_transform", + "_stream_wrap", + "_stream_writable", + "_tls_common", + "_tls_wrap", + "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", + "node:sea", + "node:sqlite", + "node:test", + "node:test/reporters", [Function], ], "externalsPresets": { "node": false, }, + "externalsType": "commonjs-import", "ignoreWarnings": [ /Module not found/, ],