Skip to content

Commit

Permalink
fix: do not clean directory if webpack errors are present during init…
Browse files Browse the repository at this point in the history
…ial build
  • Loading branch information
chrisblossom committed May 2, 2019
1 parent 9f8a3bb commit 9a14d8b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
67 changes: 66 additions & 1 deletion src/clean-webpack-plugin.test.ts
Expand Up @@ -1133,7 +1133,72 @@ describe('verbose option', () => {
});

describe('webpack errors', () => {
test('does nothing when webpack errors are present', async () => {
test('does nothing when webpack errors are present on initial build', async () => {
createSrcBundle(2);

const cleanWebpackPluginPrevious = new CleanWebpackPlugin();

const compilerPrevious = webpack({
entry: entryFileFull,
output: {
path: outputPathFull,
filename: 'bundle.js',
chunkFilename: '[name].bundle.js',
},
plugins: [cleanWebpackPluginPrevious],
});

expect(cleanWebpackPluginPrevious.currentAssets).toEqual([]);

/**
* Run successful build to place files inside dist folder but not in current assets
*/
await compilerPrevious.run();

const cleanWebpackPlugin = new CleanWebpackPlugin({
verbose: true,
});

const compiler = webpack({
entry: entryFileFull,
output: {
path: outputPathFull,
filename: 'bundle.js',
chunkFilename: '[name].bundle.js',
},
plugins: [cleanWebpackPlugin],
});

expect(sandbox.getFileListSync(outputPathFull)).toEqual([
'1.bundle.js',
'bundle.js',
]);

expect(consoleSpy.mock.calls).toEqual([]);

/**
* remove entry file to create webpack compile error
*/
sandbox.deleteSync(entryFile);

try {
await compiler.run();
// eslint-disable-next-line no-empty
} catch (error) {}

expect(sandbox.getFileListSync(outputPathFull)).toEqual([
'1.bundle.js',
'bundle.js',
]);

expect(cleanWebpackPlugin.currentAssets).toEqual([]);

expect(consoleSpy.mock.calls).toEqual([
['clean-webpack-plugin: pausing due to webpack errors'],
]);
});

test('does nothing when webpack errors are present on rebuild', async () => {
createSrcBundle(2);

const cleanWebpackPlugin = new CleanWebpackPlugin({
Expand Down
30 changes: 24 additions & 6 deletions src/clean-webpack-plugin.ts
@@ -1,7 +1,9 @@
import { Compiler, Stats } from 'webpack';
import { Compiler, Stats, compilation as compilationType } from 'webpack';
import path from 'path';
import { sync as delSync } from 'del';

type Compilation = compilationType.Compilation;

interface Options {
/**
* Simulate the removal of files
Expand Down Expand Up @@ -173,12 +175,18 @@ class CleanWebpackPlugin {

if (this.cleanOnceBeforeBuildPatterns.length !== 0) {
if (hooks) {
hooks.compile.tap('clean-webpack-plugin', () => {
this.handleInitial();
hooks.emit.tap('clean-webpack-plugin', (compilation) => {
this.handleInitial(compilation);
});
} else {
compiler.plugin('compile', () => {
this.handleInitial();
compiler.plugin('emit', (compilation, callback) => {
try {
this.handleInitial(compilation);

callback();
} catch (error) {
callback(error);
}
});
}
}
Expand All @@ -201,11 +209,21 @@ class CleanWebpackPlugin {
*
* Warning: It is recommended to initially clean your build directory outside of webpack to minimize unexpected behavior.
*/
handleInitial() {
handleInitial(compilation: Compilation) {
if (this.initialClean) {
return;
}

/**
* Do not remove files if there are compilation errors
*
* Handle logging inside this.handleDone
*/
const stats = compilation.getStats();
if (stats.hasErrors()) {
return;
}

this.initialClean = true;

this.removeFiles(this.cleanOnceBeforeBuildPatterns);
Expand Down

0 comments on commit 9a14d8b

Please sign in to comment.