--build-snapshot


在进程退出时生成快照二进制文件并写入磁盘,之后可以使用 --snapshot-blob 加载。

🌐 Generates a snapshot blob when the process exits and writes it to disk, which can be loaded later with --snapshot-blob.

在构建快照时,如果未指定 --snapshot-blob,生成的 blob 默认会写入当前工作目录下的 snapshot.blob。否则,它将写入由 --snapshot-blob 指定的路径。

🌐 When building the snapshot, if --snapshot-blob is not specified, the generated blob will be written, by default, to snapshot.blob in the current working directory. Otherwise it will be written to the path specified by --snapshot-blob.

$ echo "globalThis.foo = 'I am from the snapshot'" > snapshot.js

# Run snapshot.js to initialize the application and snapshot the
# state of it into snapshot.blob.
$ node --snapshot-blob snapshot.blob --build-snapshot snapshot.js

$ echo "console.log(globalThis.foo)" > index.js

# Load the generated snapshot and start the application from index.js.
$ node --snapshot-blob snapshot.blob index.js
I am from the snapshot 

v8.startupSnapshot API 可以在快照构建时指定入口点,从而避免在反序列化时需要额外的入口脚本:

🌐 The v8.startupSnapshot API can be used to specify an entry point at snapshot building time, thus avoiding the need of an additional entry script at deserialization time:

$ echo "require('v8').startupSnapshot.setDeserializeMainFunction(() => console.log('I am from the snapshot'))" > snapshot.js
$ node --snapshot-blob snapshot.blob --build-snapshot snapshot.js
$ node --snapshot-blob snapshot.blob
I am from the snapshot 

欲了解更多信息,请查看 v8.startupSnapshot API 文档。

🌐 For more information, check out the v8.startupSnapshot API documentation.

快照目前仅支持在快照构建过程中加载单个入口点,该入口点可以加载内置模块,但不能加载其他用户自定义模块。用户可以在构建快照之前,使用他们选择的打包工具将应用打包成单个脚本。

🌐 The snapshot currently only supports loding a single entrypoint during the snapshot building process, which can load built-in modules, but not additional user-land modules. Users can bundle their applications into a single script with their bundler of choice before building a snapshot.

由于确保所有内置模块的可序列化性比较复杂,而这些模块也在不断增加,只有部分内置模块在快照构建过程中经过充分测试,确认可序列化。Node.js 核心测试套件会检查一些相当复杂的应用能否进行快照。被 被 Node.js 内置快照捕获 列出的内置模块被认为是受支持的。当快照构建器遇到无法序列化的内置模块时,可能会导致快照构建过程崩溃。在这种情况下,一个典型的解决方法是推迟该模块的加载,直到运行时,可以使用 v8.startupSnapshot.setDeserializeMainFunction()v8.startupSnapshot.addDeserializeCallback()。如果在快照构建过程中需要对额外模块进行序列化,请在 Node.js 问题跟踪器 中提交请求,并在 用户态快照的跟踪问题 中提供链接。

🌐 As it's complicated to ensure the serializablility of all built-in modules, which are also growing over time, only a subset of the built-in modules are well tested to be serializable during the snapshot building process. The Node.js core test suite checks that a few fairly complex applications can be snapshotted. The list of built-in modules being captured by the built-in snapshot of Node.js is considered supported. When the snapshot builder encounters a built-in module that cannot be serialized, it may crash the snapshot building process. In that case a typical workaround would be to delay loading that module until runtime, using either v8.startupSnapshot.setDeserializeMainFunction() or v8.startupSnapshot.addDeserializeCallback(). If serialization for an additional module during the snapshot building process is needed, please file a request in the Node.js issue tracker and link to it in the tracking issue for user-land snapshots.