Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ ModulePhase to_phase_constant(ModuleImportPhase phase) {
switch (phase) {
case ModuleImportPhase::kEvaluation:
return kEvaluationPhase;
case ModuleImportPhase::kDefer:
return kDeferPhase;
case ModuleImportPhase::kSource:
return kSourcePhase;
default:
Expand Down Expand Up @@ -1682,6 +1684,7 @@ void ModuleWrap::CreatePerContextProperties(Local<Object> target,
V(Module::Status, kErrored);

V(ModulePhase, kEvaluationPhase);
V(ModulePhase, kDeferPhase);
V(ModulePhase, kSourcePhase);
#undef V
}
Expand Down
3 changes: 2 additions & 1 deletion src/module_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ enum HostDefinedOptions : int {

enum ModulePhase : int {
kSourcePhase = 1,
kEvaluationPhase = 2,
kDeferPhase = 2,
kEvaluationPhase = 3,
};

/**
Expand Down
8 changes: 8 additions & 0 deletions test/es-module/module-deferred-eval.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if (!globalThis.eval_list) {

Check failure on line 1 in test/es-module/module-deferred-eval.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Mandatory module "common" must be loaded before any other modules

Check failure on line 1 in test/es-module/module-deferred-eval.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Mandatory module "common" must be loaded

Check failure on line 1 in test/es-module/module-deferred-eval.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'if' statement can be replaced with a logical operator assignment with operator ||=
Copy link
Copy Markdown
Member

@joyeecheung joyeecheung Jun 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be moved into the test/fixtures folder, otherwise you get the linter complaints (and it gets to run by the python test runner).

globalThis.eval_list = [];
}
globalThis.eval_list.push('defer-1');

export const foo = 42;

console.log('executed');
20 changes: 20 additions & 0 deletions test/es-module/test-defer-import-eval.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Flags: --js-defer-import-eval

// Tests that defer import actually evaluates the imported module
// only when properties that it exports are accessed.

import * as assert from 'assert'

globalThis.eval_list = [];

import defer * as deferred from './module-deferred-eval.mjs'

assert.strictEqual(0, globalThis.eval_list.length);

// Attempts to define a property on the deferred module. This should
// trigger its execution, similar to accessing the `foo` property.
assert.throws(() => Object.defineProperty(deferred, 'newProp', {value: 15}), TypeError);
assert.equal(42, deferred.foo);

// Check that the module has been evaluated at this point.
assert.partialDeepStrictEqual(['defer-1'], globalThis.eval_list);
Loading