Skip to content

Commit

Permalink
v8cache experimental flag
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Dec 6, 2018
1 parent 8032a35 commit fd312d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Options:
-S, --no-source-map Skip source map output
-e, --external [mod] Skip bundling 'mod'. Can be used many times
-q, --quiet Disable build summaries / non-error outputs
--v8-cache Emit a build using the v8 compile cache
`;

let args;
Expand All @@ -30,7 +31,8 @@ try {
"--no-source-map": Boolean,
"-S": "--no-source-map",
"--quiet": Boolean,
"-q": "--quiet"
"-q": "--quiet",
"--v8-cache": Boolean
});
} catch (e) {
if (e.message.indexOf("Unknown or unexpected option") === -1) throw e;
Expand Down Expand Up @@ -123,7 +125,8 @@ switch (args._[0]) {
{
minify: !args["--no-minify"] && !run,
externals: args["--external"],
sourceMap: !args["--no-source-map"]
sourceMap: !args["--no-source-map"],
v8cache: args["--v8-cache"]
}
);
ncc.then(
Expand Down
23 changes: 21 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ module.exports = async (
externals = [],
minify = false,
sourceMap = false,
filename = "index.js"
filename = "index.js",
v8cache = false
} = {}
) => {
const shebangMatch = fs.readFileSync(resolve.sync(entry)).toString().match(shebangRegEx);
const mfs = new MemoryFS();
const assetNames = Object.create(null);
assetNames[filename] = true;
if (sourceMap)
assetNames[filename + '.map'] = true;
if (v8cache)
assetNames[filename + '.cache'] = assetNames[filename + '.cache.js'] = true;
const assets = Object.create(null);
const compiler = webpack({
entry,
Expand Down Expand Up @@ -212,12 +218,25 @@ module.exports = async (
return { code, map, assets };
return { code: result.code, map: result.map, assets };
})
.then(({ code, map, assets }) => {
if (!v8cache)
return { code, map, assets };
const { Script } = require('vm');
assets[filename + '.cache'] = new Script(code).createCachedData();
assets[filename + '.cache.js'] = code;
assets[filename + '.map'] = map;
code = `const { readFileSync } = require('fs'), { Script } = require('vm');\n` +
`const source = readFileSync(__dirname + '/${filename}.cache.js').toString(), cachedData = readFileSync(__dirname + '/${filename}.cache');\n` +
`new Script(source, { cachedData }).runInNewContext(Object.assign({}, global, { module, exports, require, __filename, __dirname }));\n`;
if (map) map = {};
return { code, map, assets };
})
.then(({ code, map, assets}) => {
if (!shebangMatch)
return { code, map, assets };
code = shebangMatch[0] + code;
// add a line offset to the sourcemap
if (map)
if (map && map.mappings)
map.mappings = ";" + map.mappings;
return { code, map, assets };
})
Expand Down

0 comments on commit fd312d2

Please sign in to comment.