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

fix: do not clean directory if webpack errors are present during initial build #134

Merged
merged 1 commit into from May 22, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,6 +1,8 @@
import path from 'path';
import { sync as delSync } from 'del';
import { Compiler, Stats } from 'webpack';
import { Compiler, Stats, compilation as compilationType } from 'webpack';

type Compilation = compilationType.Compilation;

export interface Options {
/**
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