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
2 changes: 2 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const { globalIgnores } = await importEslintTool('eslint/config');
const { default: js } = await importEslintTool('@eslint/js');
const { default: babelEslintParser } = await importEslintTool('@babel/eslint-parser');
const babelPluginSyntaxImportSource = resolveEslintTool('@babel/plugin-syntax-import-source');
const babelPluginImportDefer = resolveEslintTool('@babel/plugin-syntax-import-defer');
const { default: jsdoc } = await importEslintTool('eslint-plugin-jsdoc');
const { default: regexpPlugin } = await importEslintTool('eslint-plugin-regexp');
const { default: markdown } = await importEslintTool('@eslint/markdown');
Expand Down Expand Up @@ -105,6 +106,7 @@ export default [
babelOptions: {
plugins: [
babelPluginSyntaxImportSource,
babelPluginImportDefer,
],
},
requireConfigFile: false,
Expand Down
24 changes: 16 additions & 8 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {
kErrored,
kEvaluated,
kEvaluating,
kDeferPhase,
kEvaluationPhase,
kInstantiated,
kUninstantiated,
Expand Down Expand Up @@ -164,7 +165,7 @@ class ModuleJobBase {
debug(`ModuleJobBase.syncLink() ${this.url} -> ${request.specifier}`, job);
assert(!isPromise(job));
assert(job.module instanceof ModuleWrap);
if (request.phase === kEvaluationPhase) {
if (this.shouldRunModule(request.phase)) {
ArrayPrototypePush(evaluationDepJobs, job);
}
modules[idx] = job.module;
Expand Down Expand Up @@ -199,6 +200,13 @@ class ModuleJobBase {
}
}
}

shouldLinkModule(phase) {
return phase >= kDeferPhase;
}
shouldRunModule(phase) {
return phase === kEvaluationPhase;
}
}

/* A ModuleJob tracks the loading of a single Module, and the ModuleJobs of
Expand Down Expand Up @@ -227,7 +235,7 @@ class ModuleJob extends ModuleJobBase {
this.modulePromise = PromiseResolve(moduleOrModulePromise);
}

if (this.phase === kEvaluationPhase) {
if (this.shouldLinkModule(this.phase)) {
// Promise for the list of all dependencyJobs.
this.linked = this.link(requestType);
// This promise is awaited later anyway, so silence
Expand Down Expand Up @@ -279,7 +287,7 @@ class ModuleJob extends ModuleJobBase {
const dependencyJobPromise = this.loader.getOrCreateModuleJob(this.url, request, requestType);
const modulePromise = PromisePrototypeThen(dependencyJobPromise, (job) => {
debug(`ModuleJob.asyncLink() ${this.url} -> ${request.specifier}`, job);
if (request.phase === kEvaluationPhase) {
if (this.shouldRunModule(request.phase)) {
ArrayPrototypePush(evaluationDepJobs, job);
}
return job.modulePromise;
Expand Down Expand Up @@ -380,7 +388,7 @@ class ModuleJob extends ModuleJobBase {
}

runSync(parent) {
assert(this.phase === kEvaluationPhase);
assert(this.shouldRunModule(this.phase));
assert(this.module instanceof ModuleWrap);
let status = this.module.getStatus();

Expand Down Expand Up @@ -427,7 +435,7 @@ class ModuleJob extends ModuleJobBase {

async run(isEntryPoint = false) {
debug('ModuleJob.run()', this.module);
assert(this.phase === kEvaluationPhase);
assert(this.shouldRunModule(this.phase));
await this.#instantiate();
if (isEntryPoint) {
globalThis[entry_point_module_private_symbol] = this.module;
Expand Down Expand Up @@ -475,7 +483,7 @@ class ModuleJobSync extends ModuleJobBase {
assert(this.module instanceof ModuleWrap);
this.linked = undefined;
this.type = importAttributes.type;
if (phase === kEvaluationPhase) {
if (this.shouldLinkModule(phase)) {
this.linked = this.link(requestType);
}
}
Expand All @@ -494,7 +502,7 @@ class ModuleJobSync extends ModuleJobBase {
}

async run() {
assert(this.phase === kEvaluationPhase);
assert(this.shouldRunModule(this.phase));
// This path is hit by a require'd module that is imported again.
const status = this.module.getStatus();
debug('ModuleJobSync.run()', status, this.module);
Expand Down Expand Up @@ -523,7 +531,7 @@ class ModuleJobSync extends ModuleJobBase {

runSync(parent) {
debug('ModuleJobSync.runSync()', this.module);
assert(this.phase === kEvaluationPhase);
assert(this.shouldRunModule(this.phase));
// TODO(joyeecheung): add the error decoration logic from the async instantiate.
this.module.instantiate();
// If --experimental-print-required-tla is true, proceeds to evaluation even
Expand Down
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,
Comment thread
guybedford marked this conversation as resolved.
kEvaluationPhase = 3,
};

/**
Expand Down
25 changes: 25 additions & 0 deletions test/es-module/test-defer-import-eval.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Flags: --js-defer-import-eval

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

import '../common/index.mjs';
import * as assert from 'assert';

globalThis.eval_list = [];

import defer * as deferred from '../fixtures/es-modules/module-deferred-eval.mjs';

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

// 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.prop, 'newProp', { value: 15 }), TypeError);

assert.strictEqual(deferred.foo, 42);

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

// Clean-up
delete globalThis.eval_list;
9 changes: 9 additions & 0 deletions test/es-module/test-defer-import-with-module-tree.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Flags: --js-defer-import-eval

import '../common/index.mjs';

import defer * as deferred from '../fixtures/es-modules/module-with-module-tree.mjs';

console.log(deferred.bar);

delete globalThis.eval_list;
8 changes: 8 additions & 0 deletions test/fixtures/es-modules/module-deferred-eval.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if (!globalThis.eval_list) {
Comment thread
joyeecheung marked this conversation as resolved.
globalThis.eval_list = [];
}
globalThis.eval_list.push('defer-1');

export const foo = 42;

console.log('executed');
3 changes: 3 additions & 0 deletions test/fixtures/es-modules/module-with-module-tree.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './module-deferred-eval.mjs';

export const bar = 64;
16 changes: 16 additions & 0 deletions tools/eslint/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"@babel/core": "^8.0.0-rc.6",
"@babel/eslint-parser": "^8.0.0-rc.6",
"@babel/plugin-syntax-import-defer": "^8.0.0-rc.6",
"@babel/plugin-syntax-import-source": "^8.0.0-rc.6",
"@eslint/js": "^10.0.1",
"@eslint/markdown": "^8.0.2",
Expand Down
Loading