--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.】
目前对运行时快照的支持是实验性的:
【Currently the support for run-time snapshot is experimental in that:】
- 快照中尚不支持用户级模块,因此一次只能生成单个文件的快照。不过,用户可以在构建快照之前使用自己选择的打包工具将应用打包成单个脚本。
- 只有内置模块的一个子集在快照中可用,尽管 Node.js 核心测试套件会检查一些相当复杂的应用是否可以被快照。更多模块的支持正在添加中。如果在构建快照时发生任何崩溃或错误行为,请在 Node.js 问题跟踪器 中提交报告,并在 用户空间快照的跟踪问题 中链接该报告。