Skip to content

Commit 0438a5d

Browse files
committed
create_dynamic_module.js
1 parent b5656c5 commit 0438a5d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/internal/modules/esm/create_dynamic_module.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
1111
debug = fn;
1212
});
1313

14+
/**
15+
* Creates an import statement for a given module path and index.
16+
* @param {string} impt - The module path to import.
17+
* @param {number} index - The index of the import statement.
18+
*/
1419
function createImport(impt, index) {
1520
const imptPath = JSONStringify(impt);
1621
return `import * as $import_${index} from ${imptPath};
1722
import.meta.imports[${imptPath}] = $import_${index};`;
1823
}
1924

25+
/**
26+
* Creates an export for a given module.
27+
* @param {string} expt - The name of the export.
28+
*/
2029
function createExport(expt) {
2130
const name = `${expt}`;
2231
return `let $${name};
@@ -27,6 +36,17 @@ import.meta.exports.${name} = {
2736
};`;
2837
}
2938

39+
/**
40+
* Creates a dynamic module with the given imports, exports, URL, and evaluate function.
41+
* @param {string[]} imports - An array of imports.
42+
* @param {string[]} exports - An array of exports.
43+
* @param {string} [url=''] - The URL of the module.
44+
* @param {(reflect: DynamicModuleReflect) => void} evaluate - The function to evaluate the module.
45+
* @typedef {object} DynamicModuleReflect
46+
* @property {string[]} imports - The imports of the module.
47+
* @property {string[]} exports - The exports of the module.
48+
* @property {(cb: (reflect: DynamicModuleReflect) => void) => void} onReady - Callback to evaluate the module.
49+
*/
3050
const createDynamicModule = (imports, exports, url = '', evaluate) => {
3151
debug('creating ESM facade for %s with exports: %j', url, exports);
3252
const source = `
@@ -38,6 +58,7 @@ import.meta.done();
3858
const m = new ModuleWrap(`${url}`, undefined, source, 0, 0);
3959

4060
const readyfns = new SafeSet();
61+
/** @type {DynamicModuleReflect} */
4162
const reflect = {
4263
exports: { __proto__: null },
4364
onReady: (cb) => { readyfns.add(cb); },

0 commit comments

Comments
 (0)