Skip to content

Commit 84e6b6d

Browse files
committed
wip
1 parent 7ab93df commit 84e6b6d

File tree

3 files changed

+125
-15
lines changed

3 files changed

+125
-15
lines changed

apps/tester-app/index.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,18 @@ 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 ({ scriptId, caller, referenceUrl, resolvers, error }, callback) => {
73+
console.log('Resolving:', scriptId, caller);
74+
for (const [, , resolve] of resolvers) {
75+
const resolvedLocator = await resolve(scriptId, caller, referenceUrl);
76+
if (resolvedLocator) {
77+
console.log('Resolved locator:', resolvedLocator);
78+
callback({ result: resolvedLocator });
79+
return;
80+
}
7581
}
76-
console.log('ScriptManager.shared.hooks.resolve', scriptId, caller, error);
77-
callback();
82+
83+
callback(new Error('No locator found'));
7884
}
7985
);
8086

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

+24-10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ 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+
5866
/**
5967
* A manager to ease resolution, downloading and executing additional code from:
6068
* - arbitrary JavaScript scripts
@@ -118,9 +126,7 @@ export class ScriptManager extends EventEmitter {
118126
scriptId: string;
119127
caller?: string;
120128
}>(['params']),
121-
resolve: new AsyncSeriesHook<{ scriptId: string; caller?: string }, void>([
122-
'params',
123-
]),
129+
resolve: new AsyncSeriesWaterfallHook<ResolveHookParams>(['params']),
124130
afterResolve: new AsyncSeriesHook<
125131
{ scriptId: string; caller?: string },
126132
void
@@ -328,28 +334,36 @@ export class ScriptManager extends EventEmitter {
328334
this.emit('resolving', { scriptId: finalScriptId, caller: finalCaller });
329335

330336
// Check if there are any taps in the resolve hook
331-
const _hasResolveHooks = this.hooks.resolve.taps.length > 0;
337+
const hasResolveHooks = this.hooks.resolve.taps.length > 0;
332338

333339
let locator: ScriptLocator | undefined;
334340

335-
// if (hasResolveHooks) {
336-
await this.hooks.resolve.promise({
337-
scriptId: finalScriptId,
338-
caller: finalCaller,
339-
});
341+
// // if (hasResolveHooks) {
342+
// const { result } = await this.hooks.resolve.promise({
343+
// scriptId: finalScriptId,
344+
// caller: finalCaller,
345+
// referenceUrl,
346+
// resolvers: this.resolvers,
347+
// });
348+
349+
// // if (result) {
350+
// locator = result;
351+
// console.log('Resolved locator1:', locator);
352+
// // }
340353
// } else {
341354
for (const [, , resolve] of this.resolvers) {
342355
const resolvedLocator = await resolve(
343356
finalScriptId,
344357
finalCaller,
345358
referenceUrl
346359
);
360+
347361
if (resolvedLocator) {
348362
locator = resolvedLocator;
349363
break;
350364
}
351-
// }
352365
}
366+
// }
353367

354368
if (!locator) {
355369
const error = new Error(

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

+90
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import NativeScriptManager, {
33
} from '../NativeScriptManager.js';
44
import { Script } from '../Script.js';
55
import { ScriptManager } from '../ScriptManager.js';
6+
import { getWebpackContext } from '../getWebpackContext.js';
67

78
jest.mock('../NativeScriptManager', () => ({
89
loadScript: jest.fn(),
@@ -1176,6 +1177,95 @@ describe('ScriptManagerAPI', () => {
11761177
expect.any(Error)
11771178
);
11781179
});
1180+
1181+
it('should allow resolve hook to handle resolution with custom logic', async () => {
1182+
const hookOrder: string[] = [];
1183+
const customScriptId = 'custom-script';
1184+
const customCaller = 'custom-caller';
1185+
const customReferenceUrl = 'http://reference.url';
1186+
1187+
ScriptManager.shared.hooks.beforeResolve.tap(
1188+
'test-before',
1189+
({ scriptId, caller }) => {
1190+
hookOrder.push('beforeResolve');
1191+
return { scriptId, caller };
1192+
}
1193+
);
1194+
1195+
ScriptManager.shared.hooks.resolve.tapAsync(
1196+
'test-resolve',
1197+
async (params, callback) => {
1198+
expect(params.scriptId).toBe(customScriptId);
1199+
expect(params.caller).toBe(customCaller);
1200+
expect(params.referenceUrl).toBe(customReferenceUrl);
1201+
expect(params.resolvers).toBeDefined();
1202+
expect(Array.isArray(params.resolvers)).toBe(true);
1203+
1204+
hookOrder.push('resolve');
1205+
for (const [, , resolve] of params.resolvers) {
1206+
try {
1207+
const resolvedLocator = await resolve(
1208+
params.scriptId,
1209+
params.caller,
1210+
params.referenceUrl
1211+
);
1212+
1213+
if (resolvedLocator) {
1214+
callback(null, {
1215+
...params,
1216+
result: resolvedLocator,
1217+
});
1218+
return;
1219+
}
1220+
} catch (error) {
1221+
if (error instanceof Error) {
1222+
callback(error);
1223+
} else {
1224+
callback(new Error('Unknown error occurred'));
1225+
}
1226+
return;
1227+
}
1228+
}
1229+
callback(new Error('No locator found'));
1230+
}
1231+
);
1232+
1233+
ScriptManager.shared.hooks.afterResolve.tap(
1234+
'test-after',
1235+
({ scriptId, caller }) => {
1236+
expect(scriptId).toBe(customScriptId);
1237+
expect(caller).toBe(customCaller);
1238+
hookOrder.push('afterResolve');
1239+
}
1240+
);
1241+
1242+
// Add a resolver that should be called by the hook
1243+
ScriptManager.shared.addResolver(async () => {
1244+
hookOrder.push('resolver');
1245+
return {
1246+
url: Script.getRemoteURL('http://domain.ext/script.js'),
1247+
cache: true,
1248+
};
1249+
});
1250+
1251+
const script = await ScriptManager.shared.resolveScript(
1252+
customScriptId,
1253+
customCaller,
1254+
getWebpackContext(),
1255+
customReferenceUrl
1256+
);
1257+
expect(script.locator.url).toBe(
1258+
'http://domain.ext/script.js.chunk.bundle'
1259+
);
1260+
expect(hookOrder).toEqual([
1261+
'beforeResolve',
1262+
'resolve',
1263+
'resolver',
1264+
'afterResolve',
1265+
]);
1266+
1267+
ScriptManager.shared.removeAllResolvers();
1268+
});
11791269
});
11801270

11811271
describe('loading script hooks lifecycle', () => {

0 commit comments

Comments
 (0)