diff --git a/readme.md b/readme.md index 6458fb08..ee1f1908 100644 --- a/readme.md +++ b/readme.md @@ -33,7 +33,7 @@ npm i -g @zeit/ncc ```bash $ ncc ``` -Eg: +Eg: ```bash $ ncc build input.js -o dist ``` @@ -64,6 +64,7 @@ Outputs the Node.js compact build of `input.js` into `dist/index.js`. -q, --quiet Disable build summaries / non-error outputs -w, --watch Start a watched build --v8-cache Emit a build using the v8 compile cache + --stats-out [file] Emit webpack stats as json to the specified output file ``` ### Execution Testing diff --git a/src/cli.js b/src/cli.js index da3f7d55..6e30f3ba 100755 --- a/src/cli.js +++ b/src/cli.js @@ -29,6 +29,7 @@ Options: -w, --watch Start a watched build -t, --transpile-only Use transpileOnly option with the ts-loader --v8-cache Emit a build using the v8 compile cache + --stats-out [file] Emit webpack stats as json to the specified output file `; // support an API mode for CLI testing @@ -139,6 +140,7 @@ async function runCmd (argv, stdout, stderr) { "--v8-cache": Boolean, "--transpile-only": Boolean, "-t": "--transpile-only", + "--stats-out": String, }, { permissive: false, argv @@ -154,6 +156,7 @@ async function runCmd (argv, stdout, stderr) { let run = false; let outDir = args["--out"]; const quiet = args["--quiet"]; + const statsOutFile = args["--stats-out"]; switch (args._[0]) { case "cache": @@ -232,7 +235,7 @@ async function runCmd (argv, stdout, stderr) { } ); - async function handler ({ err, code, map, assets, symlinks }) { + async function handler ({ err, code, map, assets, symlinks, stats }) { // handle watch errors if (err) { stderr.write(err + '\n'); @@ -265,7 +268,7 @@ async function runCmd (argv, stdout, stderr) { } if (!quiet) { - stdout.write( + stdout.write( renderSummary( code, map, @@ -280,6 +283,9 @@ async function runCmd (argv, stdout, stderr) { stdout.write('Watching for changes...\n'); } + if (statsOutFile) + writeFileSync(statsOutFile, JSON.stringify(stats.toJson())); + if (run) { // find node_modules const root = resolve('/node_modules'); diff --git a/src/index.js b/src/index.js index 1e5043f7..aad53448 100644 --- a/src/index.js +++ b/src/index.js @@ -230,7 +230,7 @@ module.exports = ( "var e = new Error", `if (typeof req === 'number' && __webpack_require__.m[req])\n` + ` return __webpack_require__(req);\n` + - `try { return require(req) }\n` + + `try { return require(req) }\n` + `catch (e) { if (e.code !== 'MODULE_NOT_FOUND') throw e }\n` + `var e = new Error` ); @@ -279,7 +279,7 @@ module.exports = ( const errLog = stats.compilation.errors.map(err => err.message).join('\n'); return reject(new Error(errLog)); } - resolve(); + resolve(stats); }); }); }) @@ -298,7 +298,7 @@ module.exports = ( return watchHandler({ err }); if (stats.hasErrors()) return watchHandler({ err: stats.toString() }); - const returnValue = finalizeHandler(); + const returnValue = finalizeHandler(stats); if (watchHandler) watchHandler(returnValue); else @@ -331,7 +331,7 @@ module.exports = ( }; } - function finalizeHandler () { + function finalizeHandler (stats) { const assets = Object.create(null); getFlatFiles(mfs.data, assets, relocateLoader.getAssetPermissions); // filter symlinks to existing assets @@ -414,7 +414,7 @@ module.exports = ( map.mappings = ";" + map.mappings; } - return { code, map: map ? JSON.stringify(map) : undefined, assets, symlinks }; + return { code, map: map ? JSON.stringify(map) : undefined, assets, symlinks, stats }; } }; diff --git a/test/cli.js b/test/cli.js index 54e6dda9..ebfe58ee 100644 --- a/test/cli.js +++ b/test/cli.js @@ -63,5 +63,17 @@ expect (code, stdout, stderr) { return stdout.toString().indexOf('tmp/index.js') !== -1; } + }, + { + args: ["build", "-o", "tmp", "test/fixtures/test.mjs", "--stats-out", "tmp/stats.json"], + expect (code, stdout, stderr) { + const fs = require('fs'); + try { + JSON.parse(fs.readFileSync('tmp/stats.json', 'utf8')); + } catch { + return false; + } + return true; + } } -] \ No newline at end of file +]