温馨提示:本文翻译自stackoverflow.com,查看原文请点击:asp.net - Webpack not recognising css loader

asp.net - Webpack无法识别CSS加载程序

发布于 2020-03-27 12:07:28

我刚刚开始使用asp.net MVC开发系统并做出反应。在尝试集成CSS时(专门用于react-table包样式'react-table / react-table.css'),webpack拒绝对其进行编译。

我尝试添加css-loader,style-loader,Mini-CSS-Extract-Plugin和一系列其他规则,但无济于事。我想我几乎在这里查看了所有相关帖子,但没有任何进展。我也尝试过使用一个非常基本的CSS文件来查看它是否是react-table。无论我尝试什么,都会收到错误消息:

“未捕获的错误:模块解析失败:意外的令牌(1:0)您可能需要适当的加载程序来处理此文件类型,当前没有配置任何加载程序来处理此文件。”

这是当前的webpack.config.js:

"use strict";

var path = require("path");
var WebpackNotifierPlugin = require("webpack-notifier");
var BrowserSyncPlugin = require("browser-sync-webpack-plugin");

module.exports = {
    entry: "./Scripts/Home/react/index.js",
    output: {
        path: path.resolve(__dirname, "./Scripts/dist/Home/react"),
        filename: "bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.css$/,
                exclude: /node_modules/,
                use: ['style-loader', 'css-loader?modules=true&camelCase=true'],
            },
            {
                test: /\.css$/,
                include: /node_modules/,
                loaders: ['style-loader', 'css-loader?modules=true&camelCase=true'],
            }

        ]
    },
    devtool: "inline-source-map",
    plugins: [new WebpackNotifierPlugin(), new BrowserSyncPlugin()]
};

还有我的package.json

    {
  "version": "1.0.0",
  "name": "asp.net",
  "private": true,
  "scripts": {
    "dev": "webpack --mode development --watch",
    "build": "webpack"
  },
  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/plugin-proposal-class-properties": "^7.4.4",
    "@babel/preset-env": "^7.4.5",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.6",
    "browser-sync": "^2.26.7",
    "browser-sync-webpack-plugin": "^2.2.2",
    "css-loader": "^3.0.0",
    "webpack": "^4.35.2",
    "webpack-cli": "^3.3.5",
    "webpack-notifier": "^1.8.0"
  },
  "dependencies": {
    "bootstrap": "^4.3.1",
    "lodash": "^4.17.11",
    "mini-css-extract-plugin": "^0.7.0",
    "namor": "^1.1.2",
    "react": "^16.8.6",
    "react-bootstrap": "^1.0.0-beta.9",
    "react-dom": "^16.8.6",
    "react-table": "^6.10.0",
    "style-loader": "^0.23.1",
    "styled-components": "^4.3.2"
  }
}

我的印象是,添加css-loader和style-loader如此简单,以产生所需的结果,但是不幸的是我没有运气

在此先感谢您的支持!

查看更多

查看更多

提问者
JaamieT
被浏览
36
Tony Ngo 2019-07-03 23:53

您可以尝试使用我的配置。我正在使用sass,您可以将其删除

module: {
        rules: [{
                test: /\.scss$/,
                use: [
                    'style-loader',
                    MiniCssExtractPlugin.loader,
                    {
                        loader: "css-loader",
                        options: {
                            minimize: true,
                            sourceMap: true
                        }
                    },
                    {
                        loader: "sass-loader"
                    }
                ]
            },
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: ["babel-loader", "eslint-loader"]
            },
            {
                test: /\.(jpe?g|png|gif)$/i,
                loader: "file-loader"
            },
            {
                test: /\.(woff|ttf|otf|eot|woff2|svg)$/i,
                loader: "file-loader"
            }
        ]
    }

完整的代码可以在这里找到

发布
问题

分享
好友

手机
浏览

扫码手机浏览