Skip to content

Commit 8acdd5f

Browse files
committed
fix: bannerPlugin webpack injection bug fixed using bundler define
An error was identified when it is injected by BannerPlugin into webpack, which replaces the paths of routes [id] from __CONFIG___ through the placeholders option. How this was changed to do the injection through the bundlers define.
1 parent 0339874 commit 8acdd5f

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
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/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/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-1
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@ 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-
__CONFIG__: JSON.stringify(processedVercelOutput.vercelConfig),
127126
AsyncLocalStorage: JSON.stringify({}),
128127
},
128+
// defineVars (bundlers - define)
129+
defineVars: {
130+
__CONFIG__: JSON.stringify(processedVercelOutput.vercelConfig),
131+
},
129132
builderPlugins: [],
130133
};
131134
}

0 commit comments

Comments
 (0)