Skip to content

Commit 2fed871

Browse files
committed
Replace clean-webpack-plugin with the core output cleaning feature
1 parent a6cf19f commit 2fed871

11 files changed

+35
-288
lines changed

CHANGELOG.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ This is a new major version that contains several backwards-compatibility breaks
1010

1111
* #1308 Drop Vue 2 support (End-Of-Life), only Vue 3 is supported (@Kocal)
1212

13-
* #1309 Drop ESLint integration (@Kocal)
13+
* #1309 Drop ESLint integration (@Kocal)
14+
15+
* #1313 Drop `clean-webpack-plugin` in favor of webpack's `output.clean` configuration. The
16+
configuration settings supported by `Encore.cleanupOutputBeforeBuild` have changed (@stof)
1417

1518
## 4.7.0
1619

@@ -992,4 +995,3 @@ for a full description of all of the valid browser descriptions.
992995

993996
* `Encore.cleanupOutputBeforeBuild()` now empties the directory
994997
instead or removing it.
995-

index.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1491,22 +1491,21 @@ class Encore {
14911491
/**
14921492
* If enabled, the output directory is emptied between each build (to remove old files).
14931493
*
1494-
* A list of available options can be found at https://github.com./johnagan/clean-webpack-plugin
1494+
* A list of available options can be found at https://webpack.js.org/configuration/output/#outputclean
14951495
*
14961496
* For example:
14971497
*
14981498
* ```
1499-
* Encore.cleanupOutputBeforeBuild(['*.js'], (options) => {
1499+
* Encore.cleanupOutputBeforeBuild((options) => {
15001500
* options.dry = true;
15011501
* })
15021502
* ```
15031503
*
1504-
* @param {Array} paths Paths that should be cleaned, relative to the "root" option
1505-
* @param {Function} cleanWebpackPluginOptionsCallback
1504+
* @param {Function} cleanOptionsCallback
15061505
* @returns {Encore}
15071506
*/
1508-
cleanupOutputBeforeBuild(paths = ['**/*'], cleanWebpackPluginOptionsCallback = () => {}) {
1509-
webpackConfig.cleanupOutputBeforeBuild(paths, cleanWebpackPluginOptionsCallback);
1507+
cleanupOutputBeforeBuild(cleanOptionsCallback = () => {}) {
1508+
webpackConfig.cleanupOutputBeforeBuild(cleanOptionsCallback);
15101509

15111510
return this;
15121511
}

lib/WebpackConfig.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,8 @@ class WebpackConfig {
154154
svelte: () => {},
155155
};
156156

157-
// Plugins options
158-
this.cleanWebpackPluginPaths = ['**/*'];
159-
160157
// Plugins callbacks
161-
this.cleanWebpackPluginOptionsCallback = () => {};
158+
this.cleanOptionsCallback = () => {};
162159
this.definePluginOptionsCallback = () => {};
163160
this.forkedTypeScriptTypesCheckOptionsCallback = () => {};
164161
this.friendlyErrorsPluginOptionsCallback = () => {};
@@ -844,18 +841,13 @@ class WebpackConfig {
844841
this.fontRuleCallback = ruleCallback;
845842
}
846843

847-
cleanupOutputBeforeBuild(paths = ['**/*'], cleanWebpackPluginOptionsCallback = () => {}) {
848-
if (!Array.isArray(paths)) {
849-
throw new Error('Argument 1 to cleanupOutputBeforeBuild() must be an Array of paths - e.g. [\'**/*\']');
850-
}
851-
852-
if (typeof cleanWebpackPluginOptionsCallback !== 'function') {
853-
throw new Error('Argument 2 to cleanupOutputBeforeBuild() must be a callback function');
844+
cleanupOutputBeforeBuild(cleanOptionsCallback = () => {}) {
845+
if (typeof cleanOptionsCallback !== 'function') {
846+
throw new Error('Argument 1 to cleanupOutputBeforeBuild() must be a callback function');
854847
}
855848

856849
this.cleanupOutput = true;
857-
this.cleanWebpackPluginPaths = paths;
858-
this.cleanWebpackPluginOptionsCallback = cleanWebpackPluginOptionsCallback;
850+
this.cleanOptionsCallback = cleanOptionsCallback;
859851
}
860852

861853
autoProvideVariables(variables) {

lib/config-generator.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const deleteUnusedEntriesPluginUtil = require('./plugins/delete-unused-entries')
3131
const entryFilesManifestPlugin = require('./plugins/entry-files-manifest');
3232
const manifestPluginUtil = require('./plugins/manifest');
3333
const variableProviderPluginUtil = require('./plugins/variable-provider');
34-
const cleanPluginUtil = require('./plugins/clean');
3534
const definePluginUtil = require('./plugins/define');
3635
const terserPluginUtil = require('./plugins/terser');
3736
const optimizeCssAssetsUtil = require('./plugins/optimize-css-assets');
@@ -247,6 +246,7 @@ class ConfigGenerator {
247246
}
248247

249248
return {
249+
clean: this.buildCleanConfig(),
250250
path: this.webpackConfig.outputPath,
251251
filename: filename,
252252
// default "asset module" filename
@@ -259,6 +259,17 @@ class ConfigGenerator {
259259
};
260260
}
261261

262+
/**
263+
* @returns {import('webpack').CleanOptions|boolean}
264+
*/
265+
buildCleanConfig() {
266+
if (!this.webpackConfig.cleanupOutput) {
267+
return false;
268+
}
269+
270+
return applyOptionsCallback(this.webpackConfig.cleanOptionsCallback, {});
271+
}
272+
262273
buildRulesConfig() {
263274
const applyRuleConfigurationCallback = (name, defaultRules) => {
264275
return applyOptionsCallback(this.webpackConfig.loaderConfigurationCallbacks[name], defaultRules);
@@ -460,8 +471,6 @@ class ConfigGenerator {
460471

461472
variableProviderPluginUtil(plugins, this.webpackConfig);
462473

463-
cleanPluginUtil(plugins, this.webpackConfig);
464-
465474
definePluginUtil(plugins, this.webpackConfig);
466475

467476
notifierPluginUtil(plugins, this.webpackConfig);

lib/plugins/clean.js

-51
This file was deleted.

lib/plugins/plugin-priorities.js

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ module.exports = {
1515
WebpackManifestPlugin: 120,
1616
LoaderOptionsPlugin: 110,
1717
ProvidePlugin: 90,
18-
CleanWebpackPlugin: 80,
1918
DefinePlugin: 70,
2019
WebpackNotifier: 60,
2120
VueLoaderPlugin: 50,

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"assets-webpack-plugin": "7.0.*",
3232
"babel-loader": "^9.1.3",
3333
"chalk": "^4.0.0",
34-
"clean-webpack-plugin": "^4.0.0",
3534
"css-loader": "^6.7.0",
3635
"css-minimizer-webpack-plugin": "^5.0.0",
3736
"fastest-levenshtein": "^1.0.16",

test/WebpackConfig.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -225,33 +225,23 @@ describe('WebpackConfig object', () => {
225225
config.cleanupOutputBeforeBuild();
226226

227227
expect(config.cleanupOutput).to.be.true;
228-
expect(config.cleanWebpackPluginPaths).to.deep.equal(['**/*']);
229228
});
230229

231230
it('Setting paths and callback', () => {
232231
const config = createConfig();
233232
const callback = () => {};
234-
config.cleanupOutputBeforeBuild(['**/*.js', '**/*.css'], callback);
233+
config.cleanupOutputBeforeBuild(callback);
235234

236235
expect(config.cleanupOutput).to.be.true;
237-
expect(config.cleanWebpackPluginPaths).to.deep.equal(['**/*.js', '**/*.css']);
238-
expect(config.cleanWebpackPluginOptionsCallback).to.equal(callback);
239-
});
240-
241-
it('Setting invalid paths argument', () => {
242-
const config = createConfig();
243-
244-
expect(() => {
245-
config.cleanupOutputBeforeBuild('foo', () => {});
246-
}).to.throw('Argument 1 to cleanupOutputBeforeBuild() must be an Array of paths');
236+
expect(config.cleanOptionsCallback).to.equal(callback);
247237
});
248238

249239
it('Setting invalid callback argument', () => {
250240
const config = createConfig();
251241

252242
expect(() => {
253-
config.cleanupOutputBeforeBuild(['**/*'], 'foo');
254-
}).to.throw('Argument 2 to cleanupOutputBeforeBuild() must be a callback function');
243+
config.cleanupOutputBeforeBuild('foo');
244+
}).to.throw('Argument 1 to cleanupOutputBeforeBuild() must be a callback function');
255245
});
256246
});
257247

test/config-generator.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const RuntimeConfig = require('../lib/config/RuntimeConfig');
1515
const configGenerator = require('../lib/config-generator');
1616
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
1717
const { WebpackManifestPlugin } = require('../lib/webpack-manifest-plugin');
18-
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
1918
const webpack = require('webpack');
2019
const path = require('path');
2120
const logger = require('../lib/logger');
@@ -534,7 +533,7 @@ describe('The config-generator function', () => {
534533
});
535534
});
536535

537-
describe('cleanupOutputBeforeBuild() adds CleanWebpackPlugin', () => {
536+
describe('cleanupOutputBeforeBuild() configures output cleaning', () => {
538537
it('without cleanupOutputBeforeBuild()', () => {
539538
const config = createConfig();
540539
config.outputPath = '/tmp/output/public-path';
@@ -543,8 +542,7 @@ describe('The config-generator function', () => {
543542

544543
const actualConfig = configGenerator(config);
545544

546-
const cleanPlugin = findPlugin(CleanWebpackPlugin, actualConfig.plugins);
547-
expect(cleanPlugin).to.be.undefined;
545+
expect(actualConfig.output.clean).to.be.false;
548546
});
549547

550548
it('with cleanupOutputBeforeBuild()', () => {
@@ -556,8 +554,7 @@ describe('The config-generator function', () => {
556554

557555
const actualConfig = configGenerator(config);
558556

559-
const cleanPlugin = findPlugin(CleanWebpackPlugin, actualConfig.plugins);
560-
expect(cleanPlugin).to.not.be.undefined;
557+
expect(actualConfig.output.clean).to.be.haveOwnProperty('keep');
561558
});
562559
});
563560

test/plugins/clean.js

-80
This file was deleted.

0 commit comments

Comments
 (0)