edge-functions 2.2.0-stage.1 → 2.2.0-stage.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +1 -1
- package/jest.config.e2e.js +8 -0
- package/jest.config.unit.js +7 -0
- package/lib/build/bundlers/bundler-base.js +102 -0
- package/lib/build/bundlers/esbuild/esbuild.config.js +2 -0
- package/lib/build/bundlers/esbuild/index.js +46 -33
- package/lib/build/bundlers/esbuild/index.test.js +90 -0
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/index.js +92 -131
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/index.test.js +38 -0
- package/lib/build/bundlers/polyfills/node/_empty.js +0 -0
- package/lib/build/bundlers/polyfills/node/crypto.js +70 -0
- package/lib/build/bundlers/polyfills/node/globals/buffer.js +4 -0
- package/lib/build/bundlers/polyfills/node/globals/path-dirname.js +6 -0
- package/lib/build/{polyfills → bundlers/polyfills}/node/globals/process.js +76 -24
- package/lib/build/bundlers/polyfills/polyfills-manager.js +138 -0
- package/lib/build/bundlers/{esbuild/plugins/node-polyfills/node-pollyfills-paths.test.js → polyfills/polyfills-manager.test.js} +34 -21
- package/lib/build/bundlers/webpack/index.js +44 -28
- package/lib/build/bundlers/webpack/index.test.js +101 -0
- package/lib/build/bundlers/webpack/plugins/node-polyfills/index.js +65 -0
- package/lib/build/bundlers/webpack/plugins/node-polyfills/index.test.js +55 -0
- package/lib/build/bundlers/webpack/webpack.config.js +31 -2
- package/lib/build/dispatcher/dispatcher.js +203 -118
- package/lib/build/dispatcher/dispatcher.test.js +0 -1
- package/lib/constants/messages/build.messages.js +4 -0
- package/lib/presets/custom/next/compute/config.js +2 -87
- package/lib/presets/custom/next/compute/node/custom-server/12.3.1/server/require.js +3 -0
- package/package.json +12 -5
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/node-polyfills-paths.js +0 -122
- /package/lib/build/{polyfills → bundlers/polyfills}/node/dns.js +0 -0
- /package/lib/build/{polyfills → bundlers/polyfills}/node/fs.js +0 -0
- /package/lib/build/{polyfills → bundlers/polyfills}/node/globals/navigator.js +0 -0
- /package/lib/build/{polyfills → bundlers/polyfills}/node/globals/performance.js +0 -0
- /package/lib/build/{polyfills → bundlers/polyfills}/node/http2.js +0 -0
- /package/lib/build/{polyfills → bundlers/polyfills}/node/module.js +0 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
import crypto from 'crypto-browserify';
|
|
3
|
+
|
|
4
|
+
crypto.webcrypto = globalThis.crypto;
|
|
5
|
+
export default crypto;
|
|
6
|
+
export var { Cipher } = crypto;
|
|
7
|
+
export var { Cipheriv } = crypto;
|
|
8
|
+
export var { Decipher } = crypto;
|
|
9
|
+
export var { Decipheriv } = crypto;
|
|
10
|
+
export var { DiffieHellman } = crypto;
|
|
11
|
+
export var { DiffieHellmanGroup } = crypto;
|
|
12
|
+
export var { Hash } = crypto;
|
|
13
|
+
export var { Hmac } = crypto;
|
|
14
|
+
export var { Sign } = crypto;
|
|
15
|
+
export var { Verify } = crypto;
|
|
16
|
+
export var { constants } = crypto;
|
|
17
|
+
export var { createCipher } = crypto;
|
|
18
|
+
export var { createCipheriv } = crypto;
|
|
19
|
+
export var { createCredentials } = crypto;
|
|
20
|
+
export var { createDecipher } = crypto;
|
|
21
|
+
export var { createDecipheriv } = crypto;
|
|
22
|
+
export var { createDiffieHellman } = crypto;
|
|
23
|
+
export var { createDiffieHellmanGroup } = crypto;
|
|
24
|
+
export var { createECDH } = crypto;
|
|
25
|
+
export var { createHash } = crypto;
|
|
26
|
+
export var { createHmac } = crypto;
|
|
27
|
+
export var { createSign } = crypto;
|
|
28
|
+
export var { createVerify } = crypto;
|
|
29
|
+
export var { getCiphers } = crypto;
|
|
30
|
+
export var { getDiffieHellman } = crypto;
|
|
31
|
+
export var { getHashes } = crypto;
|
|
32
|
+
export var { listCiphers } = crypto;
|
|
33
|
+
export var { pbkdf2 } = crypto;
|
|
34
|
+
export var { pbkdf2Sync } = crypto;
|
|
35
|
+
export var { privateDecrypt } = crypto;
|
|
36
|
+
export var { privateEncrypt } = crypto;
|
|
37
|
+
export var { prng } = crypto;
|
|
38
|
+
export var { pseudoRandomBytes } = crypto;
|
|
39
|
+
export var { publicDecrypt } = crypto;
|
|
40
|
+
export var { publicEncrypt } = crypto;
|
|
41
|
+
export var { randomBytes } = crypto;
|
|
42
|
+
export var { randomFill } = crypto;
|
|
43
|
+
export var { randomFillSync } = crypto;
|
|
44
|
+
export var { rng } = crypto;
|
|
45
|
+
export var { webcrypto } = crypto;
|
|
46
|
+
|
|
47
|
+
export var getRandomValues = function (abv) {
|
|
48
|
+
let l = abv.length;
|
|
49
|
+
while (l--) {
|
|
50
|
+
const bytes = randomBytes(7);
|
|
51
|
+
let randomFloat = (bytes[0] % 32) / 32;
|
|
52
|
+
|
|
53
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
54
|
+
const byte = bytes[i];
|
|
55
|
+
randomFloat = (randomFloat + byte) / 256;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
abv[l] = Math.floor(randomFloat * 256);
|
|
59
|
+
}
|
|
60
|
+
return abv;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export var randomUUID = function () {
|
|
64
|
+
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, function (c) {
|
|
65
|
+
return (
|
|
66
|
+
c ^
|
|
67
|
+
(getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
|
|
68
|
+
).toString(16);
|
|
69
|
+
});
|
|
70
|
+
};
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
// shim for using process in browser
|
|
16
|
-
|
|
16
|
+
// globalThis.process = process;
|
|
17
17
|
|
|
18
18
|
// cached from whatever global is present so that test runners that stub it
|
|
19
19
|
// don't break things. But we need to wrap it in a try catch in case it is
|
|
@@ -172,7 +172,7 @@ function cleanUpNextTick() {
|
|
|
172
172
|
*
|
|
173
173
|
*/
|
|
174
174
|
|
|
175
|
-
|
|
175
|
+
const nextTick = function (fun) {
|
|
176
176
|
const args = new Array(arguments.length - 1);
|
|
177
177
|
if (arguments.length > 1) {
|
|
178
178
|
for (let i = 1; i < arguments.length; i++) {
|
|
@@ -197,42 +197,94 @@ function Item(fun, array) {
|
|
|
197
197
|
Item.prototype.run = function () {
|
|
198
198
|
this.fun.apply(null, this.array);
|
|
199
199
|
};
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
200
|
+
const title = 'browser';
|
|
201
|
+
const browser = true;
|
|
202
|
+
const env = {};
|
|
203
|
+
const argv = [];
|
|
204
|
+
const version = ''; // empty string to avoid regexp issues
|
|
205
|
+
const versions = { node: '18.3.1' }; // fake version
|
|
206
206
|
|
|
207
207
|
/**
|
|
208
208
|
*
|
|
209
209
|
*/
|
|
210
210
|
function noop() {}
|
|
211
211
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
212
|
+
const on = noop;
|
|
213
|
+
const addListener = noop;
|
|
214
|
+
const once = noop;
|
|
215
|
+
const off = noop;
|
|
216
|
+
const removeListener = noop;
|
|
217
|
+
const removeAllListeners = noop;
|
|
218
|
+
const emit = noop;
|
|
219
|
+
const prependListener = noop;
|
|
220
|
+
const prependOnceListener = noop;
|
|
221
|
+
|
|
222
|
+
const listeners = function () {
|
|
223
223
|
return [];
|
|
224
224
|
};
|
|
225
225
|
|
|
226
|
-
|
|
227
|
-
throw new Error('
|
|
226
|
+
const binding = function () {
|
|
227
|
+
throw new Error('const binding is not supported');
|
|
228
228
|
};
|
|
229
229
|
|
|
230
|
-
|
|
230
|
+
const cwd = function () {
|
|
231
231
|
return '/';
|
|
232
232
|
};
|
|
233
|
-
|
|
233
|
+
const chdir = function () {
|
|
234
234
|
throw new Error('process.chdir is not supported');
|
|
235
235
|
};
|
|
236
|
-
|
|
236
|
+
const umask = function () {
|
|
237
237
|
return 0;
|
|
238
238
|
};
|
|
239
|
+
|
|
240
|
+
const process = {
|
|
241
|
+
nextTick,
|
|
242
|
+
title,
|
|
243
|
+
browser,
|
|
244
|
+
env,
|
|
245
|
+
argv,
|
|
246
|
+
version,
|
|
247
|
+
versions,
|
|
248
|
+
on,
|
|
249
|
+
addListener,
|
|
250
|
+
once,
|
|
251
|
+
off,
|
|
252
|
+
removeListener,
|
|
253
|
+
removeAllListeners,
|
|
254
|
+
emit,
|
|
255
|
+
prependListener,
|
|
256
|
+
prependOnceListener,
|
|
257
|
+
listeners,
|
|
258
|
+
binding,
|
|
259
|
+
cwd,
|
|
260
|
+
chdir,
|
|
261
|
+
umask,
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
export default process;
|
|
265
|
+
|
|
266
|
+
export {
|
|
267
|
+
nextTick,
|
|
268
|
+
title,
|
|
269
|
+
browser,
|
|
270
|
+
env,
|
|
271
|
+
argv,
|
|
272
|
+
version,
|
|
273
|
+
versions,
|
|
274
|
+
on,
|
|
275
|
+
addListener,
|
|
276
|
+
once,
|
|
277
|
+
off,
|
|
278
|
+
removeListener,
|
|
279
|
+
removeAllListeners,
|
|
280
|
+
emit,
|
|
281
|
+
prependListener,
|
|
282
|
+
prependOnceListener,
|
|
283
|
+
listeners,
|
|
284
|
+
binding,
|
|
285
|
+
cwd,
|
|
286
|
+
chdir,
|
|
287
|
+
umask,
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
globalThis.process = process;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
import { getAbsoluteLibDirPath } from '#utils';
|
|
3
|
+
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
5
|
+
const libDirPath = getAbsoluteLibDirPath();
|
|
6
|
+
const polyfillsPath = `${libDirPath}/build/bundlers/polyfills`;
|
|
7
|
+
const nextNodePresetPath = `${libDirPath}/presets/custom/next/compute/node`;
|
|
8
|
+
const nodePolyfillsPath = `${polyfillsPath}/node`;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Manages and builds polyfills for Node and global browser environments.
|
|
12
|
+
*/
|
|
13
|
+
class PolyfillsManager {
|
|
14
|
+
/**
|
|
15
|
+
* Constructs a PolyfillsManager instance.
|
|
16
|
+
*/
|
|
17
|
+
constructor() {
|
|
18
|
+
/** @type {Map<string, string>} */
|
|
19
|
+
this.globals = new Map();
|
|
20
|
+
/** @type {Map<string, string|boolean>} */
|
|
21
|
+
this.libs = new Map();
|
|
22
|
+
/** @type {Map<string, string|boolean>} */
|
|
23
|
+
this.alias = new Map();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Sets a global polyfill.
|
|
28
|
+
* @param {string} name - Name of the global.
|
|
29
|
+
* @param {string} path - Path to the polyfill.
|
|
30
|
+
*/
|
|
31
|
+
setGlobal(name, path) {
|
|
32
|
+
this.globals.set(name, path);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Sets a module/library polyfill.
|
|
37
|
+
* @param {string} name - Name of the module/library.
|
|
38
|
+
* @param {string|boolean} path - Path to the polyfill or a boolean value.
|
|
39
|
+
*/
|
|
40
|
+
setLib(name, path) {
|
|
41
|
+
this.libs.set(name, path);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Sets a module/alias polyfill.
|
|
46
|
+
* @param {string} name - Name of the module/alias.
|
|
47
|
+
* @param {string|boolean} path - Path to the polyfill or a boolean value.
|
|
48
|
+
*/
|
|
49
|
+
setAlias(name, path) {
|
|
50
|
+
this.alias.set(name, path);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Builds and retrieves the polyfills for Node and globals.
|
|
55
|
+
* @returns {{ libs: Map<string, string|boolean>, globals: Map<string, string>, alias: Map<string, string> }} - Object containing libs and globals.
|
|
56
|
+
*/
|
|
57
|
+
buildPolyfills() {
|
|
58
|
+
this.setGlobal('buffer', `${nodePolyfillsPath}/globals/buffer.js`);
|
|
59
|
+
this.setGlobal('console', require.resolve('console-browserify'));
|
|
60
|
+
this.setGlobal('navigator', `${nodePolyfillsPath}/globals/navigator.js`);
|
|
61
|
+
this.setGlobal(
|
|
62
|
+
'performance',
|
|
63
|
+
`${nodePolyfillsPath}/globals/performance.js`,
|
|
64
|
+
);
|
|
65
|
+
this.setGlobal('process', `${nodePolyfillsPath}/globals/process.js`);
|
|
66
|
+
this.setGlobal('__dirname', `${nodePolyfillsPath}/globals/path-dirname.js`);
|
|
67
|
+
this.setGlobal(
|
|
68
|
+
'__filename',
|
|
69
|
+
`${nodePolyfillsPath}/globals/path-dirname.js`,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
this.setLib('accepts', require.resolve('accepts'));
|
|
73
|
+
this.setLib('async_hooks', `${nodePolyfillsPath}/_empty.js`);
|
|
74
|
+
this.setLib('buffer', require.resolve('buffer/'));
|
|
75
|
+
this.setLib('child_process', `${nodePolyfillsPath}/_empty.js`);
|
|
76
|
+
this.setLib('cluster', `${nodePolyfillsPath}/_empty.js`);
|
|
77
|
+
this.setLib('console', require.resolve('console-browserify'));
|
|
78
|
+
this.setLib('crypto', `${nodePolyfillsPath}/crypto.js`);
|
|
79
|
+
this.setLib('dgram', `${nodePolyfillsPath}/_empty.js`);
|
|
80
|
+
this.setLib('dns', `${nodePolyfillsPath}/dns.js`);
|
|
81
|
+
this.setLib('events', require.resolve('events/'));
|
|
82
|
+
this.setLib('fs', `${nodePolyfillsPath}/fs.js`);
|
|
83
|
+
this.setLib('http', require.resolve('stream-http'));
|
|
84
|
+
this.setLib('http2', `${nodePolyfillsPath}/http2.js`);
|
|
85
|
+
this.setLib('https', require.resolve('stream-http'));
|
|
86
|
+
this.setLib('inspector', `${nodePolyfillsPath}/_empty.js`);
|
|
87
|
+
this.setLib('module', `${nodePolyfillsPath}/module.js`);
|
|
88
|
+
this.setLib('net', `${nodePolyfillsPath}/_empty.js`);
|
|
89
|
+
this.setLib('os', require.resolve('os-browserify/browser'));
|
|
90
|
+
this.setLib('path', require.resolve('path-browserify'));
|
|
91
|
+
this.setLib('perf_hooks', `${nodePolyfillsPath}/_empty.js`);
|
|
92
|
+
this.setLib('process', `${nodePolyfillsPath}/globals/process.js`);
|
|
93
|
+
this.setLib('querystring', require.resolve('querystring-es3/'));
|
|
94
|
+
this.setLib('readline', `${nodePolyfillsPath}/_empty.js`);
|
|
95
|
+
this.setLib('repl', `${nodePolyfillsPath}/_empty.js`);
|
|
96
|
+
this.setLib('stream', require.resolve('stream-browserify'));
|
|
97
|
+
this.setLib(
|
|
98
|
+
'_stream_passthrough',
|
|
99
|
+
require.resolve('readable-stream/lib/_stream_passthrough.js'),
|
|
100
|
+
);
|
|
101
|
+
this.setLib(
|
|
102
|
+
'_stream_readable',
|
|
103
|
+
require.resolve('readable-stream/lib/stream.js'),
|
|
104
|
+
);
|
|
105
|
+
this.setLib(
|
|
106
|
+
'_stream_transform',
|
|
107
|
+
require.resolve('readable-stream/lib/_stream_transform.js'),
|
|
108
|
+
);
|
|
109
|
+
this.setLib(
|
|
110
|
+
'_stream_writable',
|
|
111
|
+
require.resolve('readable-stream/lib/_stream_writable.js'),
|
|
112
|
+
);
|
|
113
|
+
this.setLib('string_decoder', require.resolve('string_decoder/'));
|
|
114
|
+
this.setLib('sys', require.resolve('util/util.js'));
|
|
115
|
+
this.setLib('timers', require.resolve('timers-browserify'));
|
|
116
|
+
this.setLib('tls', `${nodePolyfillsPath}/_empty.js`);
|
|
117
|
+
this.setLib('tty', require.resolve('tty-browserify'));
|
|
118
|
+
this.setLib('url', require.resolve('url/'));
|
|
119
|
+
this.setLib('util', require.resolve('util/'));
|
|
120
|
+
this.setLib('vm', require.resolve('vm-browserify'));
|
|
121
|
+
this.setLib('zlib', require.resolve('browserify-zlib'));
|
|
122
|
+
this.setLib(
|
|
123
|
+
'next/dist/compiled/etag',
|
|
124
|
+
`${nextNodePresetPath}/custom-server/12.3.1/util/etag.js`,
|
|
125
|
+
);
|
|
126
|
+
this.setLib(
|
|
127
|
+
'@fastly/http-compute-js',
|
|
128
|
+
require.resolve('@fastly/http-compute-js'),
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
this.setAlias('util', require.resolve('util/'));
|
|
132
|
+
this.setAlias('process', `${nodePolyfillsPath}/globals/process.js`);
|
|
133
|
+
|
|
134
|
+
return { libs: this.libs, globals: this.globals, alias: this.alias };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export default new PolyfillsManager();
|
|
@@ -1,40 +1,53 @@
|
|
|
1
1
|
import { expect, it } from '@jest/globals';
|
|
2
|
-
import
|
|
2
|
+
import PolyfillsManager from './polyfills-manager.js';
|
|
3
3
|
|
|
4
|
-
describe('
|
|
4
|
+
describe('Polyfills Manager', () => {
|
|
5
5
|
it('Should return map of polyfills', () => {
|
|
6
6
|
const expectedPolyfills = [
|
|
7
|
-
'
|
|
7
|
+
'accepts',
|
|
8
|
+
'async_hooks',
|
|
8
9
|
'buffer',
|
|
9
|
-
'
|
|
10
|
-
'
|
|
10
|
+
'child_process',
|
|
11
|
+
'cluster',
|
|
12
|
+
'console',
|
|
13
|
+
'crypto',
|
|
14
|
+
'dgram',
|
|
15
|
+
'dns',
|
|
11
16
|
'events',
|
|
12
|
-
'
|
|
13
|
-
'path',
|
|
14
|
-
'querystring',
|
|
15
|
-
'punycode',
|
|
16
|
-
'url',
|
|
17
|
-
'string_decoder',
|
|
17
|
+
'fs',
|
|
18
18
|
'http',
|
|
19
|
+
'http2',
|
|
19
20
|
'https',
|
|
21
|
+
'inspector',
|
|
22
|
+
'module',
|
|
23
|
+
'net',
|
|
20
24
|
'os',
|
|
21
|
-
'
|
|
22
|
-
'
|
|
23
|
-
'
|
|
25
|
+
'path',
|
|
26
|
+
'perf_hooks',
|
|
27
|
+
'process',
|
|
28
|
+
'querystring',
|
|
29
|
+
'readline',
|
|
30
|
+
'repl',
|
|
31
|
+
'stream',
|
|
24
32
|
'_stream_passthrough',
|
|
25
33
|
'_stream_readable',
|
|
26
|
-
'_stream_writable',
|
|
27
34
|
'_stream_transform',
|
|
28
|
-
'
|
|
29
|
-
'
|
|
35
|
+
'_stream_writable',
|
|
36
|
+
'string_decoder',
|
|
37
|
+
'sys',
|
|
38
|
+
'timers',
|
|
39
|
+
'tls',
|
|
30
40
|
'tty',
|
|
31
|
-
'
|
|
41
|
+
'url',
|
|
42
|
+
'util',
|
|
43
|
+
'vm',
|
|
44
|
+
'zlib',
|
|
45
|
+
'next/dist/compiled/etag',
|
|
46
|
+
'@fastly/http-compute-js',
|
|
32
47
|
];
|
|
33
|
-
const listOfAvailablePolyfills = builtinsPolyfills();
|
|
34
48
|
const arraylistOfAvailablePolyfills = Array.from(
|
|
35
|
-
|
|
49
|
+
PolyfillsManager.buildPolyfills().libs.keys(),
|
|
36
50
|
);
|
|
37
|
-
|
|
38
51
|
expect(arraylistOfAvailablePolyfills).toEqual(expectedPolyfills);
|
|
39
52
|
});
|
|
40
53
|
});
|
|
@@ -1,23 +1,25 @@
|
|
|
1
|
+
/* eslint-disable no-param-reassign */
|
|
1
2
|
import { promisify } from 'util';
|
|
2
3
|
import webpack from 'webpack';
|
|
3
|
-
import
|
|
4
|
+
import lodash from 'lodash';
|
|
4
5
|
|
|
5
6
|
import { feedback, debug } from '#utils';
|
|
6
7
|
import { Messages } from '#constants';
|
|
7
8
|
|
|
8
9
|
import AzionWebpackConfig from './webpack.config.js';
|
|
10
|
+
import NodePolyfillPlugin from './plugins/node-polyfills/index.js';
|
|
11
|
+
import BundlerBase from '../bundler-base.js';
|
|
9
12
|
|
|
10
|
-
class Webpack {
|
|
13
|
+
class Webpack extends BundlerBase {
|
|
14
|
+
// eslint-disable-next-line no-useless-constructor
|
|
11
15
|
constructor(builderConfig) {
|
|
12
|
-
|
|
13
|
-
this.customConfigPreset = builderConfig.custom;
|
|
14
|
-
this.customConfigLocal = builderConfig.localCustom;
|
|
16
|
+
super(builderConfig);
|
|
15
17
|
}
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
async run() {
|
|
18
20
|
const runWebpack = promisify(webpack);
|
|
19
21
|
|
|
20
|
-
let config = AzionWebpackConfig;
|
|
22
|
+
let config = lodash.cloneDeep(AzionWebpackConfig);
|
|
21
23
|
config.entry = this.builderConfig.entry;
|
|
22
24
|
config.plugins = [
|
|
23
25
|
new webpack.DefinePlugin({
|
|
@@ -26,33 +28,49 @@ class Webpack {
|
|
|
26
28
|
...config.plugins,
|
|
27
29
|
];
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
config = super.mergeConfig(config);
|
|
32
|
+
config = this.applyConfig(config);
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
const stats = await runWebpack(config);
|
|
36
|
+
|
|
37
|
+
const info = stats.toJson();
|
|
38
|
+
if (stats.hasErrors()) {
|
|
39
|
+
info.errors.forEach((msg) => {
|
|
40
|
+
feedback.build.error(msg);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
} catch (error) {
|
|
44
|
+
debug.error(error);
|
|
45
|
+
throw Error(Messages.build.error.vulcan_build_failed);
|
|
32
46
|
}
|
|
47
|
+
}
|
|
33
48
|
|
|
49
|
+
applyConfig(config) {
|
|
50
|
+
const updatedConfig = { ...config };
|
|
34
51
|
// inject content in worker initial code.
|
|
35
52
|
if (this.builderConfig.contentToInject) {
|
|
36
53
|
const workerInitContent = this.builderConfig.contentToInject;
|
|
37
54
|
|
|
38
|
-
const bannerPluginIndex =
|
|
55
|
+
const bannerPluginIndex = updatedConfig.plugins.findIndex(
|
|
39
56
|
(plugin) => plugin instanceof webpack.BannerPlugin,
|
|
40
57
|
);
|
|
41
58
|
|
|
42
59
|
if (bannerPluginIndex !== -1) {
|
|
43
|
-
const oldContent =
|
|
44
|
-
|
|
45
|
-
|
|
60
|
+
const oldContent =
|
|
61
|
+
updatedConfig.plugins[bannerPluginIndex].options.banner;
|
|
62
|
+
const pluginToRemove = updatedConfig.plugins[bannerPluginIndex];
|
|
63
|
+
updatedConfig.plugins = updatedConfig.plugins.filter(
|
|
46
64
|
(plugin) => plugin !== pluginToRemove,
|
|
47
65
|
);
|
|
48
|
-
|
|
66
|
+
updatedConfig.plugins.push(
|
|
49
67
|
new webpack.BannerPlugin({
|
|
50
68
|
banner: `${oldContent} ${workerInitContent}`,
|
|
51
69
|
raw: true,
|
|
52
70
|
}),
|
|
53
71
|
);
|
|
54
72
|
} else {
|
|
55
|
-
|
|
73
|
+
updatedConfig.plugins.push(
|
|
56
74
|
new webpack.BannerPlugin({
|
|
57
75
|
banner: workerInitContent,
|
|
58
76
|
raw: true,
|
|
@@ -61,20 +79,18 @@ class Webpack {
|
|
|
61
79
|
}
|
|
62
80
|
}
|
|
63
81
|
|
|
64
|
-
|
|
65
|
-
|
|
82
|
+
// use polyfill with useNodePolyfills and preset mode compute
|
|
83
|
+
const useNodePolyfills =
|
|
84
|
+
(this.builderConfig?.useNodePolyfills ||
|
|
85
|
+
this.customConfigPreset?.useNodePolyfills ||
|
|
86
|
+
this.customConfigLocal?.useNodePolyfills) &&
|
|
87
|
+
this.presetMode === 'compute';
|
|
66
88
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
info.errors.forEach((msg) => {
|
|
70
|
-
feedback.build.error(msg);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
} catch (error) {
|
|
74
|
-
debug.error(error);
|
|
75
|
-
throw Error(Messages.build.error.vulcan_build_failed);
|
|
89
|
+
if (useNodePolyfills) {
|
|
90
|
+
updatedConfig.plugins.push(new NodePolyfillPlugin());
|
|
76
91
|
}
|
|
77
|
-
|
|
92
|
+
return updatedConfig;
|
|
93
|
+
}
|
|
78
94
|
}
|
|
79
95
|
|
|
80
96
|
export default Webpack;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import tmp from 'tmp';
|
|
3
|
+
import Webpack from './index.js';
|
|
4
|
+
import AzionWebpackConfig from './webpack.config.js';
|
|
5
|
+
|
|
6
|
+
// IN MILESECONDS
|
|
7
|
+
const TIMEOUT = 20000;
|
|
8
|
+
|
|
9
|
+
describe('Webpack Bundler', () => {
|
|
10
|
+
let tmpDir;
|
|
11
|
+
let tmpEntry;
|
|
12
|
+
let tmpOutput;
|
|
13
|
+
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
tmpDir = tmp.dirSync();
|
|
16
|
+
tmpEntry = tmp.fileSync({
|
|
17
|
+
postfix: '.js',
|
|
18
|
+
dir: tmpDir.name,
|
|
19
|
+
name: 'entry.js',
|
|
20
|
+
});
|
|
21
|
+
tmpOutput = tmp.fileSync({
|
|
22
|
+
postfix: '.js',
|
|
23
|
+
dir: tmpDir.name,
|
|
24
|
+
name: 'output.js',
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterEach(async () => {
|
|
29
|
+
tmpEntry.removeCallback();
|
|
30
|
+
tmpOutput.removeCallback();
|
|
31
|
+
tmpDir.removeCallback();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it(
|
|
35
|
+
'should execute the bundle successfully',
|
|
36
|
+
async () => {
|
|
37
|
+
const code = `import crypto from 'crypto';console.log(process.env.NODE_ENV);`;
|
|
38
|
+
await fs.promises.writeFile(tmpEntry.name, code);
|
|
39
|
+
|
|
40
|
+
const bundler = new Webpack({
|
|
41
|
+
buildId: '12345',
|
|
42
|
+
custom: {},
|
|
43
|
+
entry: tmpEntry.name,
|
|
44
|
+
localCustom: {
|
|
45
|
+
optimization: {
|
|
46
|
+
minimize: false,
|
|
47
|
+
},
|
|
48
|
+
output: {
|
|
49
|
+
path: tmpDir.name,
|
|
50
|
+
filename: 'output.js',
|
|
51
|
+
},
|
|
52
|
+
infrastructureLogging: {
|
|
53
|
+
level: 'error',
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
useNodePolyfills: true,
|
|
57
|
+
useOwnWorker: false,
|
|
58
|
+
preset: {
|
|
59
|
+
name: 'javascript',
|
|
60
|
+
mode: 'compute',
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
await bundler.run();
|
|
64
|
+
|
|
65
|
+
const result = fs.readFileSync(tmpOutput.name, 'utf-8');
|
|
66
|
+
|
|
67
|
+
expect(result).toContain('./lib/build/bundlers/polyfills/node/crypto.js');
|
|
68
|
+
},
|
|
69
|
+
TIMEOUT,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
it(
|
|
73
|
+
'should merge the config, in this case optimization.minimize false',
|
|
74
|
+
async () => {
|
|
75
|
+
const bundler = new Webpack({
|
|
76
|
+
buildId: '123456',
|
|
77
|
+
custom: {
|
|
78
|
+
optimization: {
|
|
79
|
+
minimize: true,
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
entry: tmpEntry.name,
|
|
83
|
+
localCustom: {
|
|
84
|
+
optimization: {
|
|
85
|
+
minimize: false,
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
useNodePolyfills: false,
|
|
89
|
+
useOwnWorker: false,
|
|
90
|
+
preset: {
|
|
91
|
+
name: 'javascript',
|
|
92
|
+
mode: 'compute',
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
let config = AzionWebpackConfig;
|
|
96
|
+
config = bundler.mergeConfig(config);
|
|
97
|
+
expect(config?.optimization?.minimize).toBeFalsy();
|
|
98
|
+
},
|
|
99
|
+
TIMEOUT,
|
|
100
|
+
);
|
|
101
|
+
});
|