GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: cache synchronous module jobs before linking · nodejs/node@f74beb5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/modules/esm
    • module_job.js
    • module_map.js
  • test
    • es-module
      • test-require-module-cycle-cjs-esm-esm.js
    • fixtures/es-modules/cjs-esm-esm-cycle
      • a.mjs
      • b.mjs
      • c.cjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/module_job.js
+25-14Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -295,22 +295,33 @@ class ModuleJobSync extends ModuleJobBase {
295295
#loader = null;
296296
constructor(loader, url, importAttributes, moduleWrap, isMain, inspectBrk) {
297297
super(url, importAttributes, moduleWrap, isMain, inspectBrk, true);
298-
assert(this.module instanceof ModuleWrap);
299298
this.#loader = loader;
300-
const moduleRequests = this.module.getModuleRequests();
301-
// Specifiers should be aligned with the moduleRequests array in order.
302-
const specifiers = Array(moduleRequests.length);
303-
const modules = Array(moduleRequests.length);
304-
const jobs = Array(moduleRequests.length);
305-
for (let i = 0; i < moduleRequests.length; ++i) {
306-
const { specifier, attributes } = moduleRequests[i];
307-
const job = this.#loader.getModuleJobForRequire(specifier, url, attributes);
308-
specifiers[i] = specifier;
309-
modules[i] = job.module;
310-
jobs[i] = job;
299+
300+
assert(this.module instanceof ModuleWrap);
301+
// Store itself into the cache first before linking in case there are circular
302+
// references in the linking.
303+
loader.loadCache.set(url, importAttributes.type, this);
304+
305+
try {
306+
const moduleRequests = this.module.getModuleRequests();
307+
// Specifiers should be aligned with the moduleRequests array in order.
308+
const specifiers = Array(moduleRequests.length);
309+
const modules = Array(moduleRequests.length);
310+
const jobs = Array(moduleRequests.length);
311+
for (let i = 0; i < moduleRequests.length; ++i) {
312+
const { specifier, attributes } = moduleRequests[i];
313+
const job = this.#loader.getModuleJobForRequire(specifier, url, attributes);
314+
specifiers[i] = specifier;
315+
modules[i] = job.module;
316+
jobs[i] = job;
317+
}
318+
this.module.link(specifiers, modules);
319+
this.linked = jobs;
320+
} finally {
321+
// Restore it - if it succeeds, we'll reset in the caller; Otherwise it's
322+
// not cached and if the error is caught, subsequent attempt would still fail.
323+
loader.loadCache.delete(url, importAttributes.type);
311324
}
312-
this.module.link(specifiers, modules);
313-
this.linked = jobs;
314325
}
315326

316327
get modulePromise() {
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/module_map.js
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ class LoadCache extends SafeMap {
114114
validateString(type, 'type');
115115
return super.get(url)?.[type] !== undefined;
116116
}
117+
delete(url, type = kImplicitAssertType) {
118+
const cached = super.get(url);
119+
if (cached) {
120+
cached[type] = undefined;
121+
}
122+
}
117123
}
118124

119125
module.exports = {
Collapse file

‎test/es-module/test-require-module-cycle-cjs-esm-esm.js‎

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Flags: --experimental-require-module
2+
'use strict';
3+
4+
// This tests that ESM <-> ESM cycle is allowed in a require()-d graph.
5+
const common = require('../common');
6+
const cycle = require('../fixtures/es-modules/cjs-esm-esm-cycle/c.cjs');
7+
8+
common.expectRequiredModule(cycle, { b: 5 });
Collapse file

‎test/fixtures/es-modules/cjs-esm-esm-cycle/a.mjs‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { b } from './b.mjs';
Collapse file

‎test/fixtures/es-modules/cjs-esm-esm-cycle/b.mjs‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import './a.mjs'
2+
export const b = 5;
Collapse file

‎test/fixtures/es-modules/cjs-esm-esm-cycle/c.cjs‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./a.mjs');

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.