mirror-web/_src/babel-njs/hooks.mjs
Miao Wang 88adaa9ba4 transform non-default imports and exports using babel
since they are not supported by njs.

polyfill also added
2024-04-21 13:17:19 +08:00

34 lines
816 B
JavaScript

const commonJSHooksKey =
"transform-njs-module/customWrapperPlugin";
export function defineCommonJSHook(file, hook) {
let hooks = file.get(commonJSHooksKey);
if (!hooks) file.set(commonJSHooksKey, (hooks = []));
hooks.push(hook);
}
function findMap(arr, cb) {
if (arr) {
for (const el of arr) {
const res = cb(el);
if (res != null) return res;
}
}
}
export function makeInvokers(file){
const hooks = file.get(commonJSHooksKey);
return {
getWrapperPayload(...args) {
return findMap(hooks, hook => hook.getWrapperPayload?.(...args));
},
wrapReference(...args) {
return findMap(hooks, hook => hook.wrapReference?.(...args));
},
buildRequireWrapper(...args) {
return findMap(hooks, hook => hook.buildRequireWrapper?.(...args));
},
};
}