Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit 018127d

Browse files
Bobby EarlBlackbaud-PaulCrowder
Bobby Earl
authored andcommitted
Modernizing ProcessExitCode plugin (#173)
1 parent e7e9223 commit 018127d

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

config/webpack/common.webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
88
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
99
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
1010
const ForkCheckerPlugin = require('awesome-typescript-loader').ForkCheckerPlugin;
11-
const processExitCode = require('../../plugin/process-exit-code');
11+
const ProcessExitCode = require('../../plugin/process-exit-code');
1212
const skyPagesConfigUtil = require('../sky-pages/sky-pages.config');
1313
const aliasBuilder = require('./alias-builder');
1414

@@ -142,7 +142,7 @@ function getWebpackConfig(skyPagesConfig) {
142142
),
143143

144144
// Webpack 2 behavior does not correctly return non-zero exit code.
145-
processExitCode
145+
new ProcessExitCode()
146146
]
147147
};
148148
}

config/webpack/test.webpack.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function getWebpackConfig(skyPagesConfig, argv) {
1616
const DefinePlugin = require('webpack/lib/DefinePlugin');
1717
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
1818
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
19-
const processExitCode = require('../../plugin/process-exit-code');
19+
const ProcessExitCode = require('../../plugin/process-exit-code');
2020
const skyPagesConfigUtil = require('../sky-pages/sky-pages.config');
2121
const aliasBuilder = require('./alias-builder');
2222

@@ -160,7 +160,7 @@ function getWebpackConfig(skyPagesConfig, argv) {
160160
),
161161

162162
// Webpack 2 behavior does not correctly return non-zero exit code.
163-
processExitCode
163+
new ProcessExitCode()
164164
]
165165
};
166166

plugin/process-exit-code/index.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
* This function is inspired from webpack-fail-plugin, which has been deprecated.
66
* Unfortunately, webpack does not correctly return a non-zero exit code unless using their CLI.
77
*/
8-
module.exports = function processExitCode() {
9-
let isWatch = true;
8+
function ProcessExitCode() { }
109

11-
this.plugin('run', (compiler, callback) => {
12-
isWatch = false;
13-
callback.call(compiler);
14-
});
15-
16-
this.plugin('done', stats => {
17-
if (stats.compilation.errors && stats.compilation.errors.length && !isWatch) {
18-
process.on('beforeExit', () => process.exit(1));
10+
ProcessExitCode.prototype.apply = function (compiler) {
11+
compiler.plugin('done', stats => {
12+
if (stats.compilation.errors && stats.compilation.errors.length) {
13+
process.on('exit', () => process.exitCode = 1);
1914
}
2015
});
21-
2216
};
17+
18+
module.exports = ProcessExitCode;

test/config-webpack-common.spec.js

+40-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const path = require('path');
55
const fs = require('fs');
6+
const ProcessExitCode = require('../plugin/process-exit-code');
67
const runtimeUtils = require('../utils/runtime-test-utils');
78

89
describe('config webpack common', () => {
@@ -123,12 +124,11 @@ describe('config webpack common', () => {
123124
});
124125

125126
expect(processOnSpy).not.toHaveBeenCalled();
126-
})
127+
});
127128

128-
it('should pass a non-zero exit code to process.exit on errors', () => {
129-
const processExitSpy = spyOn(process, 'exit');
129+
it('should set process.exitCode to 1 if compilation errors', () => {
130130
const processOnSpy = spyOn(process, 'on').and.callFake((evt, cb) => {
131-
if (evt === 'beforeExit') {
131+
if (evt === 'exit') {
132132
cb();
133133
}
134134
});
@@ -139,13 +139,10 @@ describe('config webpack common', () => {
139139
});
140140

141141
config.plugins.forEach(plugin => {
142-
if (plugin.name === 'processExitCode') {
142+
if (plugin instanceof ProcessExitCode) {
143143
plugin.apply({
144144
plugin: (evt, cb) => {
145145
switch (evt) {
146-
case 'run':
147-
cb(() => {}, () => {});
148-
break;
149146
case 'done':
150147
cb({
151148
compilation: {
@@ -162,7 +159,41 @@ describe('config webpack common', () => {
162159
});
163160

164161
expect(processOnSpy).toHaveBeenCalled();
165-
expect(processExitSpy).toHaveBeenCalledWith(1);
162+
expect(process.exitCode).toEqual(1);
163+
});
164+
165+
it('should not set process.exit listener if no compilation errors', () => {
166+
167+
// Reset process.exitCode from any previous tests
168+
process.exitCode = 0;
169+
170+
const processOnSpy = spyOn(process, 'on');
171+
const lib = require('../config/webpack/common.webpack.config');
172+
const config = lib.getWebpackConfig({
173+
runtime: runtimeUtils.getDefaultRuntime(),
174+
skyux: {}
175+
});
176+
177+
config.plugins.forEach(plugin => {
178+
if (plugin instanceof ProcessExitCode) {
179+
plugin.apply({
180+
plugin: (evt, cb) => {
181+
switch (evt) {
182+
case 'done':
183+
cb({
184+
compilation: {
185+
errors: []
186+
}
187+
});
188+
break;
189+
}
190+
}
191+
});
192+
}
193+
});
194+
195+
expect(processOnSpy).not.toHaveBeenCalled();
196+
expect(process.exitCode).not.toEqual(1);
166197
});
167198

168199
});

0 commit comments

Comments
 (0)