--build-snapshot
进程退出时生成快照 blob 并将其写入磁盘,稍后可以使用 --snapshot-blob
加载。
当构建快照时,如果不指定 --snapshot-blob
,则生成的 blob 会默认写入当前工作目录下的 snapshot.blob
。
否则会写入 --snapshot-blob
指定的路径。
$ echo "globalThis.foo = 'I am from the snapshot'" > snapshot.js
# 运行 snapshot.js 以初始化应用程序并将其
# 状态快照到 snapshot.blob。
$ node --snapshot-blob snapshot.blob --build-snapshot snapshot.js
$ echo "console.log(globalThis.foo)" > index.js
# 加载生成的快照并从 index.js 启动应用程序。
$ node --snapshot-blob snapshot.blob index.js
I am from the snapshot
v8.startupSnapshot
API 可用于在快照构建时指定入口点,从而避免在反序列化时需要额外的入口脚本:
$ 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 文档。
目前对运行时快照的支持是实验性的:
- 快照尚不支持用户级模块,因此只能对一个文件进行快照。 但是,用户可以在构建快照之前使用他们选择的捆绑器将他们的应用程序捆绑到一个脚本中。
- 只有一部分内置模块在快照中工作,尽管 Node.js 核心测试套件会检查一些相当复杂的应用程序是否可以被快照。 正在添加对更多模块的支持。 如果在构建快照时发生任何崩溃或错误行为,则请在 Node.js 问题跟踪器中提交报告,并在用户空间快照的跟踪问题中链接到它。
Generates a snapshot blob when the process exits and writes it to
disk, which can be loaded later with --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 intialize 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
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
For more information, check out the v8.startupSnapshot
API documentation.
Currently the support for run-time snapshot is experimental in that:
- 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.
- 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.