Skip to content

Commit 08f793b

Browse files
committedMay 24, 2024·
Stop polyfilling console, 'domain', process, and internal stream modules by default; rename includeAliases to additionalAliases, and allow ignoring the defaults with onlyAliases
Fixes #40, fixes #52, fixes #18, fixes #44, fixes #20 Signed-off-by: Richie Bendall <richiebendall@gmail.com>
1 parent 6b4feb8 commit 08f793b

File tree

4 files changed

+118
-34
lines changed

4 files changed

+118
-34
lines changed
 

‎index.d.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,26 @@ declare namespace NodePolyfillPlugin {
3333
| 'vm'
3434
| 'zlib';
3535

36-
export type IncludeOptions = {
36+
export type OnlyAliasesOptions = {
3737
/**
38-
By default, the modules that were polyfilled in Webpack 4 are mirrored over. However, you can choose to only include certain aliases. For example, you can only have `console` polyfilled.
38+
You can choose to only include certain aliases, ignoring the defaults. For example, you can have only `console` polyfilled.
3939
*/
40-
includeAliases?: readonly Alias[];
40+
onlyAliases?: readonly Alias[];
4141
};
4242

43-
export type ExcludeOptions = {
43+
export type AdditionalExcludeAliasesOptions = {
4444
/**
45-
By default, the modules that were polyfilled in Webpack 4 are mirrored over. However, if you don't want a module like `console` to be polyfilled you can specify alises to be skipped here.
45+
You can choose to add certain aliases to the list of polyfilled modules. For example, you can choose to polyfill `console`.
46+
*/
47+
additionalAliases?: readonly Alias[];
48+
49+
/**
50+
If you don't want a module to be polyfilled, you can specify aliases to be skipped here.
4651
*/
4752
excludeAliases?: readonly Alias[];
4853
};
4954

50-
export type Options = MergeExclusive<IncludeOptions, ExcludeOptions>;
55+
export type Options = MergeExclusive<OnlyAliasesOptions, AdditionalExcludeAliasesOptions>;
5156
}
5257

5358
declare class NodePolyfillPlugin {

‎index.js

+52-11
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,72 @@ function includeKeys(object, predicate) {
2020
return result;
2121
}
2222

23-
// https://github.com/sindresorhus/filter-obj/blob/58086b537bb622166387216bfb7da6e8184996ba/index.js#L27-L34
24-
function excludeKeys(object, keys) {
25-
const set = new Set(keys);
23+
const defaultPolyfills = new Set([
24+
'assert',
25+
'buffer',
26+
'Buffer',
27+
'constants',
28+
'crypto',
29+
'events',
30+
'http',
31+
'https',
32+
'os',
33+
'path',
34+
'querystring',
35+
'stream',
36+
'string_decoder',
37+
'sys',
38+
'timers',
39+
'tty',
40+
'url',
41+
'util',
42+
'vm',
43+
'zlib',
44+
]);
2645

27-
return includeKeys(object, key => !set.has(key));
46+
function createAliasFilter({excludeAliases, onlyAliases, additionalAliases}) {
47+
if (onlyAliases.length > 0) {
48+
return object => includeKeys(object, onlyAliases);
49+
}
50+
51+
if (additionalAliases.length > 0) {
52+
return object => includeKeys(object, key => (defaultPolyfills.has(key) && !excludeAliases.includes(key)) || additionalAliases.includes(key));
53+
}
54+
55+
return object => includeKeys(object, key => defaultPolyfills.has(key) && !excludeAliases.includes(key));
2856
}
2957

30-
function createAliasFilter({includeAliases, excludeAliases}) {
31-
if (includeAliases.length > 0) {
32-
return object => includeKeys(object, includeAliases);
58+
function areItemsUnique(...iterables) {
59+
const seen = new Set();
60+
61+
for (const iterable of iterables) {
62+
for (const item of iterable) {
63+
if (seen.has(item)) {
64+
return false;
65+
}
66+
67+
seen.add(item);
68+
}
3369
}
3470

35-
return object => excludeKeys(object, excludeAliases);
71+
return true;
3672
}
3773

3874
module.exports = class NodePolyfillPlugin {
3975
constructor(options = {}) {
4076
this.options = {
4177
excludeAliases: [],
42-
includeAliases: [],
78+
onlyAliases: [],
79+
additionalAliases: [],
4380
...options,
4481
};
4582

46-
if (this.options.includeAliases.length > 0 && this.options.excludeAliases.length > 0) {
47-
throw new Error('excludeAliases and includeAliases are mutually exclusive');
83+
if (this.options.onlyAliases.length > 0) {
84+
if (this.options.excludeAliases.length > 0 || this.options.additionalAliases.length > 0) {
85+
throw new Error('onlyAliases is mutually exclusive with excludeAliases and additionalAliases');
86+
}
87+
} else if (!areItemsUnique(this.options.excludeAliases, this.options.additionalAliases)) {
88+
throw new Error('excludeAliases and additionalAliases must not include the same items');
4889
}
4990
}
5091

‎readme.md

+39-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,22 @@ const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
2020
module.exports = {
2121
// Other rules...
2222
plugins: [
23-
new NodePolyfillPlugin()
23+
new NodePolyfillPlugin(),
24+
]
25+
};
26+
```
27+
28+
`console`, `process`, and most deprecated/internal Node.js core modules are not polyfilled by default. If you still need to polyfill them, you can use the `additionalAliases` option:
29+
30+
```js
31+
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
32+
33+
module.exports = {
34+
// Other rules...
35+
plugins: [
36+
new NodePolyfillPlugin({
37+
additionalAliases: ['process', 'punycode'],
38+
})
2439
]
2540
};
2641
```
@@ -33,11 +48,28 @@ module.exports = {
3348

3449
Type: `object`
3550

36-
`excludeAliases` and `includeAliases` are mutually exclusive.
51+
`onlyAliases` is mutually exclusive with `excludeAliases` and `additionalAliases`.
3752

3853
#### excludeAliases
3954

40-
By default, the modules that were polyfilled in Webpack 4 are mirrored over. However, if you don't want a module like `console` to be polyfilled you can specify alises to be skipped here.
55+
If you don't want a module to be polyfilled, you can specify aliases to be skipped here.
56+
57+
```js
58+
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
59+
60+
module.exports = {
61+
// Other rules...
62+
plugins: [
63+
new NodePolyfillPlugin({
64+
excludeAliases: ['console'],
65+
})
66+
]
67+
};
68+
```
69+
70+
#### additionalAliases
71+
72+
Alternatively, you can choose to add certain aliases to the list of polyfilled modules. For example, you can choose to polyfill `console`.
4173

4274
```js
4375
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
@@ -46,15 +78,15 @@ module.exports = {
4678
// Other rules...
4779
plugins: [
4880
new NodePolyfillPlugin({
49-
excludeAliases: ['console']
81+
additionalAliases: ['console'],
5082
})
5183
]
5284
};
5385
```
5486

55-
#### includeAliases
87+
#### onlyAliases
5688

57-
Alternatively, you can choose to only include certain aliases. For example, you can only have `console` polyfilled.
89+
You can also choose to only include certain aliases, ignoring the defaults. For example, you can have only `console` polyfilled.
5890

5991
```js
6092
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
@@ -63,7 +95,7 @@ module.exports = {
6395
// Other rules...
6496
plugins: [
6597
new NodePolyfillPlugin({
66-
includeAliases: ['console']
98+
onlyAliases: ['console'],
6799
})
68100
]
69101
};

‎test.js

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('node:fs');
1+
const {promises: fs} = require('node:fs');
22
const test = require('ava');
33
const webpack = require('p-webpack');
44
const NodePolyfillPlugin = require('./index.js');
@@ -14,18 +14,21 @@ test('main', async t => {
1414
},
1515
plugins: [
1616
new NodePolyfillPlugin({
17-
excludeAliases: ['console'],
17+
additionalAliases: ['assert', 'buffer'],
18+
// ExcludeAliases: ['console'],
1819
}),
1920
],
2021
});
2122

2223
t.is(require('./dist/1.js'), 'Hello World');
2324

25+
const output = await fs.readFile('./dist/1.js', {encoding: 'utf8'});
26+
2427
// https://github.com/browserify/console-browserify/blob/f7eefc7c908c29d2e94954e5c6c1098e8c1028b4/index.js#L63
25-
t.false(fs.readFileSync('./dist/1.js').toString().includes('No such label: '));
28+
t.false(output.includes('No such label: '));
2629

27-
// https://github.com/feross/buffer/blob/master/index.js#L80
28-
t.true(fs.readFileSync('./dist/1.js').toString().includes('is invalid for option "size"'));
30+
// https://github.com/feross/buffer/blob/5857e295f4d37e3ad02c3abcbf7e8e5ef51f3be6/index.js#L101
31+
t.true(output.includes('is invalid for option "size"'));
2932
});
3033

3134
test('includeAliases', async t => {
@@ -39,23 +42,26 @@ test('includeAliases', async t => {
3942
},
4043
plugins: [
4144
new NodePolyfillPlugin({
42-
includeAliases: ['console'],
45+
additionalAliases: ['console', 'assert'],
46+
excludeAliases: ['buffer', 'Buffer'],
4347
}),
4448
],
4549
});
4650

4751
t.is(require('./dist/2.js'), 'Hello World');
4852

53+
const output = await fs.readFile('./dist/2.js', {encoding: 'utf8'});
54+
4955
// https://github.com/browserify/console-browserify/blob/f7eefc7c908c29d2e94954e5c6c1098e8c1028b4/index.js#L63
50-
t.true(fs.readFileSync('./dist/2.js').toString().includes('No such label: '));
56+
t.true(output.includes('No such label: '));
5157

5258
// https://github.com/feross/buffer/blob/master/index.js#L80
53-
t.false(fs.readFileSync('./dist/2.js').toString().includes('is invalid for option "size"'));
59+
t.false(output.includes('is invalid for option "size"'));
5460
});
5561

56-
test('includeAliases and excludeAliases used at the same time', t => {
62+
test('onlyAliases and excludeAliases used at the same time', t => {
5763
t.throws(() => new NodePolyfillPlugin({
58-
includeAliases: ['console'],
64+
onlyAliases: ['console'],
5965
excludeAliases: ['crypto'],
6066
}), {instanceOf: Error});
6167
});

0 commit comments

Comments
 (0)
Please sign in to comment.