使用React和Webpack配置Ant

时间:2019-03-08 10:08:04

标签: reactjs webpack babel babel-loader

这是我收到的错误:

    Uncaught Error: Module parse failed: Unexpected token (15:5)
You may need an appropriate loader to handle this file type.
| /* stylelint-disable at-rule-no-unknown */
| html,
> body {
|   width: 100%;
|   height: 100%;
    at eval (antd.css:1)
    at Object../node_modules/antd/dist/antd.css (index.js:228)
    at __webpack_require__ (index.js:20)
    at eval (App.js:10)
    at Module../KQShopping/frontend/src/App.js (index.js:97)
    at __webpack_require__ (index.js:20)
    at eval (index.js:2)
    at Module../KQShopping/frontend/src/index.js (index.js:109)
    at __webpack_require__ (index.js:20)
    at index.js:84

webpack配置文件:

    const path = require('path');
const fs  = require('fs');

const lessToJs = require('less-vars-to-js');
const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, './ant-theme-vars.less'), 'utf8'));

module.exports = {
    module: {
        rules: [
            {
                loader: 'babel-loader',
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                options: {
                    plugins: [
                        ['import', { libraryName: "antd", style: true }]
                    ]
                },
            },
            {
                test: /\.less$/,
                use: [
                    {loader: "style-loader"},
                    {loader: "css-loader"},
                    {loader: "less-loader",
                        options: {
                            modifyVars: themeVariables
                        }
                    }
                ]
            }
        ]
    }
};

App.js:

     import React from "react";
import ReactDOM from "react-dom";
import { DatePicker, message } from "antd";
import "antd/dist/antd.css";

class App extends React.Component {
    state = {
        date: null,
    };

    handleChange = date => {
        message.info(`Selected Date: ${date ? date.format("YYYY-MM-DD") : "None"}`);
        this.setState({ date });
    };

    render() {
        const { date } = this.state;
        return (
            <div style={{ width: 400, margin: "100px auto" }}>
                <DatePicker onChange={this.handleChange} />
                <div style={{ marginTop: 20 }}>
                    Selected Date: {date ? date.format("YYYY-MM-DD") : "None"}
                </div>
            </div>
        );
    }
}

ReactDOM.render(<App />, document.getElementById("app"));

Babel配置文件:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "plugins": ["transform-class-properties", ["import", { "libraryName": "antd", "style": "true" }]]
}

我遵循了多个教程和操作方法,但每次都以错误告终,而且由于我刚开始学习有关babel和webpack的经验,因此我不知道如何解决此错误。

在这个问题中,我完全遵循了https://ant.design/docs/react/getting-started文档,但仍然出现错误

2 个答案:

答案 0 :(得分:0)

您不需要导入App.js顶部的antd css。我还认为babel插件应在babel配置文件中将style设置为css(无论如何,这就是我们的配置文件的设置!)。

[
    'import',
    {
        'libraryName': 'antd',
        'style': 'css'
    }
]

我们的较少加载程序还将javascriptEnabled设置为true:

test: /\.less$/,
use: [{
    loader: 'style-loader' // creates style nodes from JS strings
},
{
    loader: 'css-loader' // translates CSS into CommonJ
},
{
    loader: 'less-loader', // compiles Less to CSS
    options: {
        javascriptEnabled: true
    }
}]

答案 1 :(得分:0)

最后,我使用CSS,在webpack配置文件中添加以下内容:

{
use: ['style-loader', 'css-loader'],
test: /\.css$/
}