mirror of
https://github.com/nodejs/node.git
synced 2025-05-05 22:29:54 +00:00

This commit refactors createDynamicModule() for readability: - The map() callback functions are named and moved to a higher scope. - The two export map() loops are combined. - JSON.stringify() is only called once per import. PR-URL: https://github.com/nodejs/node/pull/27809 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
65 lines
1.6 KiB
JavaScript
65 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const { ArrayPrototype, JSON, Object } = primordials;
|
|
|
|
const debug = require('internal/util/debuglog').debuglog('esm');
|
|
|
|
function createImport(impt, index) {
|
|
const imptPath = JSON.stringify(impt);
|
|
return `import * as $import_${index} from ${imptPath};
|
|
import.meta.imports[${imptPath}] = $import_${index};`;
|
|
}
|
|
|
|
function createExport(expt) {
|
|
const name = `${expt}`;
|
|
return `let $${name};
|
|
export { $${name} as ${name} };
|
|
import.meta.exports.${name} = {
|
|
get: () => $${name},
|
|
set: (v) => $${name} = v,
|
|
};`;
|
|
}
|
|
|
|
const createDynamicModule = (imports, exports, url = '', evaluate) => {
|
|
debug('creating ESM facade for %s with exports: %j', url, exports);
|
|
const source = `
|
|
${ArrayPrototype.join(ArrayPrototype.map(imports, createImport), '\n')}
|
|
${ArrayPrototype.join(ArrayPrototype.map(exports, createExport), '\n')}
|
|
import.meta.done();
|
|
`;
|
|
const { ModuleWrap, callbackMap } = internalBinding('module_wrap');
|
|
const m = new ModuleWrap(source, `${url}`);
|
|
|
|
const readyfns = new Set();
|
|
const reflect = {
|
|
exports: Object.create(null),
|
|
onReady: (cb) => { readyfns.add(cb); },
|
|
};
|
|
|
|
if (imports.length)
|
|
reflect.imports = Object.create(null);
|
|
|
|
callbackMap.set(m, {
|
|
initializeImportMeta: (meta, wrap) => {
|
|
meta.exports = reflect.exports;
|
|
if (reflect.imports)
|
|
meta.imports = reflect.imports;
|
|
meta.done = () => {
|
|
evaluate(reflect);
|
|
reflect.onReady = (cb) => cb(reflect);
|
|
for (const fn of readyfns) {
|
|
readyfns.delete(fn);
|
|
fn(reflect);
|
|
}
|
|
};
|
|
},
|
|
});
|
|
|
|
return {
|
|
module: m,
|
|
reflect,
|
|
};
|
|
};
|
|
|
|
module.exports = createDynamicModule;
|