gpt4 book ai didi

css - Webpack4 - 从 less 文件导入的 CSS 文件没有被缩小

转载 作者:行者123 更新时间:2023-11-28 11:00:45 25 4
gpt4 key购买 nike

我正在用 webpack 4 构建一个元素。几乎所有元素都可以正常工作,我只是在缩小 CSS 方面遇到了问题。

如果在我的入口点包含 a.css 或 .less 文件,那么一切正常。如果我使用 @include './myfile.less',它也能正常工作。但是,如果我使用 @include './myfile.css',则导入该部分时不会缩小。

如何使导入的 CSS 文件起作用?

webpack.config

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin'); //installed via npm
const webpack = require('webpack'); //to access built-in plugins
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {

entry: {
'app': './src/app/app.js',
},

output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].bundle.js'
},

module: {
rules: [
{
test: /\.html/,
use: 'html-loader'
},
{
test: /\.less$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
"css-loader",
"less-loader"
]
},
{
test: /\.(png|svg|jpg|gif)$/,
use: ['file-loader?name=assets/images/[path][name].[ext]?[hash]']
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader?name=assets/fonts/[name].[ext]?[hash]'
]
}
],
},

plugins: [
new HtmlWebpackPlugin({ template: './src/public/index.html' }),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "[name].bundle.css",
chunkFilename: "[id].css"
}),
]

};

最佳答案

虽然 webpack 5 可能内置了 CSS 最小化器,但对于 webpack 4,您需要自带。要缩小输出,请使用像 optimize-css-assets-webpack-plugin 这样的插件。设置 optimization.minimizer 会覆盖 webpack 提供的默认值,因此请确保还指定了一个 JS 最小化器

webpack.config.js

const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
module.exports = {
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true // set to true if you want JS source maps
}),
new OptimizeCSSAssetsPlugin({})
]
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
chunkFilename: "[id].css"
})
],
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader"
]
}
]
}
}

Reference

关于css - Webpack4 - 从 less 文件导入的 CSS 文件没有被缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52306735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com