edge-functions 4.5.0 → 4.6.0
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 +14 -0
- package/docs/nodejs-apis.md +1 -1
- package/lib/build/bundlers/esbuild/esbuild.bundlers.test.js +3 -1
- package/lib/build/bundlers/esbuild/esbuild.config.js +1 -1
- package/lib/build/bundlers/esbuild/plugins/azion-polyfills/azion-polyfills.plugins.js +9 -11
- package/lib/build/bundlers/esbuild/plugins/azion-polyfills/azion-polyfills.plugins.test.js +1 -1
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/node-polyfills.plugins.js +297 -119
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/node-polyfills.plugins.test.js +1 -1
- package/lib/build/bundlers/helpers/azion-libs.js +25 -0
- package/lib/build/bundlers/webpack/plugins/azion-polyfills/azion-polyfills.plugins.js +4 -8
- package/lib/build/bundlers/webpack/plugins/node-polyfills/node-polyfills.plugins.js +104 -44
- package/lib/build/bundlers/webpack/plugins/node-polyfills/node-polyfills.plugins.test.js +0 -1
- package/lib/build/bundlers/webpack/webpack.bundlers.test.js +4 -2
- package/lib/build/bundlers/webpack/webpack.config.js +1 -1
- package/lib/env/polyfills/fs/fs.polyfills.js +2 -2
- package/lib/env/polyfills/fs/{promises.polyfills.js → promises/promises.polyfills.js} +1 -1
- package/package.json +6 -13
- package/lib/build/bundlers/polyfills/index.js +0 -3
- package/lib/build/bundlers/polyfills/node/_empty.js +0 -0
- package/lib/build/bundlers/polyfills/node/crypto.js +0 -70
- package/lib/build/bundlers/polyfills/node/dns.js +0 -152
- package/lib/build/bundlers/polyfills/node/fs.js +0 -524
- package/lib/build/bundlers/polyfills/node/fs.promises.js +0 -0
- package/lib/build/bundlers/polyfills/node/globals/buffer.js +0 -4
- package/lib/build/bundlers/polyfills/node/globals/navigator.js +0 -5
- package/lib/build/bundlers/polyfills/node/globals/path-dirname.js +0 -6
- package/lib/build/bundlers/polyfills/node/globals/performance.js +0 -45
- package/lib/build/bundlers/polyfills/node/globals/process.cjs +0 -203
- package/lib/build/bundlers/polyfills/node/http2.js +0 -148
- package/lib/build/bundlers/polyfills/node/module.js +0 -138
- package/lib/build/bundlers/polyfills/polyfills-manager.js +0 -198
- package/lib/build/bundlers/polyfills/polyfills-manager.test.js +0 -55
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [4.6.0](https://github.com/aziontech/bundler/compare/v4.5.0...v4.6.0) (2025-02-06)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* adding the unenv package and removing the polyfill manager ([#425](https://github.com/aziontech/bundler/issues/425)) ([529c267](https://github.com/aziontech/bundler/commit/529c267affba776467dd5127980078e8d98dbde6))
|
|
7
|
+
|
|
8
|
+
## [4.6.0-stage.1](https://github.com/aziontech/bundler/compare/v4.5.0...v4.6.0-stage.1) (2025-02-04)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* adding the unenv package and removing the polyfill manager ([#425](https://github.com/aziontech/bundler/issues/425)) ([529c267](https://github.com/aziontech/bundler/commit/529c267affba776467dd5127980078e8d98dbde6))
|
|
14
|
+
|
|
1
15
|
## [4.5.0](https://github.com/aziontech/bundler/compare/v4.4.2...v4.5.0) (2025-01-30)
|
|
2
16
|
|
|
3
17
|
|
package/docs/nodejs-apis.md
CHANGED
|
@@ -51,7 +51,7 @@ Table:
|
|
|
51
51
|
| Fs | ✅ |
|
|
52
52
|
| Async Hooks | ✅ |
|
|
53
53
|
|
|
54
|
-
Last test run date:
|
|
54
|
+
Last test run date: 02/06/25 03:55:49 AM
|
|
55
55
|
#### Docs support
|
|
56
56
|
|
|
57
57
|
See support for the Node.js APIs in the [https://www.azion.com/en/documentation/products/azion-edge-runtime/compatibility/node/](https://www.azion.com/en/documentation/products/azion-edge-runtime/compatibility/node/)
|
|
@@ -53,7 +53,9 @@ describe('Esbuild Bundler', () => {
|
|
|
53
53
|
|
|
54
54
|
const result = fs.readFileSync(tmpOutput.name, 'utf-8');
|
|
55
55
|
|
|
56
|
-
expect(result).toContain(
|
|
56
|
+
expect(result).toContain(
|
|
57
|
+
'node-built-in-modules:unenv/runtime/node/crypto/index',
|
|
58
|
+
);
|
|
57
59
|
},
|
|
58
60
|
TIMEOUT,
|
|
59
61
|
);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* eslint-disable consistent-return */
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import path from 'path';
|
|
4
|
-
import
|
|
4
|
+
import azionLibs from '../../../helpers/azion-libs.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* ESBuild Azion Module Plugin for polyfilling node modules.
|
|
@@ -9,7 +9,7 @@ import PolyfillsManager from '../../../polyfills/index.js';
|
|
|
9
9
|
* @returns {object} - ESBuild plugin object.
|
|
10
10
|
*/
|
|
11
11
|
const ESBuildAzionModulePlugin = (buildProd) => {
|
|
12
|
-
const NAME = '
|
|
12
|
+
const NAME = 'bundler-azion-modules-polyfills';
|
|
13
13
|
const NAMESPACE = NAME;
|
|
14
14
|
const filter = /^azion:/;
|
|
15
15
|
const prefix = 'azion:';
|
|
@@ -21,14 +21,12 @@ const ESBuildAzionModulePlugin = (buildProd) => {
|
|
|
21
21
|
*/
|
|
22
22
|
name: NAME,
|
|
23
23
|
setup: (build) => {
|
|
24
|
-
const polyfillManager = PolyfillsManager.buildPolyfills();
|
|
25
|
-
|
|
26
24
|
const options = build.initialOptions;
|
|
27
25
|
|
|
28
26
|
// external
|
|
29
27
|
if (buildProd) {
|
|
30
28
|
options.external = options.external || [];
|
|
31
|
-
[...
|
|
29
|
+
[...azionLibs.external].forEach(([key]) => {
|
|
32
30
|
if (/^[^:]+:/.test(key) && !options.external.includes(key)) {
|
|
33
31
|
options.external.push(key);
|
|
34
32
|
}
|
|
@@ -38,8 +36,8 @@ const ESBuildAzionModulePlugin = (buildProd) => {
|
|
|
38
36
|
if (!buildProd) {
|
|
39
37
|
// build inject prefix (azion:) is not present and the polyfill is Azion
|
|
40
38
|
options.inject = options.inject || [];
|
|
41
|
-
if (
|
|
42
|
-
[...
|
|
39
|
+
if (azionLibs.external) {
|
|
40
|
+
[...azionLibs.external].forEach(([key, value]) => {
|
|
43
41
|
const hasPrefix = /^[^:]+:/.test(key);
|
|
44
42
|
if (
|
|
45
43
|
!hasPrefix &&
|
|
@@ -57,13 +55,13 @@ const ESBuildAzionModulePlugin = (buildProd) => {
|
|
|
57
55
|
* @returns {object|undefined} - Object with path and namespace or undefined.
|
|
58
56
|
*/
|
|
59
57
|
build.onResolve({ filter }, async (args) => {
|
|
60
|
-
if (!buildProd &&
|
|
58
|
+
if (!buildProd && azionLibs.external.has(args.path)) {
|
|
61
59
|
return {
|
|
62
60
|
path: args.path,
|
|
63
61
|
namespace: NAMESPACE,
|
|
64
62
|
};
|
|
65
63
|
}
|
|
66
|
-
if (!
|
|
64
|
+
if (!azionLibs.external.has(args.path)) {
|
|
67
65
|
return;
|
|
68
66
|
}
|
|
69
67
|
|
|
@@ -87,10 +85,10 @@ const ESBuildAzionModulePlugin = (buildProd) => {
|
|
|
87
85
|
* @returns {object} - Object with loader, contents, and resolve directory.
|
|
88
86
|
*/
|
|
89
87
|
build.onLoad({ filter, namespace: NAMESPACE }, async (args) => {
|
|
90
|
-
if (!
|
|
88
|
+
if (!azionLibs.external.has(args.path)) {
|
|
91
89
|
return;
|
|
92
90
|
}
|
|
93
|
-
const resolved =
|
|
91
|
+
const resolved = azionLibs.external.get(args.path);
|
|
94
92
|
const contents = await fs.promises.readFile(resolved, 'utf8');
|
|
95
93
|
const resolveDir = path.dirname(resolved);
|
|
96
94
|
|
|
@@ -1,16 +1,297 @@
|
|
|
1
1
|
/* eslint-disable consistent-return */
|
|
2
|
+
import { createRequire } from 'module';
|
|
2
3
|
import fs from 'fs';
|
|
3
4
|
import path from 'path';
|
|
4
|
-
import
|
|
5
|
+
import { env, nodeless } from 'unenv';
|
|
6
|
+
import { builtinModules } from 'node:module';
|
|
7
|
+
import { getAbsoluteLibDirPath } from '#utils';
|
|
8
|
+
import unenvPresetAzion from 'azion/unenv-preset';
|
|
9
|
+
|
|
10
|
+
const require = createRequire(import.meta.url);
|
|
11
|
+
|
|
12
|
+
const { alias, inject, polyfill, external } = env(nodeless, unenvPresetAzion);
|
|
13
|
+
|
|
14
|
+
const INTERNAL_POLYFILL_DEV = 'internal-env-dev';
|
|
15
|
+
const INTERNAL_POLYFILL_PROD = 'internal-env-prod';
|
|
16
|
+
const INTERNAL_POLYFILL_PATH = `${getAbsoluteLibDirPath()}/env/polyfills`;
|
|
17
|
+
const INTERNAL_POLYFILL_PATH_PROD = `azion/unenv-preset/src/polyfills/node`;
|
|
18
|
+
const POLYFILL_PREFIX_DEV = 'aziondev:';
|
|
19
|
+
const POLYFILL_PREFIX_PROD = 'azionprd:';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Get global inject
|
|
23
|
+
* @param {*} globalInject Global inject
|
|
24
|
+
* @returns {*} Return import statement and export name
|
|
25
|
+
*/
|
|
26
|
+
function getGlobalInject(globalInject) {
|
|
27
|
+
if (typeof globalInject === 'string') {
|
|
28
|
+
return {
|
|
29
|
+
importStatement: `import globalVar from "${globalInject}";`,
|
|
30
|
+
exportName: 'globalVar',
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const [moduleSpecifier, exportName] = globalInject;
|
|
34
|
+
return {
|
|
35
|
+
importStatement: `import { ${exportName} } from "${moduleSpecifier}";`,
|
|
36
|
+
exportName,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Handle alias unenv
|
|
42
|
+
* @param {*} build Build object
|
|
43
|
+
*/
|
|
44
|
+
function handleAliasUnenv(build) {
|
|
45
|
+
const UNENV_ALIAS_NAMESPACE = 'imported-unenv-alias';
|
|
46
|
+
|
|
47
|
+
const aliasAbsolute = {};
|
|
48
|
+
Object.entries(alias).forEach(([module, unresolvedAlias]) => {
|
|
49
|
+
try {
|
|
50
|
+
aliasAbsolute[module] = require
|
|
51
|
+
.resolve(unresolvedAlias)
|
|
52
|
+
.replace(/\.cjs$/, '.mjs');
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.log(e?.message);
|
|
55
|
+
// this is an alias for package that is not installed in the current app => ignore
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const UNENV_ALIAS_RE = new RegExp(
|
|
60
|
+
`^(${Object.keys(aliasAbsolute).join('|')})$`,
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
build.onResolve({ filter: UNENV_ALIAS_RE }, (args) => {
|
|
64
|
+
return {
|
|
65
|
+
path: args.path,
|
|
66
|
+
namespace: UNENV_ALIAS_NAMESPACE,
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
build.onLoad(
|
|
71
|
+
{ filter: /.*/, namespace: UNENV_ALIAS_NAMESPACE },
|
|
72
|
+
async (args) => {
|
|
73
|
+
const filePath = aliasAbsolute[args.path];
|
|
74
|
+
const contents = await fs.promises.readFile(filePath, 'utf8');
|
|
75
|
+
const resolveDir = path.dirname(filePath);
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
loader: 'js',
|
|
79
|
+
contents,
|
|
80
|
+
resolveDir,
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Node built in modules
|
|
88
|
+
* @param {*} build Build object
|
|
89
|
+
* @param {*} isProd Is production build
|
|
90
|
+
*/
|
|
91
|
+
function nodeBuiltInModules(build, isProd) {
|
|
92
|
+
const IMPORTED_NODE_BUILT_IN_NAMESPACE = 'node-built-in-modules';
|
|
93
|
+
|
|
94
|
+
const NODEJS_MODULES_RE = new RegExp(
|
|
95
|
+
`^(node:)?(${builtinModules.join('|')})$`,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
build.onResolve({ filter: NODEJS_MODULES_RE }, (args) => {
|
|
99
|
+
const nameModuleWithoutNode = args.path.replace('node:', '');
|
|
100
|
+
const pathAlias =
|
|
101
|
+
alias[nameModuleWithoutNode] ?? alias[args.path] ?? args.path;
|
|
102
|
+
|
|
103
|
+
const findModulePolyfill = (prefix, nameModule) => {
|
|
104
|
+
return polyfill.find((p) => p.startsWith(`${prefix}${nameModule}`));
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const resolvePolyfill = (prefix, namespace, nameModule) => {
|
|
108
|
+
const externalModule = external.find((ext) => {
|
|
109
|
+
return (
|
|
110
|
+
ext === nameModule ||
|
|
111
|
+
ext === args.path ||
|
|
112
|
+
`node:${nameModule}` === ext
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
const polyfillResult = findModulePolyfill(prefix, nameModule);
|
|
116
|
+
// if polyfill is not found, check if the module is external
|
|
117
|
+
if (!polyfillResult && externalModule) {
|
|
118
|
+
return {
|
|
119
|
+
path: args.path,
|
|
120
|
+
external: externalModule.includes(args.path),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
if (polyfillResult) {
|
|
124
|
+
return { path: nameModule, namespace };
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const result = isProd
|
|
129
|
+
? resolvePolyfill(
|
|
130
|
+
POLYFILL_PREFIX_PROD,
|
|
131
|
+
INTERNAL_POLYFILL_PROD,
|
|
132
|
+
nameModuleWithoutNode,
|
|
133
|
+
)
|
|
134
|
+
: resolvePolyfill(
|
|
135
|
+
POLYFILL_PREFIX_DEV,
|
|
136
|
+
INTERNAL_POLYFILL_DEV,
|
|
137
|
+
nameModuleWithoutNode,
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
return (
|
|
141
|
+
result ?? {
|
|
142
|
+
path: pathAlias,
|
|
143
|
+
namespace: IMPORTED_NODE_BUILT_IN_NAMESPACE,
|
|
144
|
+
external: external.includes(args.path),
|
|
145
|
+
}
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
build.onLoad(
|
|
150
|
+
{ filter: /.*/, namespace: IMPORTED_NODE_BUILT_IN_NAMESPACE },
|
|
151
|
+
async (args) => {
|
|
152
|
+
const resolved = require.resolve(args.path);
|
|
153
|
+
const contents = await fs.promises.readFile(resolved, 'utf8');
|
|
154
|
+
const resolveDir = path.dirname(resolved);
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
loader: 'js',
|
|
158
|
+
contents,
|
|
159
|
+
resolveDir,
|
|
160
|
+
};
|
|
161
|
+
},
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Handle node js globals
|
|
167
|
+
* @param {*} build Build object
|
|
168
|
+
*/
|
|
169
|
+
function handleNodeJSGlobals(build) {
|
|
170
|
+
const UNENV_GLOBALS_RE = /_global_polyfill-([^.]+)\.js$/;
|
|
171
|
+
const prefix = path.resolve(
|
|
172
|
+
getAbsoluteLibDirPath(),
|
|
173
|
+
'../',
|
|
174
|
+
'_global_polyfill-',
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
// eslint-disable-next-line no-param-reassign
|
|
178
|
+
build.initialOptions.inject = [
|
|
179
|
+
...(build.initialOptions.inject ?? []),
|
|
180
|
+
...Object.keys(inject).map((globalName) => `${prefix}${globalName}.js`),
|
|
181
|
+
];
|
|
182
|
+
|
|
183
|
+
build.onResolve({ filter: UNENV_GLOBALS_RE }, (args) => ({
|
|
184
|
+
path: args.path,
|
|
185
|
+
}));
|
|
186
|
+
|
|
187
|
+
build.onLoad({ filter: UNENV_GLOBALS_RE }, (args) => {
|
|
188
|
+
const globalName = args.path.match(UNENV_GLOBALS_RE)[1];
|
|
189
|
+
const { importStatement, exportName } = getGlobalInject(inject[globalName]);
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
contents: `
|
|
193
|
+
${importStatement}
|
|
194
|
+
globalThis.${globalName} = ${exportName};
|
|
195
|
+
`,
|
|
196
|
+
};
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Handle internal polyfill env dev
|
|
202
|
+
* @param {*} build Build object
|
|
203
|
+
*/
|
|
204
|
+
function handleInternalPolyfillEnvDev(build) {
|
|
205
|
+
build.onLoad(
|
|
206
|
+
{ filter: /.*/, namespace: INTERNAL_POLYFILL_DEV },
|
|
207
|
+
async (args) => {
|
|
208
|
+
try {
|
|
209
|
+
const argsPathWhitoutNode = args.path.replace('node:', '');
|
|
210
|
+
const polyfillPath = polyfill.find((p) =>
|
|
211
|
+
p.startsWith(`${POLYFILL_PREFIX_DEV}${argsPathWhitoutNode}`),
|
|
212
|
+
);
|
|
213
|
+
const internalPolyfillsPath = path.join(
|
|
214
|
+
INTERNAL_POLYFILL_PATH,
|
|
215
|
+
polyfillPath.replace(
|
|
216
|
+
`${POLYFILL_PREFIX_DEV}${argsPathWhitoutNode}:/`,
|
|
217
|
+
'',
|
|
218
|
+
),
|
|
219
|
+
);
|
|
220
|
+
const contents = await fs.promises.readFile(
|
|
221
|
+
internalPolyfillsPath,
|
|
222
|
+
'utf8',
|
|
223
|
+
);
|
|
224
|
+
const resolveDir = path.dirname(internalPolyfillsPath);
|
|
225
|
+
return {
|
|
226
|
+
loader: 'js',
|
|
227
|
+
contents,
|
|
228
|
+
resolveDir,
|
|
229
|
+
};
|
|
230
|
+
} catch (error) {
|
|
231
|
+
console.error(`Error loading polyfill for ${args.path}`, error);
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Handle internal polyfill env prod
|
|
239
|
+
* @param {*} build Build object
|
|
240
|
+
*/
|
|
241
|
+
function handleInternalPolyfillEnvProd(build) {
|
|
242
|
+
build.onLoad(
|
|
243
|
+
{ filter: /.*/, namespace: INTERNAL_POLYFILL_PROD },
|
|
244
|
+
async (args) => {
|
|
245
|
+
try {
|
|
246
|
+
const polyfillPath = polyfill.find((p) =>
|
|
247
|
+
p.startsWith(`${POLYFILL_PREFIX_PROD}${args.path}`),
|
|
248
|
+
);
|
|
249
|
+
const internalPolyfillsPath = path.join(
|
|
250
|
+
INTERNAL_POLYFILL_PATH_PROD,
|
|
251
|
+
polyfillPath.replace(`${POLYFILL_PREFIX_PROD}${args.path}:/`, ''),
|
|
252
|
+
);
|
|
253
|
+
const resolved = require.resolve(internalPolyfillsPath);
|
|
254
|
+
const contents = await fs.promises.readFile(resolved, 'utf8');
|
|
255
|
+
const resolveDir = path.dirname(resolved);
|
|
256
|
+
return {
|
|
257
|
+
loader: 'js',
|
|
258
|
+
contents,
|
|
259
|
+
resolveDir,
|
|
260
|
+
};
|
|
261
|
+
} catch (error) {
|
|
262
|
+
console.error(`Error loading polyfill prod for ${args.path}`, error);
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Define next js runtime
|
|
270
|
+
* @param {*} options Options object
|
|
271
|
+
*/
|
|
272
|
+
function defineNextJsRuntime(options) {
|
|
273
|
+
if (fs.existsSync(path.join(process.cwd(), '.next'))) {
|
|
274
|
+
const buildId = fs.readFileSync(
|
|
275
|
+
path.join(process.cwd(), '.next/BUILD_ID'),
|
|
276
|
+
'utf-8',
|
|
277
|
+
);
|
|
278
|
+
// eslint-disable-next-line no-param-reassign
|
|
279
|
+
options.define = {
|
|
280
|
+
...options.define,
|
|
281
|
+
'process.env.NEXT_RUNTIME': '"edge"',
|
|
282
|
+
'process.env.NEXT_COMPUTE_JS': 'true',
|
|
283
|
+
'process.env.__NEXT_BUILD_ID': `"${buildId}"`,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
5
287
|
|
|
6
288
|
/**
|
|
7
|
-
* @param {boolean} buildProd Parameter to identify whether the build is dev or prod
|
|
8
289
|
* ESBuild Node Module Plugin for polyfilling node modules.
|
|
290
|
+
* @param {boolean} buildProd Parameter to identify whether the build is dev or prod
|
|
9
291
|
* @returns {object} - ESBuild plugin object.
|
|
10
292
|
*/
|
|
11
293
|
const ESBuildNodeModulePlugin = (buildProd) => {
|
|
12
|
-
const NAME = '
|
|
13
|
-
const NAMESPACE = NAME;
|
|
294
|
+
const NAME = 'bundler-node-modules-polyfills';
|
|
14
295
|
|
|
15
296
|
return {
|
|
16
297
|
/**
|
|
@@ -19,11 +300,10 @@ const ESBuildNodeModulePlugin = (buildProd) => {
|
|
|
19
300
|
*/
|
|
20
301
|
name: NAME,
|
|
21
302
|
setup: (build) => {
|
|
22
|
-
const polyfillManager = PolyfillsManager.buildPolyfills();
|
|
23
|
-
|
|
24
303
|
// build options
|
|
25
304
|
const options = build.initialOptions;
|
|
26
305
|
options.define = options.define || {};
|
|
306
|
+
|
|
27
307
|
if (!options.define?.global) {
|
|
28
308
|
options.define.global = 'globalThis';
|
|
29
309
|
}
|
|
@@ -34,124 +314,22 @@ const ESBuildNodeModulePlugin = (buildProd) => {
|
|
|
34
314
|
'process.env.NODE_ENV': '"production"',
|
|
35
315
|
};
|
|
36
316
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
path.join(process.cwd(), '.next/BUILD_ID'),
|
|
40
|
-
'utf-8',
|
|
41
|
-
);
|
|
42
|
-
options.define = {
|
|
43
|
-
...options.define,
|
|
44
|
-
'process.env.NEXT_RUNTIME': '"edge"',
|
|
45
|
-
'process.env.NEXT_COMPUTE_JS': 'true',
|
|
46
|
-
'process.env.__NEXT_BUILD_ID': `"${buildId}"`,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
317
|
+
// define nextjs runtime
|
|
318
|
+
defineNextJsRuntime(options);
|
|
49
319
|
|
|
50
320
|
// build inject
|
|
51
321
|
options.inject = options.inject || [];
|
|
52
|
-
if (polyfillManager.globals) {
|
|
53
|
-
[...polyfillManager.globals].forEach(([, value]) => {
|
|
54
|
-
options.inject.push(value);
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// external
|
|
59
|
-
if (buildProd) {
|
|
60
|
-
options.external = options.external || [];
|
|
61
|
-
[...polyfillManager.external].forEach(([key]) => {
|
|
62
|
-
if (!/^[^:]+:/.test(key)) {
|
|
63
|
-
options.external.push(key);
|
|
64
|
-
options.external.push(`node:${key}`);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Resolve callback for ESBuild.
|
|
71
|
-
* @param {object} args - Arguments object.
|
|
72
|
-
* @returns {object|undefined} - Object with path and namespace or undefined.
|
|
73
|
-
*/
|
|
74
|
-
build.onResolve({ filter: /.*/ }, async (args) => {
|
|
75
|
-
const argsPath = args.path.replace(/^node:/, '');
|
|
76
|
-
|
|
77
|
-
if (
|
|
78
|
-
!buildProd &&
|
|
79
|
-
polyfillManager.external.has(argsPath) &&
|
|
80
|
-
!/^[^:]+:/.test(argsPath)
|
|
81
|
-
) {
|
|
82
|
-
return {
|
|
83
|
-
path: args.path,
|
|
84
|
-
namespace: NAMESPACE,
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (!polyfillManager.libs.has(argsPath)) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// alias bypass
|
|
93
|
-
options.alias = options.alias || {};
|
|
94
|
-
if (
|
|
95
|
-
Object.keys(options.alias)?.length > 0 &&
|
|
96
|
-
options?.alias[argsPath]
|
|
97
|
-
) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// external bypass
|
|
102
|
-
if (
|
|
103
|
-
options?.external?.length > 0 &&
|
|
104
|
-
options?.external?.includes(argsPath)
|
|
105
|
-
) {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return {
|
|
110
|
-
path: args.path,
|
|
111
|
-
namespace: NAMESPACE,
|
|
112
|
-
};
|
|
113
|
-
});
|
|
114
322
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
* @returns {object} - Object with contents and loader type.
|
|
119
|
-
*/
|
|
120
|
-
build.onLoad({ filter: /\.(txt|html)/ }, async (args) => {
|
|
121
|
-
const contents = await fs.promises.readFile(args.path, 'utf8');
|
|
122
|
-
return {
|
|
123
|
-
contents,
|
|
124
|
-
loader: 'text',
|
|
125
|
-
};
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Load callback for node module files.
|
|
130
|
-
* @param {object} args - Arguments object.
|
|
131
|
-
* @returns {object} - Object with loader, contents, and resolve directory.
|
|
132
|
-
*/
|
|
133
|
-
build.onLoad({ filter: /.*/, namespace: NAMESPACE }, async (args) => {
|
|
134
|
-
const argsPath = args.path.replace(/^node:/, '');
|
|
135
|
-
|
|
136
|
-
let resolved = polyfillManager.libs.get(argsPath);
|
|
137
|
-
|
|
138
|
-
if (
|
|
139
|
-
!buildProd &&
|
|
140
|
-
polyfillManager.external.has(argsPath) &&
|
|
141
|
-
!/^[^:]+:/.test(argsPath)
|
|
142
|
-
) {
|
|
143
|
-
resolved = polyfillManager.external.get(argsPath);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const contents = await fs.promises.readFile(resolved, 'utf8');
|
|
147
|
-
const resolveDir = path.dirname(resolved);
|
|
323
|
+
options.alias = {
|
|
324
|
+
...options.alias,
|
|
325
|
+
};
|
|
148
326
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
327
|
+
// resolve modules
|
|
328
|
+
nodeBuiltInModules(build, buildProd);
|
|
329
|
+
handleAliasUnenv(build, alias, external);
|
|
330
|
+
handleNodeJSGlobals(build, inject);
|
|
331
|
+
handleInternalPolyfillEnvDev(build);
|
|
332
|
+
handleInternalPolyfillEnvProd(build);
|
|
155
333
|
},
|
|
156
334
|
};
|
|
157
335
|
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { getAbsoluteLibDirPath } from '#utils';
|
|
2
|
+
|
|
3
|
+
const libDirPath = getAbsoluteLibDirPath();
|
|
4
|
+
|
|
5
|
+
const externalPolyfillsPath = `${libDirPath}/env/polyfills`;
|
|
6
|
+
|
|
7
|
+
export default {
|
|
8
|
+
libs: new Map(),
|
|
9
|
+
globals: new Map(),
|
|
10
|
+
alias: new Map(),
|
|
11
|
+
external: new Map([
|
|
12
|
+
[
|
|
13
|
+
'azion:storage',
|
|
14
|
+
`${externalPolyfillsPath}/azion/storage/storage.polyfills.js`,
|
|
15
|
+
],
|
|
16
|
+
[
|
|
17
|
+
'Azion.env',
|
|
18
|
+
`${externalPolyfillsPath}/azion/env-vars/env-vars.polyfills.js`,
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
'Azion.networkList',
|
|
22
|
+
`${externalPolyfillsPath}/azion/network-list/network-list.polyfills.js`,
|
|
23
|
+
],
|
|
24
|
+
]),
|
|
25
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-param-reassign,class-methods-use-this */
|
|
2
2
|
import { generateWebpackBanner } from '#utils';
|
|
3
|
-
import
|
|
3
|
+
import azionLibs from '../../../helpers/azion-libs.js';
|
|
4
4
|
|
|
5
5
|
class AzionPolyfillPlugin {
|
|
6
6
|
constructor(buildProd) {
|
|
@@ -9,8 +9,6 @@ class AzionPolyfillPlugin {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
apply(compiler) {
|
|
12
|
-
const polyfillsManager = PolyfillsManager.buildPolyfills();
|
|
13
|
-
|
|
14
12
|
if (!compiler.options.plugins?.length) {
|
|
15
13
|
compiler.options.plugins = [];
|
|
16
14
|
}
|
|
@@ -30,7 +28,7 @@ class AzionPolyfillPlugin {
|
|
|
30
28
|
);
|
|
31
29
|
|
|
32
30
|
const filteredExternal = new Map(
|
|
33
|
-
[...
|
|
31
|
+
[...azionLibs.external].filter(([key]) => {
|
|
34
32
|
const hasPrefix = new RegExp(`^${this.prefix}`).test(key);
|
|
35
33
|
return hasPrefix;
|
|
36
34
|
}),
|
|
@@ -38,7 +36,7 @@ class AzionPolyfillPlugin {
|
|
|
38
36
|
|
|
39
37
|
// build inject prefix (azion:) is not present and the polyfill is Azion
|
|
40
38
|
if (!this.buildProd) {
|
|
41
|
-
[...
|
|
39
|
+
[...azionLibs.external].forEach(([key]) => {
|
|
42
40
|
const hasPrefix = /^[^:]+:/.test(key);
|
|
43
41
|
if (
|
|
44
42
|
!hasPrefix &&
|
|
@@ -46,9 +44,7 @@ class AzionPolyfillPlugin {
|
|
|
46
44
|
) {
|
|
47
45
|
compiler.options.plugins.push(
|
|
48
46
|
new compiler.webpack.BannerPlugin({
|
|
49
|
-
banner: generateWebpackBanner([
|
|
50
|
-
polyfillsManager.external.get(key),
|
|
51
|
-
]),
|
|
47
|
+
banner: generateWebpackBanner([azionLibs.external.get(key)]),
|
|
52
48
|
raw: true,
|
|
53
49
|
}),
|
|
54
50
|
);
|