webpack 未捕获的类型错误:无法读取未定义的属性'locals'

1yjd4xko  于 2022-12-13  发布在  Webpack
关注(0)|答案(3)|浏览(158)

我已经创建了bundle.js和bundle.css用于我的Web应用程序,
我的package.json看起来像这样:

"dependencies": {
    "bootstrap": "^4.5.3",
    "jquery": "^3.5.1",
    "popper.js": "^1.16.1"
  },
  "devDependencies": {
    "@babel/core": "^7.12.10",
    "@babel/plugin-proposal-object-rest-spread": "^7.12.1",
    "@babel/preset-env": "^7.12.11",
    "babel-loader": "^8.2.2",
    "css-loader": "^5.0.1",
    "file-loader": "^6.2.0",
    "mini-css-extract-plugin": "^1.3.3",
    "node-sass": "^5.0.0",
    "sass-loader": "^10.1.0",
    "style-loader": "^2.0.0",
    "url-loader": "^4.1.1",
    "webpack": "^5.11.0",
    "webpack-cli": "^4.2.0"
  },

我的webpack.config.js:

const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = (env, argv) => {
    return {
        //Define entry point
        entry: ['./src/index.js', './src/css/index.scss'],

        //Define output point
        output: {
            path: path.resolve(__dirname, 'wwwroot/dist'),
            filename: 'bundle.js'
        },

        module: {
            rules: [
                {
                    test: /\.s[c|a]ss$/,
                    include: path.resolve(__dirname, 'src'),
                    use: [
                        'style-loader',
                        MiniCssExtractPlugin.loader,
                        'css-loader',
                        'sass-loader'
                    ]
                },
                {
                    test: /\.js$/,
                    include: path.resolve(__dirname, 'src'),
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env"],
                        plugins: ['@babel/plugin-proposal-object-rest-spread']
                    }
                },
            ]
        },
        plugins: [
            new webpack.ProvidePlugin({
                $: 'jquery',
                jQuery: 'jquery'
            }),
            new MiniCssExtractPlugin({
                filename: 'bundle.css'
            })
        ]

    };
};

我的src结构如下:
src
index.js:

import 'jquery';
import 'bootstrap';
import './js/login';
import './js/helpers';

index.scss:

@import 'bootstrap';
@import 'util.scss';
@import 'login.scss';
@import 'site.css';

我在控制台中遇到此错误:
Console
bundle.js
其他一切都很好,我没有问题的javascript,但它臭虫我,我不知道如何修复这个错误。
PS:我对Web开发还是新手,昨天开始使用webpack,我的想法是在我的html中只使用bundle.js和bundle.css,而不单独为它们添加jquery和bootstrap标记

qyswt5oh

qyswt5oh1#

尝试将MiniCssExtractPlugin的esModule选项设置为false

use: [
    'style-loader',
    {
        loader: MiniCssExtractPlugin.loader,
        options: {
            esModule: false,
        },
    },
    'css-loader',
    'sass-loader'
]
a64a0gku

a64a0gku2#

不要将'style-loader'与mini-css-extract-plugin沿着使用,修改您的规则以匹配以下内容:

{
    test: /\.s[c|a]ss$/,
    include: path.resolve(__dirname, 'src'),
    use: [
            //'style-loader',
            MiniCssExtractPlugin.loader,
            'css-loader',
            'sass-loader'
         ]
},

您可以在此处找到更多详细信息:https://github.com/webpack-contrib/mini-css-extract-plugin/issues/613

3htmauhk

3htmauhk3#

类似于@Dina的答案,但没有MiniCssExtractPlugin

use: [
    {
      loader: "style-loader",
      options: {
        esModule: false,
      },
    },
    "css-loader",
    "sass-loader",
  ]

相关问题