Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module-plugin-polyfill): support polyfill Node.js builtin modules starting with node: #6315

Merged
merged 1 commit into from
Sep 29, 2024
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
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');
Loading