使用 mini-css-extract-plugin 和 postcss-loader 时模块构建失败

问题描述 投票:0回答:3
  • webpack 5.37.1
  • 迷你CSS提取插件1.6.0
  • postcss-loader 5.3.0
  • 少4.1.1

预期行为

取得成功。

实际行为

将 mini-css-extract-plugin 与 postcss-loader 一起使用时模块构建失败。如果我从加载器列表中删除 postcss-loader,那么它就会正确构建。 mini-css-extract-plugin 或 postcss-loader 有问题吗?

ERROR in ./src/views/app.vue?vue&type=style&index=0&lang=less& (./node_modules/mini-css-extract-plugin/dist/loader.js!./node_modules/css-loader/dist/cjs.js??clonedRuleSet-7[0].rules[0].use[1]!./node_modules/vue-loader/lib/loaders/stylePostLoader.js!./node_modules/postcss-loader/dist/cjs.js??clonedRuleSet-7[0].rules[0].use[2]!./node_modules/less-loader/dist/cjs.js??clonedRuleSet-7[0].rules[0].use[3]!./node_modules/vue-loader/lib/index.js??vue-loader-options!./src/views/app.vue?vue&type=style&index=0&lang=less&)
Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleBuildError: Module build failed (from ./node_modules/postcss-loader/dist/cjs.js):
TypeError: node.getIterator is not a function
    at LazyResult.visitTick (E:\demo\node_modules\postcss\lib\lazy-result.js:522:33)
    at LazyResult.runAsync (E:\demo\node_modules\postcss\lib\lazy-result.js:402:30)
    at async Object.loader (E:\demo\node_modules\postcss-loader\dist\index.js:87:14)
    at processResult (E:\demo\node_modules\webpack\lib\NormalModule.js:700:19)
    at E:\demo\node_modules\webpack\lib\NormalModule.js:806:5
    at E:\demo\node_modules\loader-runner\lib\LoaderRunner.js:399:11
    at E:\demo\node_modules\loader-runner\lib\LoaderRunner.js:251:18
    at context.callback (E:\demo\node_modules\loader-runner\lib\LoaderRunner.js:124:13)
    at Object.loader (E:\demo\node_modules\postcss-loader\dist\index.js:96:7)
 @ ./src/views/app.vue?vue&type=style&index=0&lang=less& 1:0-482 1:0-482
 @ ./src/views/app.vue 4:0-64
 @ ./src/main.js 7:0-32 28:13-20

代码

    exports.cssLoaders = function (options) {
        options = options || {};

        const cssLoader = {
            loader: 'css-loader',
            options: {
                sourceMap: options.sourceMap
            }
        }

        const postcssLoader = {
            loader: 'postcss-loader',
            options: {
                sourceMap: options.sourceMap
            }
        }

        function generateLoaders (loader, loaderOptions) {
            const loaders = [];

            if (options.extract) {
                loaders.push(MiniCssExtractPlugin.loader)
            } else {
                loaders.push('vue-style-loader')
            }

            loaders.push(cssLoader)

            if (options.usePostCSS) {
                loaders.push(postcssLoader)
            }

            if (loader) {
                loaders.push({
                    loader: loader + '-loader',
                    options: Object.assign({}, loaderOptions, {
                        sourceMap: options.sourceMap
                    })
                })
            }

            return loaders
        }
        return {
            css: generateLoaders(),
            postcss: generateLoaders(),
            less: generateLoaders('less')
        }
    }

javascript webpack less postcss
3个回答
1
投票

我找到了导致错误的postcss插件

postcss-px2rem-exclude


1
投票

获取 node.getIterator 也不是一个函数,我的一些包位于 [email protected] 上,有些位于 [email protected] 上。我的node_modules中唯一调用

node.getIterator
的地方是在
node_modules/optimize-css-assets-webpack-plugin/node_modules/postcss/lib
中。看起来不兼容是7/8,但是optimize-css-assets-webpack-plugin的最新用途使用了
"cssnano": "^4.1.10"
,它具有
postcss: ^7.0.0
的依赖关系,但其余文件如
optimize-css-assets-webpack-plugin\node_modules\*\package.json
使用postcss 8...

我用

yarn add [email protected] -W
修复了它。


0
投票

检查 webpack.config.js 中的插件属性,我通过在外部属性之前添加此属性来修复相同的问题:

plugins: [new MiniCssExtractPlugin({ filename: 'webcomp-name-module.css' })]
,

© www.soinside.com 2019 - 2024. All rights reserved.