简体   繁体   English

使用Webpack,Babel和React在IE11中出现语法错误

[英]Syntax error in IE11 with Webpack, Babel and React

I'm getting a Syntax Error in my React + Redux project in Internet Explorer 11, but I have no idea why it's caused. 我在Internet Explorer 11中的React + Redux项目中遇到语法错误,但我不知道为什么会导致它。

I'm using Webpack and Babel to compile it. 我正在使用Webpack和Babel来编译它。

I tried using babel-polyfill and babel-es6-polyfill, but that didn't help. 我尝试使用babel-polyfill和babel-es6-polyfill,但这没有帮助。

This is the error I'm getting: 这是我得到的错误:

SCRIPT1002: Syntax error
File: app.js, Line: 70, Column: 1

Line 70 Column 1 is where the eval starts of Webpack: 第70行第1列是Webpack的eval开始的地方:

/***/ }),
/* 21 */,
/* 22 */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
eval("\n\nObject.define... <- Line 70
^--- Column 1

This is my webpack.config.js : 这是我的webpack.config.js

'use strict';
// Include modules and plugins
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

// App and build directories
const APP_DIR = path.resolve(__dirname, 'src/');
const BUILD_DIR = path.resolve(__dirname, 'public');

// Extract sass from the application, see index.jsx
const extractSass = new ExtractTextPlugin({
    filename: 'css/[name].css'
});

// The config file to load
let env = (process.env.NODE_ENV || 'dev').toLowerCase();
let configFile = path.resolve(__dirname, 'config/config.' + env + '.json');

// Default config file if not found
const defaultConfigFile = path.resolve(__dirname, 'config/config.dev.json');

/*
 * Config to be injected into the app
 * Note that JSON files are parsed upon requiring
 */
let config;

/*
 * Get the actual config
 */
try {
    config = require(configFile);
    console.log('Loaded config file ' + configFile);
} catch (e) {
    config = require(defaultConfigFile);
    console.log('Fallen back to default config file');
}

// The actual webpack config
const webpackConfig = {
    entry: {
        // The app entry point
        app: APP_DIR + '/index.jsx',

        // Vendor files will be used for bundling, they will not be compiled into the app itself
        vendor: [
            'axios',
            'prop-types',
            'react',
            'reactstrap',
            'react-chartjs-2',
            'react-dom',
            'react-redux',
            'react-router',
            'react-router-dom',
            'redux',
            'sprintf-js',
        ]
    },

    output: {
        path: BUILD_DIR,
        filename: 'js/app.js'
    },

    module: {

        /*
         * These are loaders for webpack, these will assist with compilation
         */
        loaders: [
            {
                /*
                 * Use Babel to compile JS and JSX files
                 * See .babelrc
                 */
                test: /\.jsx?/,
                include: APP_DIR,
                loader: 'babel-loader'
            }
        ],
        rules: [
            {
                /*
                 * Sass/Scss compilation rules
                 */
                test: /\.scss$/,
                use: extractSass.extract({
                    use: [
                        {
                            loader: 'css-loader'
                        },
                        {
                            loader: 'sass-loader'
                        }
                    ],
                    fallback: 'style-loader'
                })
            },
            {
                /*
                 * JS(X) compilation rules
                 * We need this, otherwise Webpack will crash during compile time
                 */
                test: /\.jsx?/,
                loader: 'babel-loader'
            }
        ]
    },
    plugins: [
        /*
         * The CommonsChunkPlugin is responsible to create bundles out of commonly used modules
          * E.g. React, React Dom, etc
         */
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor', // See entry.vendor
            filename: 'js/vendor.bundle.js'
        }),
        extractSass
    ],
    externals: {
        /*
         * The config external will be available to the app by using require('config')
         */
        'config': JSON.stringify(config)
    },
    devServer: {
        contentBase: BUILD_DIR,
        compress: true,
        port: 7600,
        inline: true,
    },
};

if (env === 'production') {
    webpackConfig.devtool = 'hidden-source-map';
} else {
    webpackConfig.devtool = 'eval-source-map';
}

module.exports = webpackConfig;

And my dependencies: 我的依赖:

