简体   繁体   中英

Config antd with react and webpack

Here is the error I receive:

    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 config file:

    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 config file:

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

I followed multi tutorials and how-tos but I end up with an error every time and I have no idea how to fix this error since I have just started learning about babel and webpack with small experince.

In this problem I followed exactly this docs https://ant.design/docs/react/getting-started and I still end up with an error

You shouldn't need to import the antd css at the top of App.js . I also think that the babel plugin should have style set to css in the babel config file (that's how our config file is set up anyway!).

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

Our less loader also has javascriptEnabled set to 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
    }
}]

In the end, I used CSS, in the webpack config file add this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM