Skip to content

Commit 8a88ff7

Browse files
committed
feat: enable useOwnWorker for custom provider
1 parent 605d6e6 commit 8a88ff7

File tree

3 files changed

+53
-19
lines changed

3 files changed

+53
-19
lines changed

lib/build/dispatcher/dispatcher.js

+37-17
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ async function getAliasPath(alias) {
4949
* @param {string} preset - The build preset.
5050
* @param {string} entry - The entrypoint file path.
5151
* @param {string} mode - The mode of preset build.
52-
* @returns {any} The context that will be used in build.
52+
* @param {boolean} useOwnWorker - The mode of preset build.
53+
* @returns {any} This flag indicates that the constructed code inserts its own worker (provider) expression.
5354
*/
54-
async function loadBuildContext(preset, entry, mode) {
55+
async function loadBuildContext(preset, entry, mode, useOwnWorker) {
5556
const VALID_BUILD_PRESETS = presets.getKeys();
5657

5758
const validPreset = VALID_BUILD_PRESETS.includes(preset);
@@ -97,16 +98,21 @@ async function loadBuildContext(preset, entry, mode) {
9798
prebuildFilePath = new URL(`file://${prebuildFilePath}`).href;
9899

99100
const config = (await import(configFilePath)).default;
100-
const { useOwnWorker } = config;
101101

102102
const prebuild = (await import(prebuildFilePath)).default;
103-
104103
const handlerTemplate = readFileSync(handlerFilePath, 'utf-8');
105104
const handlerTemplateBody = getExportedFunctionBody(handlerTemplate);
106105

107106
let newEntryContent;
107+
108+
// use providers
109+
if (useOwnWorker) {
110+
newEntryContent = `(async function() {
111+
${handlerTemplateBody}
112+
})()`;
113+
}
114+
108115
if (!useOwnWorker) {
109-
// use default provider
110116
const workerFilePath = join(
111117
vulcanLibPath,
112118
'providers',
@@ -122,11 +128,6 @@ async function loadBuildContext(preset, entry, mode) {
122128
})()`,
123129
);
124130
}
125-
if (useOwnWorker) {
126-
newEntryContent = `(async function() {
127-
${handlerTemplateBody}
128-
})()`;
129-
}
130131

131132
// resolve #edge alias without vulcan context
132133
const edgehooksPath = await getAliasPath('edge');
@@ -139,11 +140,18 @@ async function loadBuildContext(preset, entry, mode) {
139140
try {
140141
const filePath = join(process.cwd(), entry);
141142
const entryContent = readFileSync(filePath, 'utf-8');
142-
const entrypointModified = getExportedFunctionBody(entryContent);
143-
newEntryContent = newEntryContent.replace(
144-
'__JS_CODE__',
145-
entrypointModified,
146-
);
143+
144+
if (useOwnWorker) {
145+
newEntryContent = newEntryContent.replace('__JS_CODE__', entryContent);
146+
console.log(newEntryContent);
147+
}
148+
if (!useOwnWorker) {
149+
const entrypointModified = getExportedFunctionBody(entryContent);
150+
newEntryContent = newEntryContent.replace(
151+
'__JS_CODE__',
152+
entrypointModified,
153+
);
154+
}
147155
} catch (error) {
148156
feedback.build.error(Messages.errors.file_doesnt_exist(entry));
149157
debug.error(error);
@@ -208,12 +216,15 @@ class Dispatcher {
208216
* @param {string} mode - The mode of build target.
209217
* @param {string} entry - The entry point for the build.
210218
* @param {boolean} useNodePolyfills - The flag to indicates polyfills use.
219+
* @param {boolean} useOwnWorker - This flag indicates that the constructed code inserts its own worker expression,
220+
* such as addEventListener("fetch") or similar, without the need to inject a provider.
211221
*/
212-
constructor(preset, mode, entry, useNodePolyfills) {
222+
constructor(preset, mode, entry, useNodePolyfills, useOwnWorker) {
213223
this.preset = preset;
214224
this.mode = mode;
215225
this.entry = entry;
216226
this.useNodePolyfills = useNodePolyfills;
227+
this.useOwnWorker = useOwnWorker;
217228
this.buildId = generateTimestamp();
218229
}
219230

@@ -230,11 +241,13 @@ class Dispatcher {
230241
);
231242
process.exit(1);
232243
}
244+
233245
// Load Context based on preset
234246
const { entryContent, prebuild, config } = await loadBuildContext(
235247
this.preset,
236248
this.entry,
237249
this.mode,
250+
this.useOwnWorker,
238251
);
239252

240253
const buildContext = {
@@ -244,6 +257,7 @@ class Dispatcher {
244257
entryContent,
245258
config,
246259
useNodePolyfills: this.useNodePolyfills,
260+
useOwnWorker: this.useOwnWorker,
247261
buildId: this.buildId,
248262
};
249263

@@ -267,6 +281,7 @@ class Dispatcher {
267281
config.entry = tempBuilderEntryPath;
268282
config.buildId = this.buildId;
269283
config.useNodePolyfills = this.useNodePolyfills;
284+
config.useOwnWorker = this.useOwnWorker;
270285

271286
let builder;
272287
switch (config.builder) {
@@ -292,7 +307,12 @@ class Dispatcher {
292307
entry: this.entry,
293308
preset: this.preset,
294309
mode: this.mode,
295-
useNodePolyfills: this.useNodePolyfills,
310+
...(this.useNodePolyfills !== null && {
311+
useNodePolyfills: this.useNodePolyfills,
312+
}),
313+
...(this.useOwnWorker !== null && {
314+
useOwnWorker: this.useOwnWorker,
315+
}),
296316
},
297317
'local',
298318
);

lib/commands/build.commands.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { vulcan } from '#env';
1414
* @param {string} [options.preset] - Preset to be used (e.g., 'javascript', 'typescript').
1515
* @param {string} [options.mode] - Mode in which to run the build (e.g., 'deliver', 'compute').
1616
* @param {boolean} [options.useNodePolyfills] - Whether to use Node.js polyfills.
17+
* @param {boolean} [options.useOwnWorker] - This flag indicates that the constructed code inserts its own worker expression, such as addEventListener("fetch") or similar, without the need to inject a provider.
1718
* @returns {Promise<void>} - A promise that resolves when the build is complete.
1819
* @example
1920
*
@@ -24,12 +25,19 @@ import { vulcan } from '#env';
2425
* useNodePolyfills: false
2526
* });
2627
*/
27-
async function buildCommand({ entry, preset, mode, useNodePolyfills }) {
28+
async function buildCommand({
29+
entry,
30+
preset,
31+
mode,
32+
useNodePolyfills,
33+
useOwnWorker,
34+
}) {
2835
let config = {
2936
entry,
3037
preset,
3138
mode,
3239
useNodePolyfills,
40+
useOwnWorker,
3341
};
3442

3543
const vulcanVariables = await vulcan.readVulcanEnv('local');
@@ -40,7 +48,8 @@ async function buildCommand({ entry, preset, mode, useNodePolyfills }) {
4048
preset: preset ?? vulcanVariables?.preset ?? 'javascript',
4149
mode: mode ?? vulcanVariables?.mode ?? 'compute',
4250
useNodePolyfills:
43-
useNodePolyfills ?? vulcanVariables?.useNodePolyfills ?? false,
51+
useNodePolyfills ?? vulcanVariables?.useNodePolyfills ?? null,
52+
useOwnWorker: useOwnWorker ?? vulcanVariables?.useOwnWorker ?? null,
4453
};
4554
feedback.info(`Using ${config.entry} as entry point by default`);
4655

@@ -50,6 +59,7 @@ async function buildCommand({ entry, preset, mode, useNodePolyfills }) {
5059
config.mode,
5160
config.entry,
5261
config.useNodePolyfills,
62+
config.useOwnWorker,
5363
);
5464

5565
await buildDispatcher.run();

lib/main.js

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ function startVulcanProgram() {
8282
)
8383
.option('--mode <type>', 'Mode of build target (e.g., deliver, compute)')
8484
.option('--useNodePolyfills', 'Use node polyfills in build.')
85+
.option(
86+
'--useOwnWorker',
87+
'This flag indicates that the constructed code inserts its own worker expression, such as addEventListener("fetch") or similar, without the need to inject a provider.',
88+
)
8589
.option(
8690
'--useOwnWorker',
8791
'Enabling this setting disables the automatic insertion of \'addEventListener("fetch")\' or equivalent for each provider.',

0 commit comments

Comments
 (0)