diff --git a/.changeset/modern-feet-bake.md b/.changeset/modern-feet-bake.md new file mode 100644 index 000000000000..b00b947e9ea7 --- /dev/null +++ b/.changeset/modern-feet-bake.md @@ -0,0 +1,6 @@ +--- +'@modern-js/plugin-module-node-polyfill': patch +--- + +feat(module-plugin-polyfill): support polyfill Node.js builtin modules starting with node: +feat(module-plugin-polyfill): 支持对 node: 开头的 Node.js builtin modules 进行 polyfill diff --git a/packages/module/plugin-module-node-polyfill/src/index.ts b/packages/module/plugin-module-node-polyfill/src/index.ts index a9f1738ac7d5..8017d9044ec6 100644 --- a/packages/module/plugin-module-node-polyfill/src/index.ts +++ b/packages/module/plugin-module-node-polyfill/src/index.ts @@ -5,7 +5,7 @@ import type { ICompiler, ModuleTools, } from '@modern-js/module-tools'; -import { addResolveFallback, excludeObjectKeys } from './utils'; +import { addNodePrefix, addResolveFallback, excludeObjectKeys } from './utils'; export interface NodePolyfillPluginOptions { // like https://github.com/Richienb/node-polyfill-webpack-plugin#excludealiases @@ -14,7 +14,7 @@ export interface NodePolyfillPluginOptions { overrides?: Partial>; } -export const modules = { +let modules = { assert: require.resolve('assert/'), buffer: require.resolve('buffer/'), child_process: null, @@ -60,6 +60,8 @@ export const modules = { export const getNodePolyfillHook = ( polyfillOption: NodePolyfillPluginOptions = {}, ) => { + const nodeModules = addNodePrefix(modules); + modules = Object.assign(modules, nodeModules); const polyfillModules = { ...excludeObjectKeys( addResolveFallback(modules, polyfillOption.overrides), diff --git a/packages/module/plugin-module-node-polyfill/src/utils.ts b/packages/module/plugin-module-node-polyfill/src/utils.ts index 274e6f865cf4..b99a05d97968 100644 --- a/packages/module/plugin-module-node-polyfill/src/utils.ts +++ b/packages/module/plugin-module-node-polyfill/src/utils.ts @@ -42,3 +42,16 @@ export function addResolveFallback( return newObject; } + +export function addNodePrefix( + modules: Record, +): Record { + return Object.fromEntries( + Object.entries(modules).map(([key, value]) => { + if (!key.startsWith('_')) { + return [`node:${key}`, value]; + } + return [key, value]; + }), + ); +} diff --git a/tests/integration/module/plugins/node-polyfill/node-polyfill.test.ts b/tests/integration/module/plugins/node-polyfill/node-polyfill.test.ts index fe61144c259c..b488933f76f8 100644 --- a/tests/integration/module/plugins/node-polyfill/node-polyfill.test.ts +++ b/tests/integration/module/plugins/node-polyfill/node-polyfill.test.ts @@ -15,6 +15,8 @@ describe('plugin-node-polyfill', () => { path.resolve(__dirname, 'dist/index.js'), 'utf8', ); - expect(content).toContain('init_globals'); + expect(content).toContain('init_globals()'); + expect(content).toContain('__toESM(require_browser2())'); + expect(content).toContain('__toESM(require_path_browserify())'); }); }); diff --git a/tests/integration/module/plugins/node-polyfill/src/index.js b/tests/integration/module/plugins/node-polyfill/src/index.js index 839578246bd1..d2e9394c89e5 100644 --- a/tests/integration/module/plugins/node-polyfill/src/index.js +++ b/tests/integration/module/plugins/node-polyfill/src/index.js @@ -1 +1,4 @@ +import os from 'node:os'; +import path from 'path'; + export const value = Buffer.from('value');