Skip to content

Commit 8040efa

Browse files
committed
feat: add an ability to replace load and resolve
1 parent ef6a3c4 commit 8040efa

File tree

3 files changed

+399
-55
lines changed

3 files changed

+399
-55
lines changed

apps/tester-app/index.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,27 @@ ScriptManager.shared.hooks.beforeResolve.tapAsync(
6969

7070
ScriptManager.shared.hooks.resolve.tapAsync(
7171
'test-during',
72-
({ scriptId, caller, error }, callback) => {
73-
if (!error) {
74-
console.log('During resolving:', scriptId, caller);
72+
async (params, callback) => {
73+
try {
74+
for (const [, , resolve] of params.resolvers) {
75+
const resolvedLocator = await resolve(
76+
params.scriptId,
77+
params.caller,
78+
params.referenceUrl
79+
);
80+
if (resolvedLocator) {
81+
callback(null, {
82+
...params,
83+
result: resolvedLocator,
84+
});
85+
}
86+
}
87+
// If no resolver found a result, pass through the params unchanged
88+
callback(null, params);
89+
} catch (error) {
90+
console.error('Error resolving:', error);
91+
callback(error);
7592
}
76-
console.log('ScriptManager.shared.hooks.resolve', scriptId, caller, error);
77-
callback();
7893
}
7994
);
8095

@@ -125,9 +140,18 @@ ScriptManager.shared.hooks.beforeLoad.tapAsync(
125140

126141
ScriptManager.shared.hooks.load.tapAsync(
127142
'test-load',
128-
({ scriptId, caller, error }, callback) => {
129-
console.log('ScriptManager.shared.hooks.load', scriptId, caller, error);
130-
callback();
143+
async (params, callback) => {
144+
try {
145+
console.log(
146+
'ScriptManager.shared.hooks.load',
147+
params.scriptId,
148+
params.caller
149+
);
150+
await params.loadScript();
151+
callback(null);
152+
} catch (error) {
153+
callback(error);
154+
}
131155
}
132156
);
133157

packages/repack/src/modules/ScriptManager/ScriptManager.ts

+61-31
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ export interface ResolverOptions {
5555
key?: string;
5656
}
5757

58+
interface ResolveHookParams {
59+
scriptId: string;
60+
caller?: string;
61+
referenceUrl?: string;
62+
resolvers: Array<[string, string | number, ScriptLocatorResolver]>;
63+
result?: ScriptLocator;
64+
}
65+
66+
interface LoadHookParams {
67+
scriptId: string;
68+
caller?: string;
69+
locator: NormalizedScriptLocator;
70+
loadScript: () => Promise<void>;
71+
handled?: boolean;
72+
}
73+
5874
/**
5975
* A manager to ease resolution, downloading and executing additional code from:
6076
* - arbitrary JavaScript scripts
@@ -118,9 +134,7 @@ export class ScriptManager extends EventEmitter {
118134
scriptId: string;
119135
caller?: string;
120136
}>(['params']),
121-
resolve: new AsyncSeriesHook<{ scriptId: string; caller?: string }, void>([
122-
'params',
123-
]),
137+
resolve: new AsyncSeriesWaterfallHook<ResolveHookParams>(['params']),
124138
afterResolve: new AsyncSeriesHook<
125139
{ scriptId: string; caller?: string },
126140
void
@@ -137,9 +151,7 @@ export class ScriptManager extends EventEmitter {
137151
{ scriptId: string; caller?: string },
138152
void
139153
>(['params']),
140-
load: new AsyncSeriesHook<{ scriptId: string; caller?: string }, void>([
141-
'params',
142-
]),
154+
load: new AsyncSeriesWaterfallHook<LoadHookParams>(['params']),
143155
afterLoad: new AsyncSeriesHook<{ scriptId: string; caller?: string }, void>(
144156
['params']
145157
),
@@ -326,29 +338,32 @@ export class ScriptManager extends EventEmitter {
326338
}
327339

328340
this.emit('resolving', { scriptId: finalScriptId, caller: finalCaller });
329-
330-
// Check if there are any taps in the resolve hook
331-
const _hasResolveHooks = this.hooks.resolve.taps.length > 0;
341+
const hasResolveHooks =
342+
this.hooks.resolve.taps && this.hooks.resolve.taps.length > 0;
332343

333344
let locator: ScriptLocator | undefined;
334345

335-
// if (hasResolveHooks) {
336-
await this.hooks.resolve.promise({
337-
scriptId: finalScriptId,
338-
caller: finalCaller,
339-
});
340-
// } else {
341-
for (const [, , resolve] of this.resolvers) {
342-
const resolvedLocator = await resolve(
343-
finalScriptId,
344-
finalCaller,
345-
referenceUrl
346-
);
347-
if (resolvedLocator) {
348-
locator = resolvedLocator;
349-
break;
346+
if (hasResolveHooks) {
347+
const params = await this.hooks.resolve.promise({
348+
scriptId: finalScriptId,
349+
caller: finalCaller,
350+
referenceUrl,
351+
resolvers: this.resolvers,
352+
});
353+
354+
locator = params.result;
355+
} else {
356+
for (const [, , resolve] of this.resolvers) {
357+
const resolvedLocator = await resolve(
358+
finalScriptId,
359+
finalCaller,
360+
referenceUrl
361+
);
362+
if (resolvedLocator) {
363+
locator = resolvedLocator;
364+
break;
365+
}
350366
}
351-
// }
352367
}
353368

354369
if (!locator) {
@@ -476,12 +491,27 @@ export class ScriptManager extends EventEmitter {
476491
});
477492
this.emit('loading', script.toObject());
478493

479-
const _hasLoadHooks = this.hooks.load.taps.length > 0;
480-
// if (hasLoadHooks) {
481-
await this.hooks.load.promise({ scriptId: scriptId, caller: caller });
482-
// } else {
483-
await this.loadScriptWithRetry(scriptId, script.locator);
484-
// }
494+
const hasLoadHooks =
495+
this.hooks.load.taps && this.hooks.load.taps.length > 0;
496+
497+
if (hasLoadHooks) {
498+
await this.hooks.load.promise({
499+
scriptId,
500+
caller,
501+
locator: script.locator,
502+
loadScript: async () => {
503+
await this.loadScriptWithRetry(scriptId, script.locator);
504+
},
505+
});
506+
507+
// If the hook didn't handle loading, use default loading
508+
// FIXME: Not sure if that's needed.
509+
// if (!params.handled) {
510+
// await this.loadScriptWithRetry(scriptId, script.locator);
511+
// }
512+
} else {
513+
await this.loadScriptWithRetry(scriptId, script.locator);
514+
}
485515

486516
await this.hooks.afterLoad.promise({
487517
scriptId: scriptId,

0 commit comments

Comments
 (0)