javascript - 从 React ES6 导入时,Webpack3 CSS/LESS 不起作用

标签 javascript reactjs webpack ecmascript-6

我正在使用 Webpack 来识别 CSS/LESS 文件,但没有取得很大成功。

这是我的 Webpack 文件:

const path = require("path");
const HtmlWebPackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");


module.exports = {
    entry: ["./src/App.js"],
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "app.bundle.js"
    },
    module: {
        rules: [
            {
                test: /\.css$/i,
                use: ExtractTextPlugin.extract({
                    use: ['style-loader', 'css-loader'] //, 'less-loader']
                })
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            },
            {
                test: /\.html$/,
                use: [
                    {
                        loader: "html-loader"
                    }
                ],
            },
        ]
    },
    plugins: [
        new ExtractTextPlugin('app.bundle.css'),
        new HtmlWebPackPlugin({
            template: "./src/index.html",
            filename: "./index.html",
            inject: "body"
        }),

    ],
    devServer: {
        contentBase: "./dist",
        filename: "app.bundle.js",
        publicPath: "/",
        stats: {
            colors: true
        }
    },
};

当我尝试从 React 文件导入它时,我得到以下结果:

import styles from 'styles/index.css';

错误:

ERROR in ./src/App.js
Module not found: Error: Can't resolve 'styles/index.css' in '/code/customers/project/map/src'
 @ ./src/App.js 15:13-40
 @ multi ./src/App.js

该文件清晰存在。知道为什么 webpack 拒绝包含该文件吗?

谢谢!

最佳答案

您需要以 ./ 为前缀,否则它将寻找名为 styles 的 npm 模块。

import styles from './styles/index.css';?

关于javascript - 从 React ES6 导入时,Webpack3 CSS/LESS 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47899719/

相关文章:

javascript - 删除的环回模型属性仍然存在

javascript - 如何使用同一对象的另一个键的值更改对象的键值?

javascript - ReactJS Material UI 自动完成 : How to Configure data source

javascript - 错误 : Cannot resolve module 'babel-loader'

javascript - 需要更改 CouchDB 身份验证的字符串格式

javascript - jQuery AJAX。返回值未定义?

php - 为深色背景和 V.S. 设置白色字体颜色在 PHP 中

reactjs - 更新 axios post 请求中的全局状态

webpack - monorepo 中的 create-react-app + gatsby 包不会同时运行

javascript - Angular 7 构建 : How to remove output hash and default~<view1>~<module1> file?