mirror of
https://github.com/nodejs/node.git
synced 2025-05-08 16:29:55 +00:00

This moves the following utils into modules/esm/utils.js: - Code related to default conditions - The callbackMap (which is now created in the module instead of hanging off the module_wrap binding, since the C++ land does not need it). - Per-isolate module callbacks These are self-contained code that can be included into the built-in snapshot. PR-URL: https://github.com/nodejs/node/pull/45849 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
73 lines
1.8 KiB
JavaScript
73 lines
1.8 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
ArrayPrototypeJoin,
|
|
ArrayPrototypeMap,
|
|
JSONStringify,
|
|
ObjectCreate,
|
|
SafeSet,
|
|
} = primordials;
|
|
|
|
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
|
|
debug = fn;
|
|
});
|
|
|
|
function createImport(impt, index) {
|
|
const imptPath = JSONStringify(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 = `
|
|
${ArrayPrototypeJoin(ArrayPrototypeMap(imports, createImport), '\n')}
|
|
${ArrayPrototypeJoin(ArrayPrototypeMap(exports, createExport), '\n')}
|
|
import.meta.done();
|
|
`;
|
|
const { ModuleWrap } = internalBinding('module_wrap');
|
|
const m = new ModuleWrap(`${url}`, undefined, source, 0, 0);
|
|
|
|
const readyfns = new SafeSet();
|
|
const reflect = {
|
|
exports: ObjectCreate(null),
|
|
onReady: (cb) => { readyfns.add(cb); },
|
|
};
|
|
|
|
if (imports.length)
|
|
reflect.imports = ObjectCreate(null);
|
|
const { setCallbackForWrap } = require('internal/modules/esm/utils');
|
|
setCallbackForWrap(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;
|