React模块的构建因babel和webpack4失败

时间:2019-03-27 15:15:27

标签: reactjs babel webpack-4 babel-loader

我使用Webpack 4和Babel 7解构了Create-React-App。第一次运行命令“ npm run build”时,它起作用并创建了一个新的构建文件夹。现在,第一次尝试再次运行命令后,我遇到了错误提示。

./src/index.js中的

错误 模块构建失败(来自./node_modules/babel-loader/lib/index.js):

我搜索了解决方案,但我只能找到Package.json文件中的Babel版本差异,而我似乎没有。

Package.json

{
  "name": "hamilton-beach",
  "version": "0.1.0",
  "license": "MIT",
  "main": "src/index.js",
  "scripts": {
    "start": "webpack-dev-server --open --port 3000 --compress --mode development",
    "dev": "webpack --mode development --progress",
    "build": "webpack --mode production --progress",
    "build-css": "node-sass --include-path scss src/main.scss   src/App.css",
    "watch-css": "nodemon -e scss -x \"npm run build-css\""
  },
  "private": true,
  "dependencies": {
    "@babel/polyfill": "^7.4.0",
    "axios": "^0.18.0",
    "core-js": "^3.0.0",
    "css-loader": "^2.1.1",
    "file-loader": "^3.0.1",
    "jsonwebtoken": "^8.5.0",
    "lodash": "^4.17.11",
    "node-sass": "^4.11.0",
    "nodemailer-sendgrid-transport": "^0.2.0",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-redux": "^6.0.0",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "redux": "^4.0.1",
    "redux-devtools-extension": "^2.13.8",
    "redux-logger": "^3.0.6",
    "redux-thunk": "^2.3.0",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.23.1"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "proxy": "http://localhost:5000",
  "devDependencies": {
    "@babel/core": "^7.4.0",
    "@babel/plugin-proposal-class-properties": "^7.4.0",
    "@babel/plugin-proposal-decorators": "^7.4.0",
    "@babel/preset-env": "^7.4.2",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.5",
    "html-webpack-plugin": "^4.0.0-beta.5",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0",
    "webpack-dev-server": "^3.2.1"
  }
}

.babelrc

{
    "presets": [
        [
            "@babel/env", 
            {
                "useBuiltIns": "entry",
                "targets": {
                    "browsers": [
                        "last 2 versions"
                    ]
                }
            },
            "@babel/react"

        ]
    ],
    "plugins": [
        "@babel/proposal-class-properties",
        ["@babel/plugin-proposal-decorators", { "decoratorsBeforeExport": true }],
    ]
}

webpack.config.js

const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
    devtool: 'cheap-module-source-map',
    module: {
        rules: [
            { 
                test: /\.js$/, 
                exclude: /node_modules/, 
                loader: 'babel-loader'
            },
            {
                test: /\.scss$/,
                use: [
                  {
                    loader: "style-loader" // creates style nodes from JS strings
                  },
                  {
                    loader: "css-loader" // translates CSS into CommonJS
                  },
                  {
                    loader: "sass-loader" // compiles Sass to CSS
                  }
                ]
            },
            {
                test: /\.(ttf|eot|svg|gif|png|jpg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: [{
                    loader: 'file-loader'
                }]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'index.html'
        })
    ]
}

0 个答案:

没有答案