Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 3 additions & 4 deletions doc/api/single-executable-applications.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,6 @@ executable application is launched, instead of compiling the `main` script from
scratch, Node.js would use the code cache to speed up the compilation, then
execute the script, which would improve the startup performance.

**Note:** `import()` does not work when `useCodeCache` is `true`.

### Execution arguments

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

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

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

### Using native addons in the injected main script
Expand Down
6 changes: 0 additions & 6 deletions src/node_sea.cc
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,6 @@ std::optional<std::string> GenerateCodeCache(std::string_view main_path,
Local<UnboundModuleScript> unbound = module->GetUnboundModuleScript();
cache.reset(ScriptCompiler::CreateCodeCache(unbound));
} else {
// TODO(RaisinTen): Using the V8 code cache prevents us from using
// `import()` in the SEA code. Support it. Refs:
// https://github.com/nodejs/node/pull/48191#discussion_r1213271430
// TODO(joyeecheung): this likely has been fixed by
// https://chromium-review.googlesource.com/c/v8/v8/+/5401780 - add a test
// and update docs.
LocalVector<String> parameters(
isolate,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"main": "sea.js",
"output": "sea",
"useCodeCache": true,
"disableExperimentalSEAWarning": true
}
13 changes: 13 additions & 0 deletions test/fixtures/sea/use-code-cache-dynamic-import/sea.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(async () => {
const assert = require('node:assert');

// Dynamic import of a built-in module should work even with code cache.
const { strictEqual } = await import('node:assert');
assert.strictEqual(strictEqual, assert.strictEqual);

// Dynamic import of another built-in module.
const { join } = await import('node:path');
assert.strictEqual(typeof join, 'function');

console.log('dynamic import with code cache works');
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

// This tests that import() works in a CJS single executable application
// when useCodeCache is true. A V8 fix (https://chromium-review.googlesource.com
// /c/v8/v8/+/5401780) resolved the issue where code cache serialization
// wiped host-defined options needed by HostImportModuleDynamically.
Comment thread
thisalihassan marked this conversation as resolved.
Outdated

require('../common');

const {
buildSEA,
skipIfBuildSEAIsNotSupported,
} = require('../common/sea');

skipIfBuildSEAIsNotSupported();

const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const { spawnSyncAndAssert } = require('../common/child_process');

tmpdir.refresh();

const outputFile = buildSEA(fixtures.path('sea', 'use-code-cache-dynamic-import'));

spawnSyncAndAssert(
outputFile,
[],
{
env: {
NODE_DEBUG_NATIVE: 'SEA',
...process.env,
},
},
{
stdout: 'dynamic import with code cache works\n',
});
Loading