Skip to content

Commit

Permalink
add cleanStaleWebpackAssets option
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisblossom committed Mar 4, 2019
1 parent c730f45 commit f506dc6
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -63,6 +63,13 @@ new CleanWebpackPlugin({
*/
verbose: true,

/**
* Automatically remove all unused webpack assets on rebuild
*
* default: true
*/
cleanStaleWebpackAssets: false,

/**
* **WARNING**
*
Expand Down
111 changes: 111 additions & 0 deletions src/clean-webpack-plugin.test.ts
Expand Up @@ -269,6 +269,117 @@ test('removes map files', async () => {
]);
});

describe('cleanStaleWebpackAssets option', () => {
test('does not remove assets when false', async () => {
createSrcBundle(2);
createStaticFiles();

const cleanWebpackPlugin = new CleanWebpackPlugin({
cleanStaleWebpackAssets: false,
});

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',
]);

createSrcBundle(1);

await compiler.run();

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

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

test('removes assets when true', async () => {
createSrcBundle(2);
createStaticFiles();

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

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',
]);

createSrcBundle(1);

await compiler.run();

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

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

test('removes assets by default', async () => {
createSrcBundle(2);
createStaticFiles();

const cleanWebpackPlugin = new CleanWebpackPlugin();

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',
]);

createSrcBundle(1);

await compiler.run();

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

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

describe('cleanOnceBeforeBuildPatterns option', () => {
test('does nothing when nothing changes or files added but not removed', async () => {
createSrcBundle(1);
Expand Down
16 changes: 15 additions & 1 deletion src/clean-webpack-plugin.ts
Expand Up @@ -18,6 +18,13 @@ interface Options {
*/
verbose: boolean;

/**
* Automatically remove all unused webpack assets on rebuild
*
* default: true
*/
cleanStaleWebpackAssets: boolean;

/**
* Removes files after every build (including watch mode) that match this pattern.
* Used for files that are not created directly by Webpack.
Expand Down Expand Up @@ -51,6 +58,7 @@ interface Options {
class CleanWebpackPlugin {
private readonly dry: boolean;
private readonly verbose: boolean;
private readonly cleanStaleWebpackAssets: boolean;
private readonly cleanAfterEveryBuildPatterns: string[];
private readonly cleanOnceBeforeBuildPatterns: string[];
private readonly dangerouslyAllowCleanPatternsOutsideProject: boolean;
Expand Down Expand Up @@ -94,6 +102,12 @@ class CleanWebpackPlugin {

this.verbose = this.dry === true || options.verbose === true || false;

this.cleanStaleWebpackAssets =
options.cleanStaleWebpackAssets === true ||
options.cleanStaleWebpackAssets === false
? options.cleanStaleWebpackAssets
: true;

this.cleanAfterEveryBuildPatterns = Array.isArray(
options.cleanAfterEveryBuildPatterns,
)
Expand Down Expand Up @@ -224,7 +238,7 @@ class CleanWebpackPlugin {
/**
* Remove unused webpack assets
*/
if (staleFiles.length !== 0) {
if (this.cleanStaleWebpackAssets === true && staleFiles.length !== 0) {
this.removeFiles(staleFiles);
}

Expand Down

0 comments on commit f506dc6

Please sign in to comment.