Skip to content

Commit

Permalink
fix: cleanAfterEveryBuildPatterns can exclude stale webpack asset fro…
Browse files Browse the repository at this point in the history
…m being removed via negative pattern match
  • Loading branch information
chrisblossom committed Mar 13, 2019
1 parent c0f798a commit 9d48016
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
48 changes: 48 additions & 0 deletions src/clean-webpack-plugin.test.ts
Expand Up @@ -854,6 +854,54 @@ describe('cleanAfterEveryBuildPatterns option', () => {
]);
});

test('does not remove stale webpack asset with negative pattern match', async () => {
createSrcBundle(2);
createStaticFiles();

const cleanWebpackPlugin = new CleanWebpackPlugin({
cleanAfterEveryBuildPatterns: ['!1.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',
'1.bundle.js',
'bundle.js',
'static1.js',
'static2.txt',
]);
});

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

Expand Down
12 changes: 9 additions & 3 deletions src/clean-webpack-plugin.ts
Expand Up @@ -249,18 +249,24 @@ class CleanWebpackPlugin {
*/
this.currentAssets = assets.sort();

const removePatterns = [];

/**
* Remove unused webpack assets
*/
if (this.cleanStaleWebpackAssets === true && staleFiles.length !== 0) {
this.removeFiles(staleFiles);
removePatterns.push(...staleFiles);
}

/**
* Run cleanAfterEveryBuildPatterns
* Remove cleanAfterEveryBuildPatterns
*/
if (this.cleanAfterEveryBuildPatterns.length !== 0) {
this.removeFiles(this.cleanAfterEveryBuildPatterns);
removePatterns.push(...this.cleanAfterEveryBuildPatterns);
}

if (removePatterns.length !== 0) {
this.removeFiles(removePatterns);
}
}

Expand Down

0 comments on commit 9d48016

Please sign in to comment.