Skip to content

Commit

Permalink
cleanAfterEveryBuildPatterns should not remove webpack assets
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisblossom committed Mar 4, 2019
1 parent 6d6b341 commit c730f45
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
47 changes: 47 additions & 0 deletions src/clean-webpack-plugin.test.ts
Expand Up @@ -551,6 +551,53 @@ describe('cleanAfterEveryBuildPatterns option', () => {
]);
});

test('does not remove webpack assets', async () => {
createSrcBundle(2);
createStaticFiles();

const cleanWebpackPlugin = new CleanWebpackPlugin({
cleanAfterEveryBuildPatterns: ['bundle.js'],
});

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

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

await compiler.run();

expect(cleanWebpackPlugin.currentAssets).toEqual([
'1.bundle.js',
'bundle.js',
]);

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

createSrcBundle(1);
createStaticFiles();

await compiler.run();

expect(cleanWebpackPlugin.currentAssets).toEqual(['bundle.js']);

expect(sandbox.getFileListSync(outputPathFull)).toEqual([
'.hidden.file',
'bundle.js',
'static1.js',
'static2.txt',
]);
});

test('handles the cleanAfterEveryBuildPatterns outside of webpack output directory', async () => {
createSrcBundle(1);

Expand Down
23 changes: 13 additions & 10 deletions src/clean-webpack-plugin.ts
@@ -1,6 +1,6 @@
import { Compiler, Stats } from 'webpack';
import path from 'path';
import del from 'del';
import del, { Options as DelOptions } from 'del';

interface Options {
/**
Expand Down Expand Up @@ -222,29 +222,32 @@ class CleanWebpackPlugin {
this.currentAssets = assets.sort();

/**
* Do nothing if there aren't any files to delete and cleanAfterEveryBuildPatterns is not defined
* Remove unused webpack assets
*/
if (
staleFiles.length === 0 &&
this.cleanAfterEveryBuildPatterns.length === 0
) {
return;
if (staleFiles.length !== 0) {
this.removeFiles(staleFiles);
}

/**
* Merge customPatters with stale files.
* Run cleanAfterEveryBuildPatterns
*/
this.removeFiles([...staleFiles, ...this.cleanAfterEveryBuildPatterns]);
if (this.cleanAfterEveryBuildPatterns.length !== 0) {
this.removeFiles(this.cleanAfterEveryBuildPatterns, {
// Never remove current webpack assets
ignore: this.currentAssets,
});
}
}

removeFiles(patterns: string[]) {
removeFiles(patterns: string[], delOptions: DelOptions = {}) {
try {
const deleted = del.sync(patterns, {
force: this.dangerouslyAllowCleanPatternsOutsideProject,
// Change context to build directory
cwd: this.outputPath,
dryRun: this.dry,
dot: true,
...delOptions,
});

/**
Expand Down

0 comments on commit c730f45

Please sign in to comment.