使用“ env”或“ es2015”时,webpack,react和babel无法正常工作

时间:2018-07-22 10:59:09

标签: reactjs webpack ecmascript-6

当前,当我尝试进行构建时,只有“ es2017”作为预设起作用,es2015或env会导致typeError出现“ l不是构造函数”,这表明当我附加这些代码时,jsx无法正常工作。下面是webpack配置和附加的json包。

webpack配置

const path = require("path");
const webpack = require("webpack");
const webpack_rules = [];
const webpackOption = {
    entry: './index.jsx',
    mode: 'production',
    output: {
        path: __dirname,
        filename: "bundle.js",
    },
    module: {
        rules: webpack_rules
    }
};
let babelLoader = {
    test:  /\.(js|jsx)$/,
    exclude: /(node_modules)/,
    use: {
        loader: "babel-loader"
    }
};

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


let imageLoader = { 
    test: /\.(png|jpg|gif)$/,
    loader: "url-loader?mimetype=image/png" 
};
webpack_rules.push(imageLoader, cssLoader, babelLoader);
module.exports = webpackOption;

babelrc文件

{
  "presets": ["react","env","stage-0"]
}

package.json

{
  "name": "tutorial",
  "version": "1.0.0",
  "description": "tutorial for react testing",
  "main": "./index.jsx",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start:dev": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-es2017": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "css-loader": "^0.28.11",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-redux": "^5.0.7",
    "react-router-dom": "^4.2.2",
    "redux": "^3.7.2",
    "style-loader": "^0.20.3",
    "url-loader": "^1.0.1",
    "webpack": "^4.1.1",
    "webpack-cli": "^2.0.11",
    "webpack-dev-server": "^3.1.4"
  },
  "dependencies": {
    "jspdf": "^1.4.1",
    "moment": "^2.22.2",
    "react-datepicker": "^1.5.0"
  }
}

如果有人可以指出问题所在,请提供帮助。您可以看到我正在使用每个库的所有最新版本。

0 个答案:

没有答案