diff --git a/README.md b/README.md index 0ea720c..e7517ea 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,11 @@ nexe.compile({ resourceRoot: [ 'path/' ], // where to embed the resourceFiles. flags: true, // use this for applications that need command line flags. jsFlags: "--use_strict", // v8 flags + startupSnapshot: 'path/to/snapshot.js', // when you want to specify a script to be + // added to V8's startup snapshot. This V8 + // feature deserializes a heap to save startup time. + // More information in this blog post: + // http://v8project.blogspot.de/2015/09/custom-startup-snapshots.html framework: "node" // node, nodejs, or iojs }, function(err) { if(err) { diff --git a/lib/exe.js b/lib/exe.js index 20bd95a..b8ae6e2 100644 --- a/lib/exe.js +++ b/lib/exe.js @@ -162,7 +162,7 @@ exports.compile = function(options, complete) { */ function monkeyPatchNodeConfig(next) { - _monkeyPatchNodeConfig(nodeCompiler, next); + _monkeyPatchNodeConfig(nodeCompiler, next, options); }, /** @@ -478,7 +478,7 @@ function _getNodeCompiler(nodeFileDir, nodeConfigureArgs, nodeMakeArgs, nodeVCBu if (!nodeVCBuildArgs || !nodeVCBuildArgs.length) { nodeVCBuildArgs = [ "nosign" ]; // "release" is already the default config value in VCBuild.bat } - + // spawn a vcbuild process with our custom enviroment. var vcbuild = spawn("vcbuild.bat", nodeVCBuildArgs, { cwd: dir, @@ -617,7 +617,7 @@ function _getNodeCompiler(nodeFileDir, nodeConfigureArgs, nodeMakeArgs, nodeVCBu /** */ -function _monkeyPatchNodeConfig(compiler, complete) { +function _monkeyPatchNodeConfig(compiler, complete, options) { async.waterfall([ /** * monkeypatch the gyp file to include the nexe.js and nexeres.js files @@ -626,6 +626,14 @@ function _monkeyPatchNodeConfig(compiler, complete) { _monkeyPatchGyp(compiler, next) }, + /** + * patch the configure file to allow for custom startup snapshots + */ + + function(next) { + _monkeyPatchConfigure(compiler, next, options); + }, + /** * monkeypatch main entry point */ @@ -655,6 +663,35 @@ function _monkeyPatchGyp(compiler, complete) { ) } + +/** + * patch the configure file to allow for custom startup snapshots + */ + +function _monkeyPatchConfigure(compiler, complete, options) { + var configurePath = path.join(compiler.dir, "configure"); + var snapshotPath = options.startupSnapshot; + + if (snapshotPath != null) { + _log('monkey patching configure file'); + snapshotPath = path.join(process.cwd(), snapshotPath); + _monkeypatch( + configurePath, + function(content) { + return ~content.indexOf("embed_script"); + }, + function(content, next) { + next(null, content.replace( + "def configure_v8(o):", + `def configure_v8(o):\n o['variables']['embed_script'] = '${snapshotPath}'\n o['variables']['warmup_script'] = '${snapshotPath}'`)); + }, + complete + ) + } + + return complete(); +} + /** */