Skip to content

Commit 2915765

Browse files
committed
module: enable existing machinery for deferred import of static modules
This commit enables deferred import for statically imported modules, by using the corresponding functionality in V8. It also adds a single smoke test, so more test coverage should be added. Refs: https://github.com/tc39/proposal-defer-import-eval Signed-off-by: Maya Lekova <maya@igalia.com>
1 parent ba82a41 commit 2915765

7 files changed

Lines changed: 65 additions & 1 deletion

File tree

β€Žeslint.config.mjsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const { globalIgnores } = await importEslintTool('eslint/config');
1919
const { default: js } = await importEslintTool('@eslint/js');
2020
const { default: babelEslintParser } = await importEslintTool('@babel/eslint-parser');
2121
const babelPluginSyntaxImportSource = resolveEslintTool('@babel/plugin-syntax-import-source');
22+
const babelPluginImportDefer = resolveEslintTool('@babel/plugin-syntax-import-defer');
2223
const { default: jsdoc } = await importEslintTool('eslint-plugin-jsdoc');
2324
const { default: regexpPlugin } = await importEslintTool('eslint-plugin-regexp');
2425
const { default: markdown } = await importEslintTool('@eslint/markdown');
@@ -105,6 +106,7 @@ export default [
105106
babelOptions: {
106107
plugins: [
107108
babelPluginSyntaxImportSource,
109+
babelPluginImportDefer,
108110
],
109111
},
110112
requireConfigFile: false,

β€Žsrc/module_wrap.ccβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,8 @@ ModulePhase to_phase_constant(ModuleImportPhase phase) {
555555
switch (phase) {
556556
case ModuleImportPhase::kEvaluation:
557557
return kEvaluationPhase;
558+
case ModuleImportPhase::kDefer:
559+
return kDeferPhase;
558560
case ModuleImportPhase::kSource:
559561
return kSourcePhase;
560562
default:
@@ -1682,6 +1684,7 @@ void ModuleWrap::CreatePerContextProperties(Local<Object> target,
16821684
V(Module::Status, kErrored);
16831685

16841686
V(ModulePhase, kEvaluationPhase);
1687+
V(ModulePhase, kDeferPhase);
16851688
V(ModulePhase, kSourcePhase);
16861689
#undef V
16871690
}

β€Žsrc/module_wrap.hβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ enum HostDefinedOptions : int {
3535

3636
enum ModulePhase : int {
3737
kSourcePhase = 1,
38-
kEvaluationPhase = 2,
38+
kDeferPhase = 2,
39+
kEvaluationPhase = 3,
3940
};
4041

4142
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Flags: --js-defer-import-eval
2+
3+
// Tests that defer import actually evaluates the imported module
4+
// only when properties that it exports are accessed.
5+
6+
import '../common/index.mjs';
7+
import * as assert from 'assert';
8+
9+
globalThis.eval_list = [];
10+
11+
import defer * as deferred from '../fixtures/es-modules/module-deferred-eval.mjs';
12+
13+
assert.strictEqual(globalThis.eval_list.length, 0);
14+
15+
// Attempts to define a property on the deferred module. This should
16+
// trigger its execution, similar to accessing the `foo` property.
17+
// assert.throws(() => Object.defineProperty(deferred, 'newProp', { value: 15 }), TypeError);
18+
assert.strictEqual(deferred.foo, 42);
19+
20+
// Check that the module has been evaluated at this point.
21+
assert.partialDeepStrictEqual(['defer-1'], globalThis.eval_list);
22+
23+
// Clean-up
24+
delete globalThis.eval_list;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if (!globalThis.eval_list) {
2+
globalThis.eval_list = [];
3+
}
4+
globalThis.eval_list.push('defer-1');
5+
6+
export const foo = 42;
7+
8+
console.log('executed');

β€Žtools/eslint/package-lock.jsonβ€Ž

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žtools/eslint/package.jsonβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"dependencies": {
66
"@babel/core": "^8.0.0-rc.6",
77
"@babel/eslint-parser": "^8.0.0-rc.6",
8+
"@babel/plugin-syntax-import-defer": "^7.29.7",
89
"@babel/plugin-syntax-import-source": "^8.0.0-rc.6",
910
"@eslint/js": "^10.0.1",
1011
"@eslint/markdown": "^8.0.2",

0 commit comments

Comments
Β (0)