generated from Richienb/node-module-boilerplate
-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
139 lines (123 loc) · 3.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
// https://github.com/sindresorhus/filter-obj/blob/58086b537bb622166387216bfb7da6e8184996ba/index.js#L1-L25
function includeKeys(object, predicate) {
const result = {};
if (Array.isArray(predicate)) {
for (const key of predicate) {
result[key] = object[key];
}
} else {
for (const key of Object.keys(object)) {
const value = object[key];
if (predicate(key, value, object)) {
result[key] = value;
}
}
}
return result;
}
const defaultPolyfills = new Set([
'assert',
'buffer',
'Buffer',
'constants',
'crypto',
'events',
'fs',
'http',
'https',
'os',
'path',
'querystring',
'stream',
'string_decoder',
'sys',
'timers',
'tty',
'url',
'util',
'vm',
'zlib',
]);
function createAliasFilter({excludeAliases, onlyAliases, additionalAliases}) {
if (onlyAliases.length > 0) {
return object => includeKeys(object, onlyAliases);
}
if (additionalAliases.length > 0) {
return object => includeKeys(object, key => (defaultPolyfills.has(key) && !excludeAliases.includes(key)) || additionalAliases.includes(key));
}
return object => includeKeys(object, key => defaultPolyfills.has(key) && !excludeAliases.includes(key));
}
function areItemsUnique(...iterables) {
const seen = new Set();
for (const iterable of iterables) {
for (const item of iterable) {
if (seen.has(item)) {
return false;
}
seen.add(item);
}
}
return true;
}
module.exports = class NodePolyfillPlugin {
constructor(options = {}) {
this.options = {
excludeAliases: [],
onlyAliases: [],
additionalAliases: [],
...options,
};
if (this.options.onlyAliases.length > 0) {
if (this.options.excludeAliases.length > 0 || this.options.additionalAliases.length > 0) {
throw new Error('onlyAliases is mutually exclusive with excludeAliases and additionalAliases');
}
} else if (!areItemsUnique(this.options.excludeAliases, this.options.additionalAliases)) {
throw new Error('excludeAliases and additionalAliases must not include the same items');
}
}
apply(compiler) {
const filter = createAliasFilter(this.options);
compiler.options.plugins.push(new compiler.webpack.ProvidePlugin(filter({
Buffer: [require.resolve('buffer/'), 'Buffer'],
console: require.resolve('console-browserify'),
process: require.resolve('process/browser'),
})));
compiler.options.resolve.fallback = {
...filter({
assert: require.resolve('assert/'),
buffer: require.resolve('buffer/'),
console: require.resolve('console-browserify'),
constants: require.resolve('constants-browserify'),
crypto: require.resolve('crypto-browserify'),
domain: require.resolve('domain-browser'),
events: require.resolve('events/'),
fs: false,
http: require.resolve('stream-http'),
https: require.resolve('https-browserify'),
os: require.resolve('os-browserify/browser'),
path: require.resolve('path-browserify'),
punycode: require.resolve('punycode/'),
process: require.resolve('process/browser'),
querystring: require.resolve('querystring-es3'),
stream: require.resolve('stream-browserify'),
/* eslint-disable camelcase */
_stream_duplex: require.resolve('readable-stream/lib/_stream_duplex'),
_stream_passthrough: require.resolve('readable-stream/lib/_stream_passthrough'),
_stream_readable: require.resolve('readable-stream/lib/_stream_readable'),
_stream_transform: require.resolve('readable-stream/lib/_stream_transform'),
_stream_writable: require.resolve('readable-stream/lib/_stream_writable'),
string_decoder: require.resolve('string_decoder/'),
/* eslint-enable camelcase */
sys: require.resolve('util/'),
timers: require.resolve('timers-browserify'),
tty: require.resolve('tty-browserify'),
url: require.resolve('url/'),
util: require.resolve('util/'),
vm: require.resolve('vm-browserify'),
zlib: require.resolve('browserify-zlib'),
}),
...compiler.options.resolve.fallback,
};
}
};