Skip to content

Commit

Permalink
feat(module-plugin-polyfill): support polyfill Node.js builtin module…
Browse files Browse the repository at this point in the history
…s starting with node:
  • Loading branch information
Timeless0911 committed Sep 29, 2024
1 parent ae3e230 commit 38f5d2c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/modern-feet-bake.md
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions packages/module/plugin-module-node-polyfill/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,7 +14,7 @@ export interface NodePolyfillPluginOptions {
overrides?: Partial<Record<keyof typeof modules, string>>;
}

export const modules = {
let modules = {
assert: require.resolve('assert/'),
buffer: require.resolve('buffer/'),
child_process: null,
Expand Down Expand Up @@ -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),
Expand Down
13 changes: 13 additions & 0 deletions packages/module/plugin-module-node-polyfill/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,16 @@ export function addResolveFallback(

return newObject;
}

export function addNodePrefix(
modules: Record<string, string | null>,
): Record<string, string | null> {
return Object.fromEntries(
Object.entries(modules).map(([key, value]) => {
if (!key.startsWith('_')) {
return [`node:${key}`, value];
}
return [key, value];
}),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())');
});
});
3 changes: 3 additions & 0 deletions tests/integration/module/plugins/node-polyfill/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
import os from 'node:os';
import path from 'path';

export const value = Buffer.from('value');

0 comments on commit 38f5d2c

Please sign in to comment.