fix(webpack): don't ignore compilation errors (#9369)

* fix(webpack): Fail build in case of compilation errors.

WebPack's own documentation states that the `err` object **will not**
include compilation errors.

https://webpack.js.org/api/node/#webpack

This fix addresses compilation errors by setting the correct `process.exitCode`
looking at the result of the `stats.hasErrors()` call.

* fix(tsc): Ensure that TypeScript compilation errors are handled.

The `async` flag of the `fork-ts-checker-webpack-plugin` will (by default
in development mode) avoid reporting any errors detected by `tsc` back
to webpack:

https://github.com/TypeStrong/fork-ts-checker-webpack-plugin#options

> If `true` reports issues **after** webpack's compilation is done.
> Thanks to that it doesn't block the compilation.

The problem in this case is that any compilation error will be then
undetectable by the `WatchStatePlugin` which will happily tell the
NativeScript CLI to continue with the build process.

* fix(cli): Do not send the `compilation` message to the CLI on errors.

When the compilation fails, this patch will prevent for the `compilation`
message to be sent back to the CLI, preventing broken builds hitting the device.
This commit is contained in:
Pier Fumagalli
2021-08-03 21:36:02 +09:00
committed by GitHub
parent 0fd53e3abb
commit 0a6f80a554
3 changed files with 15 additions and 1 deletions

View File

@ -98,6 +98,9 @@ program
}
if (stats) {
// Set the process exit code depending on errors
process.exitCode = stats.hasErrors() ? 1 : 0;
console.log(
stats.toString({
chunks: false,

View File

@ -233,6 +233,11 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
.plugin('ForkTsCheckerWebpackPlugin')
.use(ForkTsCheckerWebpackPlugin, [
{
// If we use "async" errors compiling typescript will be ignored by
// WebPack (we will send the "compilation" message back to the CLI,
// and the process exit code will be zero), therefore we will end
// up with a broken build
async: false,
typescript: {
memoryLimit: 4096,
},

View File

@ -48,9 +48,15 @@ export class WatchStatePlugin {
isWatchMode ? messages.startWatching : messages.compilationComplete
);
// Do not notify the CLI if the compilation failed
const stats = compilation.getStats();
if (stats.hasErrors()) {
return;
}
// logic taken from CleanWebpackPlugin
const assets =
compilation.getStats().toJson(
stats.toJson(
{
assets: true,
},