Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit be1166f

Browse filesBrowse files
cjihrigtargos
authored andcommitted
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: #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>
1 parent a9f9557 commit be1166f
Copy full SHA for be1166f

File tree

Expand file treeCollapse file tree

1 file changed

+14
-11
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+14
-11
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/esm/create_dynamic_module.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/create_dynamic_module.js
+14-11Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,27 @@ const { ArrayPrototype, JSON, Object } = primordials;
44

55
const debug = require('internal/util/debuglog').debuglog('esm');
66

7-
const createDynamicModule = (imports, exports, url = '', evaluate) => {
8-
debug('creating ESM facade for %s with exports: %j', url, exports);
9-
const names = ArrayPrototype.map(exports, (name) => `${name}`);
10-
11-
const source = `
12-
${ArrayPrototype.join(ArrayPrototype.map(imports, (impt, index) =>
13-
`import * as $import_${index} from ${JSON.stringify(impt)};
14-
import.meta.imports[${JSON.stringify(impt)}] = $import_${index};`), '\n')
7+
function createImport(impt, index) {
8+
const imptPath = JSON.stringify(impt);
9+
return `import * as $import_${index} from ${imptPath};
10+
import.meta.imports[${imptPath}] = $import_${index};`;
1511
}
16-
${ArrayPrototype.join(ArrayPrototype.map(names, (name) =>
17-
`let $${name};
12+
13+
function createExport(expt) {
14+
const name = `${expt}`;
15+
return `let $${name};
1816
export { $${name} as ${name} };
1917
import.meta.exports.${name} = {
2018
get: () => $${name},
2119
set: (v) => $${name} = v,
22-
};`), '\n')
20+
};`;
2321
}
2422

23+
const createDynamicModule = (imports, exports, url = '', evaluate) => {
24+
debug('creating ESM facade for %s with exports: %j', url, exports);
25+
const source = `
26+
${ArrayPrototype.join(ArrayPrototype.map(imports, createImport), '\n')}
27+
${ArrayPrototype.join(ArrayPrototype.map(exports, createExport), '\n')}
2528
import.meta.done();
2629
`;
2730
const { ModuleWrap, callbackMap } = internalBinding('module_wrap');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.