Skip to content

Commit 702c744

Browse files
thisalihassanaduh95
authored andcommitted
sea: add test and update docs for import() with code cache
Signed-off-by: Ali Hassan <ali-hassan27@outlook.com> PR-URL: #62678 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 8e79b1f commit 702c744

5 files changed

Lines changed: 56 additions & 10 deletions

File tree

‎doc/api/single-executable-applications.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,6 @@ executable application is launched, instead of compiling the `main` script from
352352
scratch, Node.js would use the code cache to speed up the compilation, then
353353
execute the script, which would improve the startup performance.
354354
355-
**Note:** `import()` does not work when `useCodeCache` is `true`.
356-
357355
### Execution arguments
358356
359357
The `execArgv` field can be used to specify Node.js-specific
@@ -587,8 +585,9 @@ injected main script with the following properties:
587585
588586
<!-- TODO(joyeecheung): support and document module.registerHooks -->
589587
590-
When using `"mainFormat": "module"`, `import()` can be used to dynamically
591-
load built-in modules. Attempting to use `import()` to load modules from
588+
`import()` can be used to dynamically load built-in modules in both
589+
CommonJS and ESM (`"mainFormat": "module"`) single executable applications.
590+
Attempting to use `import()` to load modules from
592591
the file system will throw an error.
593592
594593
### 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
@@ -752,12 +752,6 @@ std::optional<std::string> GenerateCodeCache(std::string_view main_path,
752752
Local<UnboundModuleScript> unbound = module->GetUnboundModuleScript();
753753
cache.reset(ScriptCompiler::CreateCodeCache(unbound));
754754
} else {
755-
// TODO(RaisinTen): Using the V8 code cache prevents us from using
756-
// `import()` in the SEA code. Support it. Refs:
757-
// https://github.com/nodejs/node/pull/48191#discussion_r1213271430
758-
// TODO(joyeecheung): this likely has been fixed by
759-
// https://chromium-review.googlesource.com/c/v8/v8/+/5401780 - add a test
760-
// and update docs.
761755
LocalVector<String> parameters(
762756
isolate,
763757
{
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
// This tests that import() works in a CJS single executable application
4+
// when useCodeCache is true.
5+
6+
require('../common');
7+
8+
const {
9+
buildSEA,
10+
skipIfBuildSEAIsNotSupported,
11+
} = require('../common/sea');
12+
13+
skipIfBuildSEAIsNotSupported();
14+
15+
const tmpdir = require('../common/tmpdir');
16+
const fixtures = require('../common/fixtures');
17+
const { spawnSyncAndAssert } = require('../common/child_process');
18+
19+
tmpdir.refresh();
20+
21+
const outputFile = buildSEA(fixtures.path('sea', 'use-code-cache-dynamic-import'));
22+
23+
spawnSyncAndAssert(
24+
outputFile,
25+
[],
26+
{
27+
env: {
28+
NODE_DEBUG_NATIVE: 'SEA',
29+
...process.env,
30+
},
31+
},
32+
{
33+
stdout: 'dynamic import with code cache works\n',
34+
});

0 commit comments

Comments
 (0)