--build-snapshot


稳定性: 1 - 实验性的

¥Stability: 1 - Experimental

进程退出时生成快照 blob 并将其写入磁盘,稍后可以使用 --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.

目前对运行时快照的支持是实验性的:

¥Currently the support for run-time snapshot is experimental in that:

  1. 快照尚不支持用户级模块,因此只能对一个文件进行快照。但是,用户可以在构建快照之前使用他们选择的打包器将他们的应用打包到一个脚本中。

    ¥User-land modules are not yet supported in the snapshot, so only one single file can be snapshotted. Users can bundle their applications into a single script with their bundler of choice before building a snapshot, however.

  2. 只有一部分内置模块在快照中工作,尽管 Node.js 核心测试套件会检查一些相当复杂的应用是否可以被快照。正在添加对更多模块的支持。如果在构建快照时发生任何崩溃或错误行为,请在 Node.js 问题跟踪器 中提交报告并在 用户态快照的跟踪问题 中链接到它。

    ¥Only a subset of the built-in modules work in the snapshot, though the Node.js core test suite checks that a few fairly complex applications can be snapshotted. Support for more modules are being added. If any crashes or buggy behaviors occur when building a snapshot, please file a report in the Node.js issue tracker and link to it in the tracking issue for user-land snapshots.