Skip to content

Commit c3bb922

Browse files
committed
sea: add test and update docs for import() with code cache
Signed-off-by: Ali Hassan <ali-hassan27@outlook.com>
1 parent 3b8d6c4 commit c3bb922

File tree

5 files changed

+58
-10
lines changed

5 files changed

+58
-10
lines changed

doc/api/single-executable-applications.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@ executable application is launched, instead of compiling the `main` script from
216216
scratch, Node.js would use the code cache to speed up the compilation, then
217217
execute the script, which would improve the startup performance.
218218

219-
**Note:** `import()` does not work when `useCodeCache` is `true`.
220-
221219
### Execution arguments
222220

223221
The `execArgv` field can be used to specify Node.js-specific
@@ -451,8 +449,9 @@ injected main script with the following properties:
451449

452450
<!-- TODO(joyeecheung): support and document module.registerHooks -->
453451

454-
When using `"mainFormat": "module"`, `import()` can be used to dynamically
455-
load built-in modules. Attempting to use `import()` to load modules from
452+
`import()` can be used to dynamically load built-in modules in both
453+
CommonJS and ESM (`"mainFormat": "module"`) single executable applications.
454+
Attempting to use `import()` to load modules from
456455
the file system will throw an error.
457456

458457
### Using native addons in the injected main script

src/node_sea.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,12 +666,6 @@ std::optional<std::string> GenerateCodeCache(std::string_view main_path,
666666
Local<UnboundModuleScript> unbound = module->GetUnboundModuleScript();
667667
cache.reset(ScriptCompiler::CreateCodeCache(unbound));
668668
} else {
669-
// TODO(RaisinTen): Using the V8 code cache prevents us from using
670-
// `import()` in the SEA code. Support it. Refs:
671-
// https://github.com/nodejs/node/pull/48191#discussion_r1213271430
672-
// TODO(joyeecheung): this likely has been fixed by
673-
// https://chromium-review.googlesource.com/c/v8/v8/+/5401780 - add a test
674-
// and update docs.
675669
LocalVector<String> parameters(
676670
isolate,
677671
{
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"main": "sea.js",
3+
"output": "sea",
4+
"useCodeCache": true,
5+
"disableExperimentalSEAWarning": true
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
(async () => {
2+
const assert = require('node:assert');
3+
4+
// Dynamic import of a built-in module should work even with code cache.
5+
const { strictEqual } = await import('node:assert');
6+
assert.strictEqual(strictEqual, assert.strictEqual);
7+
8+
// Dynamic import of another built-in module.
9+
const { join } = await import('node:path');
10+
assert.strictEqual(typeof join, 'function');
11+
12+
console.log('dynamic import with code cache works');
13+
})();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
// This tests that import() works in a CJS single executable application
4+
// when useCodeCache is true. A V8 fix (https://chromium-review.googlesource.com
5+
// /c/v8/v8/+/5401780) resolved the issue where code cache serialization
6+
// wiped host-defined options needed by HostImportModuleDynamically.
7+
8+
require('../common');
9+
10+
const {
11+
buildSEA,
12+
skipIfBuildSEAIsNotSupported,
13+
} = require('../common/sea');
14+
15+
skipIfBuildSEAIsNotSupported();
16+
17+
const tmpdir = require('../common/tmpdir');
18+
const fixtures = require('../common/fixtures');
19+
const { spawnSyncAndAssert } = require('../common/child_process');
20+
21+
tmpdir.refresh();
22+
23+
const outputFile = buildSEA(fixtures.path('sea', 'use-code-cache-dynamic-import'));
24+
25+
spawnSyncAndAssert(
26+
outputFile,
27+
[],
28+
{
29+
env: {
30+
NODE_DEBUG_NATIVE: 'SEA',
31+
...process.env,
32+
},
33+
},
34+
{
35+
stdout: 'dynamic import with code cache works\n',
36+
});

0 commit comments

Comments
 (0)