Skip to content

Commit

Permalink
Merge branch 'master' of github.com:zeit/ncc
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Jan 27, 2019
2 parents e109bdc + 1c0ae20 commit 596c10d
Show file tree
Hide file tree
Showing 10 changed files with 496 additions and 471 deletions.
64 changes: 47 additions & 17 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ async function main() {
const { code: cli, assets: cliAssets } = await ncc(
__dirname + "/../src/cli",
{
filename: "cli.js",
externals: ["./index.js"],
minify: true
minify: true,
v8cache: true
}
);

const { code: index, assets: indexAssets } = await ncc(
__dirname + "/../src/index",
{
Expand All @@ -22,36 +25,44 @@ async function main() {
// bundle, webpack (and therefore ncc) cannot currently bundle
// chokidar, which is quite convenient
externals: ["chokidar"],
minify: true
filename: "index.js",
minify: true,
v8cache: true
}
);

const { code: nodeLoader, assets: nodeLoaderAssets } = await ncc(
__dirname + "/../src/loaders/node-loader",
{ minify: true }
{
filename: "node-loader.js",
minify: true,
v8cache: true
}
);

const { code: relocateLoader, assets: relocateLoaderAssets } = await ncc(
__dirname + "/../src/loaders/relocate-loader",
{ minify: true }
{ filename: "relocate-loader.js", minify: true, v8cache: true }
);

const { code: shebangLoader, assets: shebangLoaderAssets } = await ncc(
__dirname + "/../src/loaders/shebang-loader",
{ minify: true }
{ filename: "shebang-loader.js", minify: true, v8cache: true }
);

const { code: tsLoader, assets: tsLoaderAssets } = await ncc(
__dirname + "/../src/loaders/ts-loader",
{
externals: ["typescript"],
minify: true
filename: "ts-loader.js",
minify: true,
v8cache: true
}
);

const { code: sourcemapSupport, assets: sourcemapAssets } = await ncc(
require.resolve("source-map-support/register"),
{ minfiy: true }
{ filename: "source-register.js", minfiy: true, v8cache: true }
);

const { code: typescript, assets: typescriptAssets } = await ncc(
Expand All @@ -60,19 +71,38 @@ async function main() {
);

// detect unexpected asset emissions from core build
if (
Object.keys(cliAssets).length ||
Object.keys(indexAssets).some(asset => !asset.startsWith('locales/')) ||
Object.keys(nodeLoaderAssets).length ||
Object.keys(relocateLoaderAssets).length ||
Object.keys(shebangLoaderAssets).length ||
Object.keys(tsLoaderAssets).length ||
Object.keys(sourcemapAssets).length ||
Object.keys(typescriptAssets).some(asset => !asset.startsWith('lib/'))
) {
const unknownAssets = [
...Object.keys(cliAssets),
...Object.keys(indexAssets).filter(asset => !asset.startsWith('locales/')),
...Object.keys(nodeLoaderAssets),
...Object.keys(relocateLoaderAssets),
...Object.keys(shebangLoaderAssets),
...Object.keys(tsLoaderAssets).filter(asset => !asset.startsWith('lib/')),
...Object.keys(sourcemapAssets),
...Object.keys(typescriptAssets).filter(asset => !asset.startsWith('lib/'))
].filter(asset => !asset.endsWith('.js.cache') && !asset.endsWith('.cache.js'));

if (unknownAssets.length) {
console.error("New assets are being emitted by the core build");
console.log(unknownAssets);
}

writeFileSync(__dirname + "/../dist/ncc/cli.js.cache", cliAssets["cli.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/index.js.cache", indexAssets["index.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js.cache", sourcemapAssets["sourcemap-register.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/node-loader.js.cache", nodeLoaderAssets["node-loader.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js.cache", relocateLoaderAssets["relocate-loader.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js.cache", shebangLoaderAssets["shebang-loader.js.cache"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js.cache", tsLoaderAssets["ts-loader.js.cache"]);

writeFileSync(__dirname + "/../dist/ncc/cli.js.cache.js", cliAssets["cli.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/index.js.cache.js", indexAssets["index.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/sourcemap-register.js.cache.js", sourcemapAssets["sourcemap-register.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/node-loader.js.cache.js", nodeLoaderAssets["node-loader.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/relocate-loader.js.cache.js", relocateLoaderAssets["relocate-loader.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/shebang-loader.js.cache.js", shebangLoaderAssets["shebang-loader.js.cache.js"]);
writeFileSync(__dirname + "/../dist/ncc/loaders/ts-loader.js.cache.js", tsLoaderAssets["ts-loader.js.cache.js"]);

writeFileSync(__dirname + "/../dist/ncc/cli.js", cli);
writeFileSync(__dirname + "/../dist/ncc/index.js", index);
writeFileSync(__dirname + "/../dist/ncc/typescript/index.js", `
Expand Down
7 changes: 5 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Options:
-e, --external [mod] Skip bundling 'mod'. Can be used many times
-q, --quiet Disable build summaries / non-error outputs
-w, --watch Start a watched build
--v8-cache Emit a build using the v8 compile cache
`;

let args;
Expand All @@ -42,7 +43,8 @@ try {
"--quiet": Boolean,
"-q": "--quiet",
"--watch": Boolean,
"-w": "--watch"
"-w": "--watch",
"--v8-cache": Boolean
});
} catch (e) {
if (e.message.indexOf("Unknown or unexpected option") === -1) throw e;
Expand Down Expand Up @@ -173,7 +175,8 @@ switch (args._[0]) {
externals: args["--external"],
sourceMap: args["--source-map"] || run,
cache: args["--no-cache"] ? false : undefined,
watch: args["--watch"]
watch: args["--watch"],
v8cache: args["--v8-cache"]
}
);

Expand Down
27 changes: 23 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,24 @@ module.exports = (
filename = "index.js",
minify = false,
sourceMap = false,
watch = false
watch = false,
v8cache = false
} = {}
) => {
const resolvedEntry = resolve.sync(entry);
const shebangMatch = fs.readFileSync(resolvedEntry).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 resolvePlugins = [];
let tsconfigMatchPath;
const assetState = {
assets: Object.create(null),
assetNames: Object.create(null),
assetNames,
assetPermissions: undefined
};
assetState.assetNames[filename] = true;
Expand Down Expand Up @@ -272,8 +279,8 @@ module.exports = (
getFlatFiles(mfs.data, assets, assetState.assetPermissions);
delete assets[filename];
delete assets[filename + ".map"];
let code = mfs.readFileSync("/" + filename, "utf8");
let map = sourceMap ? mfs.readFileSync("/" + filename + ".map", "utf8") : null;
let code = mfs.readFileSync(`/${filename}`, "utf8");
let map = sourceMap ? mfs.readFileSync(`/${filename}.map`, "utf8") : null;

if (minify) {
const result = terser.minify(code, {
Expand All @@ -294,6 +301,18 @@ module.exports = (
({ code, map } = { code: result.code, map: result.map });
}

if (v8cache) {
const { Script } = require('vm');
assets[filename + '.cache'] = new Script(code).createCachedData();
assets[filename + '.cache.js'] = code;
if (map)
assets[filename + '.map'] = map;
code = `const { readFileSync } = require('fs'), { Script } = require('vm'), { wrap } = require('module');\n` +
`const source = readFileSync(__dirname + '/${filename}.cache.js').toString(), cachedData = readFileSync(__dirname + '/${filename}.cache');\n` +
`(new Script(wrap(source), { cachedData }).runInThisContext())(exports, require, module, __filename, __dirname);\n`;
if (map) map = {};
}

if (shebangMatch) {
code = shebangMatch[0] + code;
// add a line offset to the sourcemap
Expand Down
Loading

0 comments on commit 596c10d

Please sign in to comment.