"dependencies": {
  "axios": "^0.16.1",
  "babel-core": "^6.24.0",
  "babel-loader": "^6.4.1",
  "babel-polyfill": "6.5.1",
  "babel-preset-es2015": "^6.24.0",
  "babel-preset-react": "^6.23.0",
  "babel-preset-stage-1": "^6.24.1",
  "chart.js": "^2.6.0",
  "cross-env": "^3.2.4",
  "css-loader": "^0.27.3",
  "enumify": "^1.0.4",
  "extract-text-webpack-plugin": "^2.1.0",
  "history": "^4.6.3",
  "ip": "^1.1.5",
  "lodash": "^4.17.4",
  "moment": "^2.18.1",
  "node-sass": "^4.5.1",
  "prop-types": "^15.5.10",
  "react": "^15.4.2",
  "react-addons-css-transition-group": "^15.5.2",
  "react-addons-transition-group": "^15.5.2",
  "react-chartjs-2": "^2.1.0",
  "react-dom": "^15.4.2",
  "react-js-pagination": "^2.1.0",
  "react-redux": "^5.0.4",
  "react-router": "^4.1.1",
  "react-router-dom": "^4.1.1",
  "reactstrap": "^4.5.0",
  "redux": "^3.6.0",
  "sass-loader": "^6.0.3",
  "sprintf-js": "^1.1.0",
  "style-loader": "^0.16.0",
  "webpack": "^2.3.2"
},
"devDependencies": {
  "eslint-plugin-react": "^6.10.3",
  "webpack-dev-server": "^2.5.1"
}

And my .babelrc: 而我的.babelrc:

{
   "presets" : [
      "es2015",
      "react",
      "stage-1"
   ]
}

EDIT 1 编辑1

Following BANANENMANNFRAU's answer, I added babel-preset-env and edited my .babelrc to be as follows: 按照BANANENMANNFRAU的回答,我添加了babel-preset-env并编辑了我的.babelrc如下:

{
   "presets" : [
      [ "env", {
         "targets": {
            "browsers": [
               "last 5 versions",
               "ie >= 11"
            ]
         }
      }],
      "es2015",
      "react",
      "stage-1"
   ]
}

This didn't help, it still resulted in the error in IE11. 这没有帮助,它仍然导致IE11中的错误。

I know it's over a year later, but I believe the issue was your devtool configuration: 我知道这是一年多了,但我相信问题是你的devtool配置:

webpackConfig.devtool = 'eval-source-map';

IE11 isn't a fan of the eval() code that gets inserted by webpack for those, apparently. 显然,IE11并不是webpack插入的eval()代码的粉丝。 Using webpackConfig.devtool = 'none' (or one of the alternative values ) should fix it. 使用webpackConfig.devtool = 'none' (或其中一个替代值 )应该修复它。

Install babel-preset-env with npm install babel-preset-env --save-dev and use the following config in your .babelrc : 安装通天预置-ENVnpm install babel-preset-env --save-dev ,并在您使用以下配置.babelrc

{
  "presets" : [
    ["env", {
      "targets": {
        "browsers": ["last 2 versions", "ie >= 11"]
      }
    }],
    "react",
  ]
}

You can also remove the following part from your config: 您还可以从配置中删除以下部分:

        loaders: [
            {
                /*
                 * Use Babel to compile JS and JSX files
                 * See .babelrc
                 */
                test: /\.jsx?/,
                include: APP_DIR,
                loader: 'babel-loader'
            }
        ],

Check the docs here 这里查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SCRIPT1002:使用 React + Babel + Webpack 的 IE11 中的语法错误 - SCRIPT1002: Syntax error in IE11 with React + Babel + Webpack 带有 React、Typescript、Babel 和 Webpack 的 IE11 空白页面 - IE11 Blank Page with React, Typescript, Babel, and Webpack Babel Loader和Webpack + React语法错误 - Babel Loader and Webpack + React syntax error 由于 Webpack + React + TypeScript 的长时间运行脚本错误,IE11 没有响应 - IE11 is not responding due to a long running script error with Webpack + React + TypeScript React 开发和构建版本在 IE 11 和 Edge 中不起作用 - Webpack/Babel - React Development and Build version not working in IE 11 and Edge - Webpack/Babel 通过SCRIPT1002反应js createElement:IE11中的语法错误 - React js createElement throughs SCRIPT1002: Syntax error in IE11 IE11及以下版本的React 16:script1002语法错误 - React 16: script1002 syntax error on IE11 and below script1010:语法不明| 无法使用Babel for IE11将ES6转换为ES5 - script1010: unidentified syntax | Not Able to convert ES6 to ES5 using Babel for IE11 React IE11 async导致Expected}错误 - React IE11 async causes Expected } error 将 Webpack 从 v4 迁移到 v5 后,我的 React 应用程序未在 IE11 中运行 - My react app not running in IE11 after migrated Webpack from v4 to v5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM