react 使用webpack打包问题汇总

一、TypeError: src\foo.js: Cannot read property 'bindings' of null
问题原因:
1.babel-loader一系列包的版本不兼容有冲突,需要检查版本,下面是能够成功运行的版本的版本组合:
"@babel/plugin-proposal-class-properties": "^7.1.0",
"babel-core": "^7.0.0-bridge.0",
"babel-loader": "^7.1.5",
"babel-preset-env": "^2.0.0-alpha.20",
"babel-preset-es2015": "^7.0.0-beta.3",
"babel-preset-react": "^7.0.0-beta.3",
2.rules配置的时候没有将node_modules exclude掉, 例子:

{
                test: /\.js?$/,
                exclude: path.resolve(__dirname, "node_modules"),
                include: path.resolve(__dirname, "src"),
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env', 'react'],
                        plugins: [
                            ["@babel/plugin-proposal-class-properties", { "loose": true }]
                        ]
                    },
                }
}

二、工程中涉及到antd框架的时候,应该如何配置?

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    mode: "development",
    entry: path.resolve(__dirname, "./src/index.js"),
    output: {
        path: path.resolve(__dirname, "dist")
    },
    devServer: {
        contentBase: path.join(__dirname, "dist"),
        compress: true,
        port: 3000
    },
    resolve: {
        extensions: ['.js', '.jsx', '.less']
    },
    module: {
        rules: [
            {
                test: /\.css?$/,
                include: [path.join(__dirname, 'src'), path.join(__dirname, 'node_modules/antd')],
                use: [ 'style-loader', 'css-loader']
            },
            {
                test: /\.less?$/,
                include: [path.join(__dirname, 'src'), path.join(__dirname, 'node_modules/antd')],
                use: {
                    loader: "less-loader",
                    options:{
                        javascriptEnabled: true
                    }
                }
            },
            {
                test: /\.js?$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env', 'react'],
                        plugins: [
                            ["@babel/plugin-proposal-class-properties", { "loose": true }],
["import", { "libraryName": "antd", "style": "css" }],#按需加载antd里面的组件
                        ]
                    },
                }
            },
            {
                test: /\.(ico|png|svg|jpg|gif)$/,
                exclude: path.resolve(__dirname, "node_modules"),
                include: path.resolve(__dirname, "src"),
                use: [
                    {
                        loader: 'file-loader',
                        options: {}
                    }
                ]
            }
        ]

    },
    plugins: [
        new HtmlWebpackPlugin({template: './public/index.html'})
    ]
};

你可能感兴趣的:(react 使用webpack打包问题汇总)