Skip to content

Commit

Permalink
Replace clean-webpack-plugin with the core output cleaning feature
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Aug 30, 2024
1 parent 3056c2a commit bdd2512
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 249 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@ class Encore {
/**
* If enabled, the output directory is emptied between each build (to remove old files).
*
* A list of available options can be found at https://github.com/johnagan/clean-webpack-plugin
* A list of available options can be found at https://webpack.js.org/configuration/output/#outputclean
*
* For example:
*
Expand Down
25 changes: 22 additions & 3 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const deleteUnusedEntriesPluginUtil = require('./plugins/delete-unused-entries')
const entryFilesManifestPlugin = require('./plugins/entry-files-manifest');
const manifestPluginUtil = require('./plugins/manifest');
const variableProviderPluginUtil = require('./plugins/variable-provider');
const cleanPluginUtil = require('./plugins/clean');
const definePluginUtil = require('./plugins/define');
const terserPluginUtil = require('./plugins/terser');
const optimizeCssAssetsUtil = require('./plugins/optimize-css-assets');
Expand All @@ -40,6 +39,7 @@ const PluginPriorities = require('./plugins/plugin-priorities');
const applyOptionsCallback = require('./utils/apply-options-callback');
const copyEntryTmpName = require('./utils/copyEntryTmpName');
const getVueVersion = require('./utils/get-vue-version');
const multimatch = require('multimatch');
const tmp = require('tmp');
const fs = require('fs');
const path = require('path');
Expand Down Expand Up @@ -244,6 +244,7 @@ class ConfigGenerator {
}

return {
clean: this.buildCleanConfig(),
path: this.webpackConfig.outputPath,
filename: filename,
// default "asset module" filename
Expand All @@ -256,6 +257,26 @@ class ConfigGenerator {
};
}

/**

Check failure on line 260 in lib/config-generator.js

View workflow job for this annotation

GitHub Actions / ESLint

JSDoc syntax error
* @returns {import('webpack').CleanOptions|boolean}
*/
buildCleanConfig() {
if (!this.webpackConfig.cleanupOutput) {
return false;
}

const cleanedPaths = [].concat(this.webpackConfig.cleanWebpackPluginPaths);
// works around a bug where manifest.json is emitted when
// using dev-server... but then CleanWebpackPlugin deletes it
cleanedPaths.push('!manifest.json');

const cleanConfig = {
keep: (path)=> multimatch(path, cleanedPaths).length === 0
};

return applyOptionsCallback(this.webpackConfig.cleanWebpackPluginOptionsCallback, cleanConfig);
}

buildRulesConfig() {
const applyRuleConfigurationCallback = (name, defaultRules) => {
return applyOptionsCallback(this.webpackConfig.loaderConfigurationCallbacks[name], defaultRules);
Expand Down Expand Up @@ -457,8 +478,6 @@ class ConfigGenerator {

variableProviderPluginUtil(plugins, this.webpackConfig);

cleanPluginUtil(plugins, this.webpackConfig);

definePluginUtil(plugins, this.webpackConfig);

notifierPluginUtil(plugins, this.webpackConfig);
Expand Down
48 changes: 0 additions & 48 deletions lib/plugins/clean.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/plugins/plugin-priorities.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module.exports = {
WebpackManifestPlugin: 120,
LoaderOptionsPlugin: 110,
ProvidePlugin: 90,
CleanWebpackPlugin: 80,
DefinePlugin: 70,
WebpackNotifier: 60,
VueLoaderPlugin: 50,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"assets-webpack-plugin": "7.0.*",
"babel-loader": "^9.1.3",
"chalk": "^4.0.0",
"clean-webpack-plugin": "^4.0.0",
"css-loader": "^6.7.0",
"css-minimizer-webpack-plugin": "^5.0.0",
"fastest-levenshtein": "^1.0.16",
"mini-css-extract-plugin": "^2.6.0",
"multimatch": "^5",
"pretty-error": "^4.0.0",
"resolve-url-loader": "^5.0.0",
"semver": "^7.3.2",
Expand Down
9 changes: 3 additions & 6 deletions test/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const RuntimeConfig = require('../lib/config/RuntimeConfig');
const configGenerator = require('../lib/config-generator');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { WebpackManifestPlugin } = require('../lib/webpack-manifest-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const webpack = require('webpack');
const path = require('path');
const logger = require('../lib/logger');
Expand Down Expand Up @@ -534,7 +533,7 @@ describe('The config-generator function', () => {
});
});

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

const actualConfig = configGenerator(config);

const cleanPlugin = findPlugin(CleanWebpackPlugin, actualConfig.plugins);
expect(cleanPlugin).to.be.undefined;
expect(actualConfig.output.clean).to.be.false;
});

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

const actualConfig = configGenerator(config);

const cleanPlugin = findPlugin(CleanWebpackPlugin, actualConfig.plugins);
expect(cleanPlugin).to.not.be.undefined;
expect(actualConfig.output.clean).to.be.haveOwnProperty('keep');
});
});

Expand Down
80 changes: 0 additions & 80 deletions test/plugins/clean.js

This file was deleted.

Loading

0 comments on commit bdd2512

Please sign in to comment.