Open rightaway opened 3 years ago
I get the same error as @rightaway ... What is the solution to apply, please?
Finally, that's how it's working fine:
const{ CleanWebpackPlugin } = require("clean-webpack-plugin");
const path = require("path");
module.exports = {
output: {
path: path.resolve(__dirname, "dist"),
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [path.join(__dirname, "dist/**/*")],
}),
],
};
Is this when using the clean-webpack-plugin
together with webpack-dev-server
? I'm guessing that Webpack 5 doesn't set output.path
when running as dev server, because it keeps the files in memory, so there's nothing to clean. If I'm not mistaken, the solution would probably be to use the clean-webpack-plugin
in the production build configuration.
If the author of this module could confirm this, that would be awesome.
Hi @evpaassen ... And how would it be an example of the code refered to "use the clean-webpack-plugin
in the production build configuration."?
The code I put isn't a good solution?
@PJ-CM this issue is about clean-webpack-plugin not respecting webpack 5 defaults. Your solution is not really a solution to the issue :)
I think the issue or the question that @rightaway does is about the right configuration of clean-wbpack-plugin for Webpack 5 and how to avoid that mistake.
No, it's not about correct configuration. It's about clean-webpack-plugin not taking into account default output.path value when it's not explicitly provided.
@PJ-CM - your solution works because you've explicitly configured output.path
. If you remove it, clean-webpack-plugin
will fail to clean up things.
@evpaassen - the behavior is consistent with any kind of build. As @smashercosmo mentioned, clean-webpack-plugin
seems to ignore Webpack 5 default output.path
, which Webpack 5 upgrade guide calls out to remove if it is path.resolve(__dirname, 'dist')
, as that is the default. Once removed (per the Webpack 5 upgrade guide), clean-webpack-plugin
stops working, and the build logs show the message:
clean-webpack-plugin: options.output.path not defined. Plugin disabled...
It is worth mentioning that beginning Webpack 5.20.0+, there is an output.clean
option that cleans the output directory before emit:
module.exports = {
//...
output: {
clean: true, // Clean the output directory before emit.
},
};
@solimant coool, didn't know that. thx for the tip)
So, finally, with the output.clean
option, there is no need to use clean-webpack-plugin, right?
@PJ-CM - if you're on Webpack 5.20.0+, yes, at least for basic clean-up needs, in my opinion.
That's the version I'm using ... Thank you @solimant . Nothing better than immediate responses.
See also #197
This plugin still has value if using cleanAfterEveryBuildPatterns
option, so the original question still stands as to why it is necessary to explicitly specify the output.path.
Issue description or question
With webpack 5.11.1 and clean-webpack-plugin 3.0.0 I get error
clean-webpack-plugin: options.output.path not defined. Plugin disabled...
whenpath
isn't defined explicitly in the config file. But https://webpack.js.org/configuration/output/#outputpath defines a default path so is this check necessary? It should use the default path if one isn't provided explicitly.