admin管理员组文章数量:1404924
In production mode my webpack minifies the .js (as it should). But I also need to minify my .css, and to do this I must use OptimizeCssAssetsPlugin. When I use it, it minifies my .css but then my .js stays unminified.
My guess is that when I use optimization (next to 'modules, and 'plugins') something is missing for js because without whole 'optimization' block it works. But what is it? And why?
const path = require('path');
const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const WriteFilePlugin = require("write-file-webpack-plugin");
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, '/dist'),
filename: 'bundle.js',
publicPath: '/'
},
devtool: 'source-map',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.(sa|sc|c)ss$/,
exclude: /node_modules/,
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"]
}
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./demo/index.html",
filename: "index.html",
inject: false
}),
new MiniCssExtractPlugin({
filename: "style.css"
}),
new WriteFilePlugin()
],
optimization: {
minimizer: [
new OptimizeCssAssetsPlugin(),
],
},
};
In production mode my webpack minifies the .js (as it should). But I also need to minify my .css, and to do this I must use OptimizeCssAssetsPlugin. When I use it, it minifies my .css but then my .js stays unminified.
My guess is that when I use optimization (next to 'modules, and 'plugins') something is missing for js because without whole 'optimization' block it works. But what is it? And why?
const path = require('path');
const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const WriteFilePlugin = require("write-file-webpack-plugin");
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, '/dist'),
filename: 'bundle.js',
publicPath: '/'
},
devtool: 'source-map',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.(sa|sc|c)ss$/,
exclude: /node_modules/,
use: [MiniCssExtractPlugin.loader, "css-loader", "sass-loader"]
}
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./demo/index.html",
filename: "index.html",
inject: false
}),
new MiniCssExtractPlugin({
filename: "style.css"
}),
new WriteFilePlugin()
],
optimization: {
minimizer: [
new OptimizeCssAssetsPlugin(),
],
},
};
Share
Improve this question
edited Mar 27, 2019 at 19:11
prk_001
asked Mar 25, 2019 at 14:38
prk_001prk_001
3813 silver badges18 bronze badges
3
- 1 Please have a look at the production configuration of MiniCssExtractPlugin documentation : While webpack 5 is likely to e with a CSS minimizer built-in, with webpack 4 you need to bring your own. To minify the output, use a plugin like optimize-css-assets-webpack-plugin. Setting optimization.minimizer overrides the defaults provided by webpack, so make sure to also specify a JS minimizer... – tagkiller Commented Mar 25, 2019 at 15:05
- 1 Thank you @tagkiller ! You should post it as an answer as it is a correct answer. I'd never think to look for it in MiniCssExtractPlugin doc though. – prk_001 Commented Mar 25, 2019 at 15:10
- Thanks to let me know it helped you, I just put the answer as it was what you were looking for :-) – tagkiller Commented Mar 26, 2019 at 17:04
2 Answers
Reset to default 5Please have a look at the production configuration of MiniCssExtractPlugin documentation :
While webpack 5 is likely to e with a CSS minimizer built-in, with webpack 4 you need to bring your own. To minify the output, use a plugin like optimize-css-assets-webpack-plugin. Setting optimization.minimizer overrides the defaults provided by webpack, so make sure to also specify a JS minimizer...
Regards,
const TerserJSPlugin = require('terser-webpack-plugin');
....
optimization: {
minimizer: [
// Minify js files:
// (TerserJS is webpack default minifier but we have to specify it explicitly
// as soon as we include more minifiers)
new TerserJSPlugin({}),
// Minify css files:
new OptimizeCSSAssetsPlugin(),
],
},
First, install the plugin:
npm install optimize-css-assets-webpack-plugin --save-dev
Then we need to require it at the top of our webpack.config.js:
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
Optimization.minimizer” setting:
module.exports = {......
optimization: {minimizer: [new TerserJSPlugin({}),new OptimizeCSSAssetsPlugin({})],}
Note : that besides the “OptimizeCSSAssetsPlugin,” there is another one called “TerserJSPlugin,” which is a plugin that already es with webpack.
You won’t need to install it yourself; that’s the plugin used by default to minify your JavaScript when you use “production” mode.
However, the thing you need to do is require it at the top of your webpack configuration file:
const TerserJSPlugin = require('terser-webpack-plugin');
本文标签:
版权声明:本文标题:javascript - Webpack not minifying .js file when OptimizeCssAssetsPlugin is added (without it it works) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744874197a2629816.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论