使用条目的带有 Less 和 MiniCssExtractPlugin 的 Webpack 4

标签 webpack less webpack-style-loader webpack-4 mini-css-extract-plugin

我的应用程序中的样式具有以下结构:

申请

- css/
   - bootstrap/
      - boostrap.less -> has (@import "another.less")
      - another.less
   - common/
      - common.less
   - entries/
      - bootstrap.js -> has (import style from "../bootstrap/bootstrap.less")
      - common.js -> has (import common from "../common/common.less")

现在我需要从导入到条目 bootstrap.js 和 common.js 的样式中创建单独的 CSS-es。

webpack.config.js
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
{
    entry: {
        "boostrap": ["./css/entries/boostrap.js"]
    },
    module: {
        rules: [
            {
                test: /\.(less)$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader",
                    "less-loader"
                ]
            }
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: "./css/[name].css"
        })
    ]
}

package.json
{
    "webpack": "^4.16.3",
    "webpack-cli": "^3.1.0",
    "css-loader": "^1.0.0",
    "less-loader": "^4.1.0",
    "mini-css-extract-plugin": "^0.4.1",
}

当我运行 webpack 时,出现以下错误:
Entrypoint boostrap = boostrap.js
    [0] multi ./css/entries/boostrap.js 28 bytes {0} [built]
    [1] ./css/entries/boostrap.js 48 bytes {0} [built]
    [2] ./css/bootstrap/bootstrap.less 1.41 KiB {0} [built] [failed] [1 error]

    ERROR in ./css/bootstrap/bootstrap.less
    Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
    ModuleParseError: Module parse failed: Unexpected character ' ' (1:0)
    You may need an appropriate loader to handle this file type.

你知道有什么问题吗?它看起来像 bootstrap.less在导入其他 less 文件期间抛出错误,但我不知道为什么。

谢谢

拉法尔

PS:报告类似问题 here .

最佳答案

我找到了原因。原来我的boostrap.less引用了 glyphicons.less . Glyphicons 为扩展名导入字体:*.eot、*.woff2、*.woff、*.ttf 和 *.svg,这是我缺少的部分。
file-loaderurl-loader是两种可能的选择。我选择了后者:

npm install url-loader --save-dev



并添加如下配置:
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
{
    entry: {
        "boostrap": ["./css/entries/boostrap.js"]
    },
    module: {
        rules: [
            {
                test: /\.(less)$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    "css-loader",
                    "less-loader"
                ]
            },
            {
                test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/,
                use: "url-loader"
            }
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: "./css/[name].css"
        })
    ]
}

谢谢

拉法尔

关于使用条目的带有 Less 和 MiniCssExtractPlugin 的 Webpack 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51671425/

相关文章:

less - Ant 设计 : How are they changing color on run time and so quick?

css - 如何在 less 中分离设置和 css?

security - Webpack 样式加载器 appendChild 对 CSP 不友好

Laravel Mix npm run dev 错误 ELIFECYCLE 在服务器上

javascript - WEBPACK - 我如何需要原型(prototype)函数?

linux - xdg-打开 : no method available for opening 'http://localhost:8080'

html - Flexbox 布局模式 1/3

javascript - 在 webpack.config.js 中定义 css 文件

javascript - webpack 样式加载器和 css 加载器对于简单示例不起作用

javascript - 如何使用 Vue Cli 3 配置 Webpack-simple?