diff --git a/config/webpack/common.webpack.config.js b/config/webpack/common.webpack.config.js index bcde0f68..efadc452 100644 --- a/config/webpack/common.webpack.config.js +++ b/config/webpack/common.webpack.config.js @@ -8,7 +8,7 @@ const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin'); const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin'); const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin'); const ForkCheckerPlugin = require('awesome-typescript-loader').ForkCheckerPlugin; -const processExitCode = require('../../plugin/process-exit-code'); +const ProcessExitCode = require('../../plugin/process-exit-code'); const skyPagesConfigUtil = require('../sky-pages/sky-pages.config'); const aliasBuilder = require('./alias-builder'); @@ -142,7 +142,7 @@ function getWebpackConfig(skyPagesConfig) { ), // Webpack 2 behavior does not correctly return non-zero exit code. - processExitCode + new ProcessExitCode() ] }; } diff --git a/config/webpack/test.webpack.config.js b/config/webpack/test.webpack.config.js index af5ba379..365f3746 100644 --- a/config/webpack/test.webpack.config.js +++ b/config/webpack/test.webpack.config.js @@ -16,7 +16,7 @@ function getWebpackConfig(skyPagesConfig, argv) { const DefinePlugin = require('webpack/lib/DefinePlugin'); const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin'); const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin'); - const processExitCode = require('../../plugin/process-exit-code'); + const ProcessExitCode = require('../../plugin/process-exit-code'); const skyPagesConfigUtil = require('../sky-pages/sky-pages.config'); const aliasBuilder = require('./alias-builder'); @@ -160,7 +160,7 @@ function getWebpackConfig(skyPagesConfig, argv) { ), // Webpack 2 behavior does not correctly return non-zero exit code. - processExitCode + new ProcessExitCode() ] }; diff --git a/plugin/process-exit-code/index.js b/plugin/process-exit-code/index.js index 57a1ff03..26a514a7 100644 --- a/plugin/process-exit-code/index.js +++ b/plugin/process-exit-code/index.js @@ -5,18 +5,14 @@ * This function is inspired from webpack-fail-plugin, which has been deprecated. * Unfortunately, webpack does not correctly return a non-zero exit code unless using their CLI. */ -module.exports = function processExitCode() { - let isWatch = true; +function ProcessExitCode() { } - this.plugin('run', (compiler, callback) => { - isWatch = false; - callback.call(compiler); - }); - - this.plugin('done', stats => { - if (stats.compilation.errors && stats.compilation.errors.length && !isWatch) { - process.on('beforeExit', () => process.exit(1)); +ProcessExitCode.prototype.apply = function (compiler) { + compiler.plugin('done', stats => { + if (stats.compilation.errors && stats.compilation.errors.length) { + process.on('exit', () => process.exitCode = 1); } }); - }; + +module.exports = ProcessExitCode; diff --git a/test/config-webpack-common.spec.js b/test/config-webpack-common.spec.js index 6802208a..b74fdf71 100644 --- a/test/config-webpack-common.spec.js +++ b/test/config-webpack-common.spec.js @@ -3,6 +3,7 @@ const path = require('path'); const fs = require('fs'); +const ProcessExitCode = require('../plugin/process-exit-code'); const runtimeUtils = require('../utils/runtime-test-utils'); describe('config webpack common', () => { @@ -123,12 +124,11 @@ describe('config webpack common', () => { }); expect(processOnSpy).not.toHaveBeenCalled(); - }) + }); - it('should pass a non-zero exit code to process.exit on errors', () => { - const processExitSpy = spyOn(process, 'exit'); + it('should set process.exitCode to 1 if compilation errors', () => { const processOnSpy = spyOn(process, 'on').and.callFake((evt, cb) => { - if (evt === 'beforeExit') { + if (evt === 'exit') { cb(); } }); @@ -139,13 +139,10 @@ describe('config webpack common', () => { }); config.plugins.forEach(plugin => { - if (plugin.name === 'processExitCode') { + if (plugin instanceof ProcessExitCode) { plugin.apply({ plugin: (evt, cb) => { switch (evt) { - case 'run': - cb(() => {}, () => {}); - break; case 'done': cb({ compilation: { @@ -162,7 +159,41 @@ describe('config webpack common', () => { }); expect(processOnSpy).toHaveBeenCalled(); - expect(processExitSpy).toHaveBeenCalledWith(1); + expect(process.exitCode).toEqual(1); + }); + + it('should not set process.exit listener if no compilation errors', () => { + + // Reset process.exitCode from any previous tests + process.exitCode = 0; + + const processOnSpy = spyOn(process, 'on'); + const lib = require('../config/webpack/common.webpack.config'); + const config = lib.getWebpackConfig({ + runtime: runtimeUtils.getDefaultRuntime(), + skyux: {} + }); + + config.plugins.forEach(plugin => { + if (plugin instanceof ProcessExitCode) { + plugin.apply({ + plugin: (evt, cb) => { + switch (evt) { + case 'done': + cb({ + compilation: { + errors: [] + } + }); + break; + } + } + }); + } + }); + + expect(processOnSpy).not.toHaveBeenCalled(); + expect(process.exitCode).not.toEqual(1); }); });