edge-functions 2.3.0 → 2.4.0-stage.1
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 +13 -0
- package/lib/build/bundlers/{bundler-base.js → base.bunlders.js} +3 -3
- package/lib/build/bundlers/esbuild/esbuild.bundlers.js +73 -0
- package/lib/build/bundlers/esbuild/{index.test.js → esbuild.bundlers.test.js} +1 -1
- package/lib/build/bundlers/esbuild/index.js +1 -67
- package/lib/build/bundlers/esbuild/plugins/azion-polyfills/azion-polyfills.plugins.js +90 -0
- package/lib/build/bundlers/esbuild/plugins/azion-polyfills/azion-polyfills.plugins.test.js +44 -0
- package/lib/build/bundlers/esbuild/plugins/azion-polyfills/index.js +3 -0
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/index.js +2 -137
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/node-polyfills.plugins.js +148 -0
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/{index.test.js → node-polyfills.plugins.test.js} +1 -1
- package/lib/build/bundlers/polyfills/index.js +3 -0
- package/lib/build/bundlers/polyfills/polyfills-manager.js +6 -2
- package/lib/build/bundlers/polyfills/polyfills-manager.test.js +1 -1
- package/lib/build/bundlers/webpack/index.js +1 -89
- package/lib/build/bundlers/webpack/plugins/azion-polyfills/azion-polyfills.plugins.js +67 -0
- package/lib/build/bundlers/webpack/plugins/azion-polyfills/azion-polyfills.plugins.test.js +79 -0
- package/lib/build/bundlers/webpack/plugins/azion-polyfills/index.js +3 -0
- package/lib/build/bundlers/webpack/plugins/node-polyfills/index.js +1 -89
- package/lib/build/bundlers/webpack/plugins/node-polyfills/node-polyfills.plugins.js +109 -0
- package/lib/build/bundlers/webpack/plugins/node-polyfills/{index.test.js → node-polyfills.plugins.test.js} +1 -1
- package/lib/build/bundlers/webpack/webpack.bundlers.js +96 -0
- package/lib/build/bundlers/webpack/{index.test.js → webpack.bundlers.test.js} +1 -1
- package/lib/env/polyfills/{async_hooks/index.js → async-hooks/async-hooks.polyfills.js} +3 -5
- package/lib/env/polyfills/async-hooks/context/index.js +3 -0
- package/lib/env/polyfills/async-hooks/index.js +8 -0
- package/lib/env/polyfills/{fetch/fetch.polyfills.js → azion/fetch/context/fetch.context.js} +2 -2
- package/lib/env/polyfills/{fetch/fetch.polyfills.test.js → azion/fetch/context/fetch.context.test.js} +4 -4
- package/lib/env/polyfills/azion/fetch/index.js +3 -0
- package/lib/env/polyfills/{fetch/FetchEvent.polyfills.js → azion/fetch-event/context/fetch-event.context.js} +2 -2
- package/lib/env/polyfills/azion/fetch-event/context/index.js +3 -0
- package/lib/env/polyfills/azion/fetch-event/index.js +3 -0
- package/lib/env/polyfills/azion/storage/context/index.js +3 -0
- package/lib/env/polyfills/azion/storage/context/storage.context.js +182 -0
- package/lib/env/polyfills/azion/storage/context/storage.context.test.js +199 -0
- package/lib/env/polyfills/azion/storage/index.js +4 -0
- package/lib/env/polyfills/azion/storage/storage.polyfills.js +206 -0
- package/lib/env/polyfills/azion/storage/storage.polyfills.test.js +102 -0
- package/lib/env/polyfills/index.js +5 -4
- package/lib/env/runtime.env.js +10 -6
- package/package.json +1 -1
- /package/lib/env/polyfills/{async_hooks/context/index.js → async-hooks/context/async-hooks.context.js} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
## [2.4.0-stage.1](https://github.com/aziontech/vulcan/compare/v2.3.0...v2.4.0-stage.1) (2024-01-24)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* adding polyfill for Storage API for local server ([444ea38](https://github.com/aziontech/vulcan/commit/444ea388801bf6acd2b201426f8296a8ba18cc3e))
|
|
7
|
+
* storage api local server ([#214](https://github.com/aziontech/vulcan/issues/214)) ([d793c90](https://github.com/aziontech/vulcan/commit/d793c904ccfb6a5f7bb971f85b8a832c5a5f8e0a))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Bug Fixes
|
|
11
|
+
|
|
12
|
+
* removing the requirement to useNodePolyfill when api external azion ([4b6cc2d](https://github.com/aziontech/vulcan/commit/4b6cc2d8e57297fa577fdb961831d14d4946fdf2))
|
|
13
|
+
|
|
1
14
|
## [2.3.0](https://github.com/aziontech/vulcan/compare/v2.2.0...v2.3.0) (2024-01-22)
|
|
2
15
|
|
|
3
16
|
|
|
@@ -6,7 +6,7 @@ import merge from 'lodash.merge';
|
|
|
6
6
|
* Base class for a bundler.
|
|
7
7
|
* @class
|
|
8
8
|
*/
|
|
9
|
-
class
|
|
9
|
+
class BaseBundlers {
|
|
10
10
|
/**
|
|
11
11
|
* Represents a configuration object.
|
|
12
12
|
* @typedef {object} BuilderConfig
|
|
@@ -60,7 +60,7 @@ class BundlerBase {
|
|
|
60
60
|
// This method can be overridden by subclasses
|
|
61
61
|
// as it does not require the use of `this`
|
|
62
62
|
// eslint-disable-next-line class-methods-use-this
|
|
63
|
-
throw new Error('Running
|
|
63
|
+
throw new Error('Running BaseBundlers run method');
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
/**
|
|
@@ -98,4 +98,4 @@ class BundlerBase {
|
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
export default
|
|
101
|
+
export default BaseBundlers;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import * as esbuild from 'esbuild';
|
|
2
|
+
import lodash from 'lodash';
|
|
3
|
+
|
|
4
|
+
import { Messages } from '#constants';
|
|
5
|
+
import { debug } from '#utils';
|
|
6
|
+
|
|
7
|
+
import BaseBundlers from '../base.bunlders.js';
|
|
8
|
+
import AzionEsbuildConfig from './esbuild.config.js';
|
|
9
|
+
import ESBuildNodeModulePlugin from './plugins/node-polyfills/index.js';
|
|
10
|
+
import ESBuildAzionModulePlugin from './plugins/azion-polyfills/index.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Class representing an ESBuild bundler, extending BaseBundlers.
|
|
14
|
+
*/
|
|
15
|
+
class Esbuild extends BaseBundlers {
|
|
16
|
+
/**
|
|
17
|
+
* Asynchronous method to run the ESBuild bundler.
|
|
18
|
+
*/
|
|
19
|
+
async run() {
|
|
20
|
+
let config = lodash.cloneDeep(AzionEsbuildConfig);
|
|
21
|
+
config.entryPoints = [this.builderConfig.entry];
|
|
22
|
+
|
|
23
|
+
if (!config.plugins) config.plugins = [];
|
|
24
|
+
|
|
25
|
+
// merge config common
|
|
26
|
+
config = super.mergeConfig(config);
|
|
27
|
+
config = this.applyConfig(config);
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
await esbuild.build(config);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
debug.error(error);
|
|
33
|
+
throw Error(Messages.build.error.vulcan_build_failed);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Applies specific configurations to the ESBuild config.
|
|
39
|
+
* @param {object} config - ESBuild configuration object.
|
|
40
|
+
* @returns {object} - Updated ESBuild configuration object.
|
|
41
|
+
*/
|
|
42
|
+
applyConfig(config) {
|
|
43
|
+
const updatedConfig = { ...config };
|
|
44
|
+
// use polyfill with useNodePolyfills and preset mode compute
|
|
45
|
+
const useNodePolyfills =
|
|
46
|
+
(this.builderConfig?.useNodePolyfills ||
|
|
47
|
+
this.customConfigPreset?.useNodePolyfills ||
|
|
48
|
+
this.customConfigLocal?.useNodePolyfills) &&
|
|
49
|
+
this.presetMode === 'compute';
|
|
50
|
+
|
|
51
|
+
if (!updatedConfig.plugins) updatedConfig.plugins = [];
|
|
52
|
+
if (useNodePolyfills) {
|
|
53
|
+
updatedConfig.plugins.push(ESBuildNodeModulePlugin(globalThis.buildProd));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// plugin resolve azion:
|
|
57
|
+
updatedConfig.plugins.push(ESBuildAzionModulePlugin(globalThis.buildProd));
|
|
58
|
+
|
|
59
|
+
// inject content in worker initial code.
|
|
60
|
+
if (this.builderConfig.contentToInject) {
|
|
61
|
+
const workerInitContent = this.builderConfig.contentToInject;
|
|
62
|
+
|
|
63
|
+
if (updatedConfig.banner?.js) {
|
|
64
|
+
updatedConfig.banner.js = `${updatedConfig.banner.js} ${workerInitContent}`;
|
|
65
|
+
} else {
|
|
66
|
+
updatedConfig.banner = { js: workerInitContent };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return updatedConfig;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export default Esbuild;
|
|
@@ -1,69 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import lodash from 'lodash';
|
|
3
|
-
|
|
4
|
-
import { Messages } from '#constants';
|
|
5
|
-
import { debug } from '#utils';
|
|
6
|
-
|
|
7
|
-
import BundlerBase from '../bundler-base.js';
|
|
8
|
-
import AzionEsbuildConfig from './esbuild.config.js';
|
|
9
|
-
import ESBuildNodeModulePlugin from './plugins/node-polyfills/index.js';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Class representing an ESBuild bundler, extending BundlerBase.
|
|
13
|
-
*/
|
|
14
|
-
class Esbuild extends BundlerBase {
|
|
15
|
-
/**
|
|
16
|
-
* Asynchronous method to run the ESBuild bundler.
|
|
17
|
-
*/
|
|
18
|
-
async run() {
|
|
19
|
-
let config = lodash.cloneDeep(AzionEsbuildConfig);
|
|
20
|
-
config.entryPoints = [this.builderConfig.entry];
|
|
21
|
-
|
|
22
|
-
if (!config.plugins) config.plugins = [];
|
|
23
|
-
|
|
24
|
-
// merge config common
|
|
25
|
-
config = super.mergeConfig(config);
|
|
26
|
-
config = this.applyConfig(config);
|
|
27
|
-
|
|
28
|
-
try {
|
|
29
|
-
await esbuild.build(config);
|
|
30
|
-
} catch (error) {
|
|
31
|
-
debug.error(error);
|
|
32
|
-
throw Error(Messages.build.error.vulcan_build_failed);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Applies specific configurations to the ESBuild config.
|
|
38
|
-
* @param {object} config - ESBuild configuration object.
|
|
39
|
-
* @returns {object} - Updated ESBuild configuration object.
|
|
40
|
-
*/
|
|
41
|
-
applyConfig(config) {
|
|
42
|
-
const updatedConfig = { ...config };
|
|
43
|
-
// use polyfill with useNodePolyfills and preset mode compute
|
|
44
|
-
const useNodePolyfills =
|
|
45
|
-
(this.builderConfig?.useNodePolyfills ||
|
|
46
|
-
this.customConfigPreset?.useNodePolyfills ||
|
|
47
|
-
this.customConfigLocal?.useNodePolyfills) &&
|
|
48
|
-
this.presetMode === 'compute';
|
|
49
|
-
|
|
50
|
-
if (useNodePolyfills) {
|
|
51
|
-
if (!updatedConfig.plugins) updatedConfig.plugins = [];
|
|
52
|
-
updatedConfig.plugins.push(ESBuildNodeModulePlugin(globalThis.buildProd));
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// inject content in worker initial code.
|
|
56
|
-
if (this.builderConfig.contentToInject) {
|
|
57
|
-
const workerInitContent = this.builderConfig.contentToInject;
|
|
58
|
-
|
|
59
|
-
if (updatedConfig.banner?.js) {
|
|
60
|
-
updatedConfig.banner.js = `${updatedConfig.banner.js} ${workerInitContent}`;
|
|
61
|
-
} else {
|
|
62
|
-
updatedConfig.banner = { js: workerInitContent };
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return updatedConfig;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
1
|
+
import Esbuild from './esbuild.bundlers.js';
|
|
68
2
|
|
|
69
3
|
export default Esbuild;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/* eslint-disable consistent-return */
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import PolyfillsManager from '../../../polyfills/index.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* ESBuild Azion Module Plugin for polyfilling node modules.
|
|
8
|
+
* @param {boolean} buildProd Parameter to identify whether the build is dev or prod
|
|
9
|
+
* @returns {object} - ESBuild plugin object.
|
|
10
|
+
*/
|
|
11
|
+
const ESBuildAzionModulePlugin = (buildProd) => {
|
|
12
|
+
const NAME = 'vulcan-azion-modules-polyfills';
|
|
13
|
+
const NAMESPACE = NAME;
|
|
14
|
+
const filter = /^azion:/;
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
/**
|
|
18
|
+
* Name and setup of the ESBuild plugin.
|
|
19
|
+
* @param {object} build - ESBuild build object.
|
|
20
|
+
*/
|
|
21
|
+
name: NAME,
|
|
22
|
+
setup: (build) => {
|
|
23
|
+
const polyfillManager = PolyfillsManager.buildPolyfills();
|
|
24
|
+
|
|
25
|
+
const options = build.initialOptions;
|
|
26
|
+
|
|
27
|
+
// external
|
|
28
|
+
if (buildProd) {
|
|
29
|
+
options.external = options.external || [];
|
|
30
|
+
[...polyfillManager.external].forEach(([key]) => {
|
|
31
|
+
if (/^[^:]+:/.test(key) && !options.external.includes(key)) {
|
|
32
|
+
options.external.push(key);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Resolve callback for ESBuild.
|
|
39
|
+
* @param {object} args - Arguments object.
|
|
40
|
+
* @returns {object|undefined} - Object with path and namespace or undefined.
|
|
41
|
+
*/
|
|
42
|
+
build.onResolve({ filter }, async (args) => {
|
|
43
|
+
if (!buildProd && polyfillManager.external.has(args.path)) {
|
|
44
|
+
return {
|
|
45
|
+
path: args.path,
|
|
46
|
+
namespace: NAMESPACE,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
if (!polyfillManager.external.has(args.path)) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// external bypass
|
|
54
|
+
if (
|
|
55
|
+
options?.external?.length > 0 &&
|
|
56
|
+
options?.external?.includes(args.path)
|
|
57
|
+
) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
path: args.path,
|
|
63
|
+
namespace: NAMESPACE,
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Load callback for node module files.
|
|
69
|
+
* @param {object} args - Arguments object.
|
|
70
|
+
* @returns {object} - Object with loader, contents, and resolve directory.
|
|
71
|
+
*/
|
|
72
|
+
build.onLoad({ filter, namespace: NAMESPACE }, async (args) => {
|
|
73
|
+
if (!polyfillManager.external.has(args.path)) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const resolved = polyfillManager.external.get(args.path);
|
|
77
|
+
const contents = await fs.promises.readFile(resolved, 'utf8');
|
|
78
|
+
const resolveDir = path.dirname(resolved);
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
loader: 'js',
|
|
82
|
+
contents,
|
|
83
|
+
resolveDir,
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export default ESBuildAzionModulePlugin;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as esbuild from 'esbuild';
|
|
2
|
+
import ESBuildAzionModulePlugin from './azion-polyfills.plugins.js';
|
|
3
|
+
|
|
4
|
+
describe('Esbuild Azion Plugin', () => {
|
|
5
|
+
it('should resolve the azion module: as external', async () => {
|
|
6
|
+
const results = await esbuild.build({
|
|
7
|
+
write: false,
|
|
8
|
+
target: 'es2022',
|
|
9
|
+
format: 'esm',
|
|
10
|
+
platform: 'browser',
|
|
11
|
+
mainFields: ['browser', 'main', 'module'],
|
|
12
|
+
bundle: true,
|
|
13
|
+
minify: false,
|
|
14
|
+
stdin: {
|
|
15
|
+
contents: 'import storage from "azion:storage";',
|
|
16
|
+
},
|
|
17
|
+
plugins: [ESBuildAzionModulePlugin(true)],
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
expect(results.outputFiles.at(0)?.text.trim()).toContain(
|
|
21
|
+
'import storage from "azion:storage";',
|
|
22
|
+
);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should resolve the azion module: for local execution', async () => {
|
|
26
|
+
const results = await esbuild.build({
|
|
27
|
+
write: false,
|
|
28
|
+
target: 'es2022',
|
|
29
|
+
format: 'esm',
|
|
30
|
+
platform: 'browser',
|
|
31
|
+
mainFields: ['browser', 'main', 'module'],
|
|
32
|
+
bundle: true,
|
|
33
|
+
minify: false,
|
|
34
|
+
stdin: {
|
|
35
|
+
contents: 'import storage from "azion:storage";',
|
|
36
|
+
},
|
|
37
|
+
plugins: [ESBuildAzionModulePlugin(false)],
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(results.outputFiles.at(0)?.text.trim()).toContain(
|
|
41
|
+
'vulcan-azion-modules-polyfills',
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -1,138 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import PolyfillsManager from '../../../polyfills/polyfills-manager.js';
|
|
1
|
+
import NodePolyfillPlugin from './node-polyfills.plugins.js';
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
* @param {boolean} buildProd Parameter to identify whether the build is dev or prod
|
|
8
|
-
* ESBuild Node Module Plugin for polyfilling node modules.
|
|
9
|
-
* @returns {object} - ESBuild plugin object.
|
|
10
|
-
*/
|
|
11
|
-
const ESBuildNodeModulePlugin = (buildProd) => {
|
|
12
|
-
const NAME = 'vulcan-node-modules-polyfills';
|
|
13
|
-
const NAMESPACE = NAME;
|
|
14
|
-
|
|
15
|
-
return {
|
|
16
|
-
/**
|
|
17
|
-
* Name and setup of the ESBuild plugin.
|
|
18
|
-
* @param {object} build - ESBuild build object.
|
|
19
|
-
*/
|
|
20
|
-
name: NAME,
|
|
21
|
-
setup: (build) => {
|
|
22
|
-
const polyfillManager = PolyfillsManager.buildPolyfills();
|
|
23
|
-
|
|
24
|
-
// build options
|
|
25
|
-
const options = build.initialOptions;
|
|
26
|
-
options.define = options.define || {};
|
|
27
|
-
if (!options.define?.global) {
|
|
28
|
-
options.define.global = 'globalThis';
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// define env
|
|
32
|
-
options.define = {
|
|
33
|
-
...options.define,
|
|
34
|
-
'process.env.NODE_ENV': '"production"',
|
|
35
|
-
'process.env.NEXT_RUNTIME': JSON.stringify('edge'),
|
|
36
|
-
'process.env.NEXT_COMPUTE_JS': JSON.stringify(true),
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
// build inject
|
|
40
|
-
options.inject = options.inject || [];
|
|
41
|
-
if (polyfillManager.globals) {
|
|
42
|
-
[...polyfillManager.globals].forEach(([, value]) => {
|
|
43
|
-
options.inject.push(value);
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// external
|
|
48
|
-
if (buildProd) {
|
|
49
|
-
options.external = options.external || [];
|
|
50
|
-
[...polyfillManager.external].forEach(([key]) => {
|
|
51
|
-
options.external.push(key);
|
|
52
|
-
options.external.push(`node:${key}`);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Resolve callback for ESBuild.
|
|
58
|
-
* @param {object} args - Arguments object.
|
|
59
|
-
* @returns {object|undefined} - Object with path and namespace or undefined.
|
|
60
|
-
*/
|
|
61
|
-
build.onResolve({ filter: /.*/ }, async (args) => {
|
|
62
|
-
const argsPath = args.path.replace(/^node:/, '');
|
|
63
|
-
|
|
64
|
-
if (!buildProd && polyfillManager.external.has(argsPath)) {
|
|
65
|
-
return {
|
|
66
|
-
path: args.path,
|
|
67
|
-
namespace: NAMESPACE,
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (!polyfillManager.libs.has(argsPath)) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// alias bypass
|
|
76
|
-
options.alias = options.alias || {};
|
|
77
|
-
if (
|
|
78
|
-
Object.keys(options.alias)?.length > 0 &&
|
|
79
|
-
options?.alias[argsPath]
|
|
80
|
-
) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// external bypass
|
|
85
|
-
if (
|
|
86
|
-
options?.external?.length > 0 &&
|
|
87
|
-
options?.external?.includes(argsPath)
|
|
88
|
-
) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return {
|
|
93
|
-
path: args.path,
|
|
94
|
-
namespace: NAMESPACE,
|
|
95
|
-
};
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Load callback for assets.
|
|
100
|
-
* @param {object} args - Arguments object.
|
|
101
|
-
* @returns {object} - Object with contents and loader type.
|
|
102
|
-
*/
|
|
103
|
-
build.onLoad({ filter: /\.(txt|html)/ }, async (args) => {
|
|
104
|
-
const contents = await fs.promises.readFile(args.path, 'utf8');
|
|
105
|
-
return {
|
|
106
|
-
contents,
|
|
107
|
-
loader: 'text',
|
|
108
|
-
};
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Load callback for node module files.
|
|
113
|
-
* @param {object} args - Arguments object.
|
|
114
|
-
* @returns {object} - Object with loader, contents, and resolve directory.
|
|
115
|
-
*/
|
|
116
|
-
build.onLoad({ filter: /.*/, namespace: NAMESPACE }, async (args) => {
|
|
117
|
-
const argsPath = args.path.replace(/^node:/, '');
|
|
118
|
-
|
|
119
|
-
let resolved = polyfillManager.libs.get(argsPath);
|
|
120
|
-
|
|
121
|
-
if (!buildProd && polyfillManager.external.has(argsPath)) {
|
|
122
|
-
resolved = polyfillManager.external.get(argsPath);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const contents = await fs.promises.readFile(resolved, 'utf8');
|
|
126
|
-
const resolveDir = path.dirname(resolved);
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
loader: 'js',
|
|
130
|
-
contents,
|
|
131
|
-
resolveDir,
|
|
132
|
-
};
|
|
133
|
-
});
|
|
134
|
-
},
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
export default ESBuildNodeModulePlugin;
|
|
3
|
+
export default NodePolyfillPlugin;
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/* eslint-disable consistent-return */
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import PolyfillsManager from '../../../polyfills/index.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {boolean} buildProd Parameter to identify whether the build is dev or prod
|
|
8
|
+
* ESBuild Node Module Plugin for polyfilling node modules.
|
|
9
|
+
* @returns {object} - ESBuild plugin object.
|
|
10
|
+
*/
|
|
11
|
+
const ESBuildNodeModulePlugin = (buildProd) => {
|
|
12
|
+
const NAME = 'vulcan-node-modules-polyfills';
|
|
13
|
+
const NAMESPACE = NAME;
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
/**
|
|
17
|
+
* Name and setup of the ESBuild plugin.
|
|
18
|
+
* @param {object} build - ESBuild build object.
|
|
19
|
+
*/
|
|
20
|
+
name: NAME,
|
|
21
|
+
setup: (build) => {
|
|
22
|
+
const polyfillManager = PolyfillsManager.buildPolyfills();
|
|
23
|
+
|
|
24
|
+
// build options
|
|
25
|
+
const options = build.initialOptions;
|
|
26
|
+
options.define = options.define || {};
|
|
27
|
+
if (!options.define?.global) {
|
|
28
|
+
options.define.global = 'globalThis';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// define env
|
|
32
|
+
options.define = {
|
|
33
|
+
...options.define,
|
|
34
|
+
'process.env.NODE_ENV': '"production"',
|
|
35
|
+
'process.env.NEXT_RUNTIME': JSON.stringify('edge'),
|
|
36
|
+
'process.env.NEXT_COMPUTE_JS': JSON.stringify(true),
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// build inject
|
|
40
|
+
options.inject = options.inject || [];
|
|
41
|
+
if (polyfillManager.globals) {
|
|
42
|
+
[...polyfillManager.globals].forEach(([, value]) => {
|
|
43
|
+
options.inject.push(value);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// external
|
|
48
|
+
if (buildProd) {
|
|
49
|
+
options.external = options.external || [];
|
|
50
|
+
[...polyfillManager.external].forEach(([key]) => {
|
|
51
|
+
if (!/^[^:]+:/.test(key)) {
|
|
52
|
+
options.external.push(key);
|
|
53
|
+
options.external.push(`node:${key}`);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Resolve callback for ESBuild.
|
|
60
|
+
* @param {object} args - Arguments object.
|
|
61
|
+
* @returns {object|undefined} - Object with path and namespace or undefined.
|
|
62
|
+
*/
|
|
63
|
+
build.onResolve({ filter: /.*/ }, async (args) => {
|
|
64
|
+
const argsPath = args.path.replace(/^node:/, '');
|
|
65
|
+
|
|
66
|
+
if (
|
|
67
|
+
!buildProd &&
|
|
68
|
+
polyfillManager.external.has(argsPath) &&
|
|
69
|
+
!/^[^:]+:/.test(argsPath)
|
|
70
|
+
) {
|
|
71
|
+
return {
|
|
72
|
+
path: args.path,
|
|
73
|
+
namespace: NAMESPACE,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!polyfillManager.libs.has(argsPath)) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// alias bypass
|
|
82
|
+
options.alias = options.alias || {};
|
|
83
|
+
if (
|
|
84
|
+
Object.keys(options.alias)?.length > 0 &&
|
|
85
|
+
options?.alias[argsPath]
|
|
86
|
+
) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// external bypass
|
|
91
|
+
if (
|
|
92
|
+
options?.external?.length > 0 &&
|
|
93
|
+
options?.external?.includes(argsPath)
|
|
94
|
+
) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
path: args.path,
|
|
100
|
+
namespace: NAMESPACE,
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Load callback for assets.
|
|
106
|
+
* @param {object} args - Arguments object.
|
|
107
|
+
* @returns {object} - Object with contents and loader type.
|
|
108
|
+
*/
|
|
109
|
+
build.onLoad({ filter: /\.(txt|html)/ }, async (args) => {
|
|
110
|
+
const contents = await fs.promises.readFile(args.path, 'utf8');
|
|
111
|
+
return {
|
|
112
|
+
contents,
|
|
113
|
+
loader: 'text',
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Load callback for node module files.
|
|
119
|
+
* @param {object} args - Arguments object.
|
|
120
|
+
* @returns {object} - Object with loader, contents, and resolve directory.
|
|
121
|
+
*/
|
|
122
|
+
build.onLoad({ filter: /.*/, namespace: NAMESPACE }, async (args) => {
|
|
123
|
+
const argsPath = args.path.replace(/^node:/, '');
|
|
124
|
+
|
|
125
|
+
let resolved = polyfillManager.libs.get(argsPath);
|
|
126
|
+
|
|
127
|
+
if (
|
|
128
|
+
!buildProd &&
|
|
129
|
+
polyfillManager.external.has(argsPath) &&
|
|
130
|
+
!/^[^:]+:/.test(argsPath)
|
|
131
|
+
) {
|
|
132
|
+
resolved = polyfillManager.external.get(argsPath);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const contents = await fs.promises.readFile(resolved, 'utf8');
|
|
136
|
+
const resolveDir = path.dirname(resolved);
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
loader: 'js',
|
|
140
|
+
contents,
|
|
141
|
+
resolveDir,
|
|
142
|
+
};
|
|
143
|
+
});
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
export default ESBuildNodeModulePlugin;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as esbuild from 'esbuild';
|
|
2
|
-
import ESBuildNodeModulePlugin from './
|
|
2
|
+
import ESBuildNodeModulePlugin from './node-polyfills.plugins.js';
|
|
3
3
|
|
|
4
4
|
describe('Esbuild Node Plugin', () => {
|
|
5
5
|
it('should check the env (NODE_ENV) in define options', async () => {
|
|
@@ -148,7 +148,11 @@ class PolyfillsManager {
|
|
|
148
148
|
|
|
149
149
|
this.setExternal(
|
|
150
150
|
'async_hooks',
|
|
151
|
-
`${externalPolyfillsPath}/
|
|
151
|
+
`${externalPolyfillsPath}/async-hooks/async-hooks.polyfills.js`,
|
|
152
|
+
);
|
|
153
|
+
this.setExternal(
|
|
154
|
+
'azion:storage',
|
|
155
|
+
`${externalPolyfillsPath}/azion/storage/storage.polyfills.js`,
|
|
152
156
|
);
|
|
153
157
|
|
|
154
158
|
return {
|
|
@@ -160,4 +164,4 @@ class PolyfillsManager {
|
|
|
160
164
|
}
|
|
161
165
|
}
|
|
162
166
|
|
|
163
|
-
export default
|
|
167
|
+
export default PolyfillsManager;
|