esm: refactor createDynamicModule()
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>
This commit is contained in:
parent
77b9ce57e2
commit
3293bbf77a
@ -4,24 +4,27 @@ const { ArrayPrototype, JSON, Object } = primordials;
|
||||
|
||||
const debug = require('internal/util/debuglog').debuglog('esm');
|
||||
|
||||
const createDynamicModule = (imports, exports, url = '', evaluate) => {
|
||||
debug('creating ESM facade for %s with exports: %j', url, exports);
|
||||
const names = ArrayPrototype.map(exports, (name) => `${name}`);
|
||||
|
||||
const source = `
|
||||
${ArrayPrototype.join(ArrayPrototype.map(imports, (impt, index) =>
|
||||
`import * as $import_${index} from ${JSON.stringify(impt)};
|
||||
import.meta.imports[${JSON.stringify(impt)}] = $import_${index};`), '\n')
|
||||
function createImport(impt, index) {
|
||||
const imptPath = JSON.stringify(impt);
|
||||
return `import * as $import_${index} from ${imptPath};
|
||||
import.meta.imports[${imptPath}] = $import_${index};`;
|
||||
}
|
||||
${ArrayPrototype.join(ArrayPrototype.map(names, (name) =>
|
||||
`let $${name};
|
||||
|
||||
function createExport(expt) {
|
||||
const name = `${expt}`;
|
||||
return `let $${name};
|
||||
export { $${name} as ${name} };
|
||||
import.meta.exports.${name} = {
|
||||
get: () => $${name},
|
||||
set: (v) => $${name} = v,
|
||||
};`), '\n')
|
||||
};`;
|
||||
}
|
||||
|
||||
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');
|
||||
|
Loading…
x
Reference in New Issue
Block a user