johnagan / clean-webpack-plugin

A webpack plugin to remove your build folder(s) before building
MIT License
1.96k stars 134 forks source link

clean-webpack-plugin: options.output.path not defined. Plugin disabled... #194

Open rightaway opened 3 years ago

rightaway commented 3 years ago

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... when path 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.

PJ-CM commented 3 years ago

I get the same error as @rightaway ... What is the solution to apply, please?

PJ-CM commented 3 years ago

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/**/*")],
        }),
    ],
};
evpaassen commented 3 years ago

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.

PJ-CM commented 3 years ago

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?

smashercosmo commented 3 years ago

@PJ-CM this issue is about clean-webpack-plugin not respecting webpack 5 defaults. Your solution is not really a solution to the issue :)

PJ-CM commented 3 years ago

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.

smashercosmo commented 3 years ago

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.

solimant commented 3 years ago

@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...
solimant commented 3 years ago

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.
  },
};
smashercosmo commented 3 years ago

@solimant coool, didn't know that. thx for the tip)

PJ-CM commented 3 years ago

So, finally, with the output.clean option, there is no need to use clean-webpack-plugin, right?

solimant commented 3 years ago

@PJ-CM - if you're on Webpack 5.20.0+, yes, at least for basic clean-up needs, in my opinion.

PJ-CM commented 3 years ago

That's the version I'm using ... Thank you @solimant . Nothing better than immediate responses.

madarche commented 3 years ago

See also #197

rbruckheimer commented 3 years ago

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.