|
1 | 1 | "use strict" |
2 | 2 | const { ProvidePlugin } = require("webpack") |
| 3 | +const filterObject = require("filter-obj") |
| 4 | + |
| 5 | +const excludeObjectKeys = (object, excludeKeys) => filterObject(object, key => !excludeKeys.includes(key)) |
3 | 6 |
|
4 | 7 | module.exports = class NodePolyfillPlugin { |
| 8 | + constructor(options = {}) { |
| 9 | + this.options = { |
| 10 | + excludeAliases: [], |
| 11 | + ...options |
| 12 | + } |
| 13 | + } |
| 14 | + |
5 | 15 | apply(compiler) { |
6 | | - compiler.options.plugins.push(new ProvidePlugin({ |
| 16 | + compiler.options.plugins.push(new ProvidePlugin(excludeObjectKeys({ |
7 | 17 | Buffer: ["buffer", "Buffer"], |
8 | 18 | console: "console-browserify", |
9 | 19 | process: "process/browser" |
10 | | - })) |
| 20 | + }, this.options.excludeAliases))) |
11 | 21 |
|
12 | 22 | 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), |
43 | 55 | ...compiler.options.resolve.fallback |
44 | 56 | } |
45 | 57 | } |
|
0 commit comments