Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option --stats-out to emit webpack statistics output #516

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ npm i -g @zeit/ncc
```bash
$ ncc <cmd> <opts>
```
Eg:
Eg:
```bash
$ ncc build input.js -o dist
```
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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":
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -265,7 +268,7 @@ async function runCmd (argv, stdout, stderr) {
}

if (!quiet) {
stdout.write(
stdout.write(
renderSummary(
code,
map,
Expand All @@ -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');
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
);
Expand Down Expand Up @@ -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);
});
});
})
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 };
}
};

Expand Down
14 changes: 13 additions & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
]
]