Skip to content

Commit 32a61a6

Browse files
Edge00赵旭阳Richienb
authored
Allow aliases to be skipped (#6)
Co-authored-by: 赵旭阳 <qiongen@xiaohongshu.com> Co-authored-by: Richie Bendall <richiebendall@gmail.com>
1 parent a59de39 commit 32a61a6

File tree

6 files changed

+163
-33
lines changed

6 files changed

+163
-33
lines changed

fixture.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ const assert = require("assert")
22

33
assert(true)
44

5+
console.clear()
6+
57
module.exports = Buffer.from("Hello World").toString()

index.d.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,49 @@
11
import { Compiler } from "webpack"
22

3+
declare namespace NodePolyfillPlugin {
4+
export interface Options {
5+
/**
6+
Aliases to skip adding. Useful if you don't want a module like `console` to be polyfilled.
7+
*/
8+
excludeAliases?: readonly Array<
9+
| "Buffer"
10+
| "console"
11+
| "process"
12+
| "assert"
13+
| "buffer"
14+
| "console"
15+
| "constants"
16+
| "crypto"
17+
| "domain"
18+
| "events"
19+
| "http"
20+
| "https"
21+
| "os"
22+
| "path"
23+
| "punycode"
24+
| "process"
25+
| "querystring"
26+
| "stream"
27+
| "_stream_duplex"
28+
| "_stream_passthrough"
29+
| "_stream_readable"
30+
| "_stream_transform"
31+
| "_stream_writable"
32+
| "string_decoder"
33+
| "sys"
34+
| "timers"
35+
| "tty"
36+
| "url"
37+
| "util"
38+
| "vm"
39+
| "zlib"
40+
>
41+
}
42+
}
43+
344
declare class NodePolyfillPlugin {
45+
constructor(options: NodePolyfillPlugin.Options)
46+
447
apply(compiler: InstanceType<typeof Compiler>): void
548
}
649

index.js

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,57 @@
11
"use strict"
22
const { ProvidePlugin } = require("webpack")
3+
const filterObject = require("filter-obj")
4+
5+
const excludeObjectKeys = (object, excludeKeys) => filterObject(object, key => !excludeKeys.includes(key))
36

47
module.exports = class NodePolyfillPlugin {
8+
constructor(options = {}) {
9+
this.options = {
10+
excludeAliases: [],
11+
...options
12+
}
13+
}
14+
515
apply(compiler) {
6-
compiler.options.plugins.push(new ProvidePlugin({
16+
compiler.options.plugins.push(new ProvidePlugin(excludeObjectKeys({
717
Buffer: ["buffer", "Buffer"],
818
console: "console-browserify",
919
process: "process/browser"
10-
}))
20+
}, this.options.excludeAliases)))
1121

1222
compiler.options.resolve.fallback = {
13-
assert: "assert",
14-
buffer: "buffer",
15-
console: "console-browserify",
16-
constants: "constants-browserify",
17-
crypto: "crypto-browserify",
18-
domain: "domain-browser",
19-
events: "events",
20-
http: "stream-http",
21-
https: "https-browserify",
22-
os: "os-browserify/browser",
23-
path: "path-browserify",
24-
punycode: "punycode",
25-
process: "process/browser",
26-
querystring: "querystring-es3",
27-
stream: "stream-browserify",
28-
/* eslint-disable camelcase */
29-
_stream_duplex: "readable-stream/duplex",
30-
_stream_passthrough: "readable-stream/passthrough",
31-
_stream_readable: "readable-stream/readable",
32-
_stream_transform: "readable-stream/transform",
33-
_stream_writable: "readable-stream/writable",
34-
string_decoder: "string_decoder",
35-
/* eslint-enable camelcase */
36-
sys: "util",
37-
timers: "timers-browserify",
38-
tty: "tty-browserify",
39-
url: "url",
40-
util: "util",
41-
vm: "vm-browserify",
42-
zlib: "browserify-zlib",
23+
...excludeObjectKeys({
24+
assert: "assert",
25+
buffer: "buffer",
26+
console: "console-browserify",
27+
constants: "constants-browserify",
28+
crypto: "crypto-browserify",
29+
domain: "domain-browser",
30+
events: "events",
31+
http: "stream-http",
32+
https: "https-browserify",
33+
os: "os-browserify/browser",
34+
path: "path-browserify",
35+
punycode: "punycode",
36+
process: "process/browser",
37+
querystring: "querystring-es3",
38+
stream: "stream-browserify",
39+
/* eslint-disable camelcase */
40+
_stream_duplex: "readable-stream/duplex",
41+
_stream_passthrough: "readable-stream/passthrough",
42+
_stream_readable: "readable-stream/readable",
43+
_stream_transform: "readable-stream/transform",
44+
_stream_writable: "readable-stream/writable",
45+
string_decoder: "string_decoder",
46+
/* eslint-enable camelcase */
47+
sys: "util",
48+
timers: "timers-browserify",
49+
tty: "tty-browserify",
50+
url: "url",
51+
util: "util",
52+
vm: "vm-browserify",
53+
zlib: "browserify-zlib"
54+
}, this.options.excludeAliases),
4355
...compiler.options.resolve.fallback
4456
}
4557
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"crypto-browserify": "^3.12.0",
3333
"domain-browser": "^4.19.0",
3434
"events": "^3.2.0",
35+
"filter-obj": "^2.0.1",
3536
"https-browserify": "^1.0.0",
3637
"os-browserify": "^0.3.0",
3738
"path-browserify": "^1.0.1",

readme.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,67 @@ module.exports = {
2626
]
2727
}
2828
```
29+
30+
## API
31+
32+
### new NodePolyfillPlugin(options?)
33+
34+
#### options
35+
36+
Type: `object`
37+
38+
#### excludeAliases
39+
40+
Aliases to skip adding. Useful if you don't want a module like `console` to be polyfilled.
41+
42+
```js
43+
const NodePolyfillPlugin = require("node-polyfill-webpack-plugin")
44+
45+
module.exports = {
46+
// Other rules...
47+
plugins: [
48+
new NodePolyfillPlugin({
49+
excludeAliases: ["console"]
50+
})
51+
]
52+
}
53+
```
54+
55+
## Aliased
56+
57+
### Globals
58+
59+
- `Buffer`
60+
- `console`
61+
- `process`
62+
63+
### Modules
64+
65+
- `assert`
66+
- `buffer`
67+
- `console`
68+
- `constants`
69+
- `crypto`
70+
- `domain`
71+
- `events`
72+
- `http`
73+
- `https`
74+
- `os`
75+
- `path`
76+
- `punycode`
77+
- `process`
78+
- `querystring`
79+
- `stream`
80+
- `_stream_duplex`
81+
- `_stream_passthrough`
82+
- `_stream_readable`
83+
- `_stream_transform`
84+
- `_stream_writable`
85+
- `string_decoder`
86+
- `sys`
87+
- `timers`
88+
- `tty`
89+
- `url`
90+
- `util`
91+
- `vm`
92+
- `zlib`

test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const fs = require("fs")
12
const test = require("ava")
23
const webpack = require("p-webpack")
34
const NodePolyfillPlugin = require(".")
@@ -11,9 +12,16 @@ test("main", async t => {
1112
}
1213
},
1314
plugins: [
14-
new NodePolyfillPlugin()
15+
new NodePolyfillPlugin({
16+
excludeAliases: ["console"]
17+
})
1518
]
1619
})
1720

21+
const result = fs.readFileSync("./dist/main.js").toString()
22+
1823
t.is(require("./dist/main"), "Hello World")
24+
25+
// https://github.com/browserify/console-browserify/blob/f7eefc7c908c29d2e94954e5c6c1098e8c1028b4/index.js#L63
26+
t.false(result.includes("No such label: "))
1927
})

0 commit comments

Comments
 (0)