Skip to content

Commit 66f091a

Browse files
fix: bundlers compilation error (#229)
2 parents a7dffb0 + c1f9c6b commit 66f091a

File tree

11 files changed

+105
-44
lines changed

11 files changed

+105
-44
lines changed

lib/build/bundlers/base.bunlders.js lib/build/bundlers/base.bundlers.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class BaseBundlers {
1717
* @property {*} localCustom - Local custom data
1818
* @property {*} preset - The preset configuration
1919
* @property {string} contentToInject - Content to inject
20+
* @property {{[key: string]: string}} defineVars - Define vars on build
2021
*/
2122

2223
/**

lib/build/bundlers/esbuild/esbuild.bundlers.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import lodash from 'lodash';
44
import { Messages } from '#constants';
55
import { debug } from '#utils';
66

7-
import BaseBundlers from '../base.bunlders.js';
7+
import BaseBundlers from '../base.bundlers.js';
88
import AzionEsbuildConfig from './esbuild.config.js';
99
import ESBuildNodeModulePlugin from './plugins/node-polyfills/index.js';
1010
import ESBuildAzionModulePlugin from './plugins/azion-polyfills/index.js';
@@ -70,6 +70,15 @@ class Esbuild extends BaseBundlers {
7070
updatedConfig.banner = { js: workerInitContent };
7171
}
7272
}
73+
74+
// define vars
75+
if (this.builderConfig.defineVars) {
76+
updatedConfig.define = {
77+
...updatedConfig.define,
78+
...this.builderConfig.defineVars,
79+
};
80+
}
81+
7382
return updatedConfig;
7483
}
7584
}

lib/build/bundlers/webpack/plugins/azion-polyfills/azion-polyfills.plugins.js

+27-15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ class AzionPolyfillPlugin {
1414
compiler.options.plugins = [];
1515
}
1616

17+
// additional plugin to handle "node:" URIs
18+
compiler.options.plugins.push(
19+
new compiler.webpack.NormalModuleReplacementPlugin(
20+
new RegExp(`^${this.prefix}`),
21+
(resource) => {
22+
const mod = resource.request.replace(
23+
new RegExp(`^${this.prefix}`),
24+
'',
25+
);
26+
resource.request = mod;
27+
},
28+
),
29+
);
30+
1731
const filteredExternal = new Map(
1832
[...polyfillsManager.external].filter(([key]) => {
1933
const hasPrefix = new RegExp(`^${this.prefix}`).test(key);
@@ -22,21 +36,19 @@ class AzionPolyfillPlugin {
2236
);
2337

2438
if (this.buildProd) {
25-
compiler.options.externals = compiler.options.externals || [];
26-
compiler.options.externals.push(
27-
// eslint-disable-next-line
28-
({ request }, callback) => {
29-
const externalsToCheck = [...filteredExternal.keys()];
30-
31-
if (
32-
externalsToCheck.some((key) => new RegExp(`${key}$`).test(request))
33-
) {
34-
return callback(null, `module ${request}`);
35-
}
36-
37-
return callback();
38-
},
39-
);
39+
compiler.options.externals = compiler.options.externals || {};
40+
compiler.options.externalsType = 'module';
41+
compiler.options.externals = {
42+
...Object.fromEntries(
43+
[...filteredExternal].flatMap(([key]) => {
44+
return [
45+
[key, key],
46+
[`${this.prefix}${key}`, `${this.prefix}${key}`],
47+
];
48+
}),
49+
),
50+
...compiler.options.externals,
51+
};
4052
} else {
4153
compiler.options.plugins.push(
4254
new compiler.webpack.NormalModuleReplacementPlugin(

lib/build/bundlers/webpack/plugins/azion-polyfills/azion-polyfills.plugins.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ describe('Webpack Azion Plugin', () => {
6161
const runWebpack = promisify(webpack);
6262

6363
await runWebpack({
64+
experiments: {
65+
outputModule: true,
66+
},
6467
entry: tmpEntry.name,
6568
mode: 'production',
6669
optimization: {
@@ -74,6 +77,6 @@ describe('Webpack Azion Plugin', () => {
7477
plugins: [new AzionPolyfillPlugin(true)],
7578
});
7679
const bundle = fs.readFileSync(tmpOutput.name, 'utf-8');
77-
expect(bundle).toContain('import("azion:storage")');
80+
expect(bundle).toContain('external "azion:storage"');
7881
}, 20000);
7982
});

lib/build/bundlers/webpack/plugins/node-polyfills/node-polyfills.plugins.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,19 @@ class NodePolyfillPlugin {
6363
);
6464

6565
if (this.buildProd) {
66-
compiler.options.externals = compiler.options.externals || [];
67-
compiler.options.externals.push(
68-
// eslint-disable-next-line
69-
({ request }, callback) => {
70-
const externalsToCheck = [...filteredExternal.keys()];
71-
72-
if (
73-
externalsToCheck.some((key) => new RegExp(`${key}$`).test(request))
74-
) {
75-
return callback(null, `module ${request}`);
76-
}
77-
78-
return callback();
79-
},
80-
);
66+
compiler.options.externals = compiler.options.externals || {};
67+
compiler.options.externalsType = 'module';
68+
compiler.options.externals = {
69+
...compiler.options.externals,
70+
...Object.fromEntries(
71+
[...filteredExternal].flatMap(([key]) => {
72+
return [
73+
[key, key],
74+
[`${this.prefix}${key}`, `${this.prefix}${key}`],
75+
];
76+
}),
77+
),
78+
};
8179
} else {
8280
compiler.options.resolve.fallback = {
8381
...Object.fromEntries(

lib/build/bundlers/webpack/plugins/node-polyfills/node-polyfills.plugins.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ describe('Webpack Node Plugin', () => {
3636
const runWebpack = promisify(webpack);
3737

3838
await runWebpack({
39+
experiments: {
40+
outputModule: true,
41+
},
3942
entry: tmpEntry.name,
4043
mode: 'production',
4144
optimization: {

lib/build/bundlers/webpack/webpack.bundlers.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { feedback, debug } from '#utils';
77
import { Messages } from '#constants';
88

99
import AzionWebpackConfig from './webpack.config.js';
10-
import BaseBundlers from '../base.bunlders.js';
10+
import BaseBundlers from '../base.bundlers.js';
1111
import NodePolyfillPlugin from './plugins/node-polyfills/index.js';
1212
import AzionPolyfillPlugin from './plugins/azion-polyfills/index.js';
1313

@@ -75,6 +75,15 @@ class Webpack extends BaseBundlers {
7575
}
7676
}
7777

78+
// define vars
79+
if (this.builderConfig.defineVars) {
80+
updatedConfig.plugins.push(
81+
new webpack.DefinePlugin({
82+
...this.builderConfig.defineVars,
83+
}),
84+
);
85+
}
86+
7887
// use polyfill with useNodePolyfills and preset mode compute
7988
const useNodePolyfills =
8089
(this.builderConfig?.useNodePolyfills ||

lib/build/bundlers/webpack/webpack.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const outputPath = isWindows
1212
: join(projectRoot, '.edge');
1313

1414
export default {
15+
experiments: {
16+
outputModule: true,
17+
},
1518
output: {
1619
path: outputPath,
1720
filename: 'worker.js',

lib/build/dispatcher/dispatcher.js

+26-11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ import {
2323

2424
const isWindows = process.platform === 'win32';
2525

26+
/**
27+
* Represents a configuration object.
28+
* @typedef {object} BuilderConfig
29+
* @property {string} entry - The entry point for the configuration
30+
* @property {boolean} useNodePolyfills - Indicates if Node polyfills are being used
31+
* @property {boolean} useOwnWorker - Indicates if a custom worker is being used
32+
* @property {object} custom - Custom configuration.
33+
* @property {*} localCustom - Local custom data
34+
* @property {*} preset - The preset configuration
35+
* @property {string} contentToInject - Content to inject
36+
* @property {{[key: string]: string}} defineVars - Define vars on build
37+
*/
38+
2639
/**
2740
* Class representing a Dispatcher for build operations.
2841
* @example
@@ -261,14 +274,8 @@ class Dispatcher {
261274
* @param {object} currentConfig.preset - The current preset.
262275
* @param {object} currentConfig.localCustom - The current localCustom.
263276
* @param {object} currentConfig.memoryFS - The current memoryFS.
264-
* @param {object} config - The configuration object.
265-
* @typedef {object} BuildConfig
266-
* @property {boolean} useNodePolyfills - Indicates whether to use Node polyfills.
267-
* @property {boolean} useOwnWorker - Indicates whether to use a custom worker.
268-
* @property {*} localCustom - Custom configuration specific to the local environment.
269-
* @property {*} preset - Preset configuration for the build.
270-
* @property {string} entry - Path to the temporary entry file for the build.
271-
* @returns {BuildConfig} - Returns the configured build object.
277+
* @param {BuilderConfig} config - The configuration object.
278+
* @returns {BuilderConfig} - Returns the configured build object.
272279
*/
273280
static configureBuild(currentConfig, config) {
274281
const { useNodePolyfills, useOwnWorker, preset, localCustom, memoryFS } =
@@ -309,7 +316,7 @@ class Dispatcher {
309316
* @param {string} context.builId - The build id.
310317
* @param {string} context.handler - The handler preset file.
311318
* @param {string} context.prebuild - The prebuild preset file.
312-
* @param {BuildConfig} buildConfig - The build configuration object.
319+
* @param {BuilderConfig} buildConfig - The build configuration object.
313320
* @returns {Promise<object>} - A promise that resolves to an object containing the prebuild context.
314321
*/
315322
static async runPrebuild(context, buildConfig) {
@@ -335,6 +342,7 @@ class Dispatcher {
335342
const filesToInject = prebuildResult?.filesToInject || null;
336343
const workerGlobalVars = prebuildResult?.workerGlobalVars || null;
337344
const builderPlugins = prebuildResult?.builderPlugins || null;
345+
const defineVars = prebuildResult?.defineVars || null;
338346

339347
/**
340348
* Object containing code to be injected in a handler.
@@ -393,6 +401,7 @@ class Dispatcher {
393401

394402
prebuildContext.builderPlugins = builderPlugins;
395403
prebuildContext.codeToInjectInHandler = codeToInjectInHandler;
404+
prebuildContext.defineVars = defineVars;
396405

397406
return Promise.resolve(prebuildContext);
398407
}
@@ -401,7 +410,7 @@ class Dispatcher {
401410
* Executes the build process based on the provided build configuration.
402411
* @param {string} originalEntry - The original entry e.g ./index.js.
403412
* @param {string} builderCurrent - The builder current e.g esbuild.
404-
* @param {BuildConfig} buildConfig - The build configuration object.
413+
* @param {BuilderConfig} buildConfig - The build configuration object.
405414
* @returns {Promise<void>} - A promise that resolves when the build process is completed.
406415
*/
407416
static async executeBuild(originalEntry, builderCurrent, buildConfig) {
@@ -466,7 +475,8 @@ class Dispatcher {
466475
{ handler, prebuild },
467476
buildConfig,
468477
);
469-
const { codeToInjectInHandler, builderPlugins } = prebuildContext;
478+
const { codeToInjectInHandler, builderPlugins, defineVars } =
479+
prebuildContext;
470480

471481
// inject code in handler (build is necessary to pull the modules,
472482
// this is not a simples code append)
@@ -490,6 +500,11 @@ class Dispatcher {
490500

491501
Dispatcher.setVulcanLibAbsolutePath(buildConfig.entry);
492502

503+
// prebuildContext.defineVars
504+
if (defineVars) {
505+
buildConfig.defineVars = defineVars;
506+
}
507+
493508
await Dispatcher.executeBuild(this.entry, this.builder, buildConfig);
494509

495510
if (postbuild) {

lib/presets/custom/next/compute/default/prebuild/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ async function run(prebuildContext) {
123123
// inject on banner the globalThis._ENTRIES
124124
// this is necessary in the order of use, which needs to be defined at the top of the worker
125125
_ENTRIES: JSON.stringify({}),
126+
AsyncLocalStorage: JSON.stringify({}),
127+
},
128+
// defineVars (bundlers - define)
129+
defineVars: {
126130
__CONFIG__: JSON.stringify(processedVercelOutput.vercelConfig),
127131
},
128132
builderPlugins: [],

lib/utils/presets/presets.utils.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('getPresetsList utils', () => {
1616
'astro',
1717
'emscripten',
1818
'hexo',
19+
'hugo',
1920
'next',
2021
'react',
2122
'rustwasm',
@@ -81,6 +82,7 @@ describe('getPresetsList utils', () => {
8182
'Astro (Deliver)',
8283
'Emscripten (Compute)',
8384
'Hexo (Deliver)',
85+
'Hugo (Deliver)',
8486
'Next (Compute)',
8587
'Next (Deliver)',
8688
'React (Deliver)',
@@ -100,6 +102,7 @@ describe('getPresetsList utils', () => {
100102
'astro',
101103
'emscripten',
102104
'hexo',
105+
'hugo',
103106
'next',
104107
'react',
105108
'rustwasm',
@@ -113,6 +116,7 @@ describe('getPresetsList utils', () => {
113116
['Deliver'],
114117
['Compute'],
115118
['Deliver'],
119+
['Deliver'],
116120
['Compute', 'Deliver'],
117121
['Deliver'],
118122
['Compute'],

0 commit comments

Comments
 (0)