Internet Explorer 10及以下版本的React,WebPack和Babel产生了SCRIPT1002:语法错误 [英] React, WebPack and Babel for Internet Explorer 10 and below produces SCRIPT1002: Syntax error

查看:417
本文介绍了Internet Explorer 10及以下版本的React,WebPack和Babel产生了SCRIPT1002:语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了有关类似问题的多个主题,并尝试了一些建议,但没有结果.

I've read multiple threads regarding similar issues and tried some propositions, but had no results.

我已经关注了与 React.js WebPack 3 相关的一些教程.结果,除了 IE 10 及以下版本之外,该应用程序在所有浏览器上均能正常运行(目前).错误指向bundle.js(一旦我使用配置Nr.1):
SCRIPT1002: Syntax error和行-const url = __webpack_require__(83);

I've followed few tutorials related to React.js and WebPack 3. As the result the application is working well on all browsers (at this moment) except IE 10 and below. The error points to bundle.js (once I'm using the configuration Nr.1):
SCRIPT1002: Syntax error and the line - const url = __webpack_require__(83);

在本地服务器上具有配置Nr2.-:SCRIPT1002: Syntax error-与eval()一致 相同的配置,但是在远程服务器上运行会产生一些不同的错误:

With configuration Nr2., on local server - : SCRIPT1002: Syntax error - line with eval() And the same configuration, but running on remote server produces a bit different error:

SCRIPT5009: 'Set' is undefine

WebPack配置Nr1.:

const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: './index.html',
  filename: 'index.html',
  inject: 'body'
})
module.exports = {
  entry: './index.js',
  output: {
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.json$/,
        exclude: /node_modules/,
        loader: 'json-loader'
      },
      {
        test: /\.css$/,
        loader: "style-loader!css-loader"
      }
    ],
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules)/,      
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['env', 'react']
          }
        }
      }
    ]
 },
   devServer: {   
      historyApiFallback: true,
      contentBase: './'
  },
 plugins: [HtmlWebpackPluginConfig]
}

WebPack配置Nr2.:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const PreloadWebpackPlugin = require('preload-webpack-plugin');
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
const StyleExtHtmlWebpackPlugin = require('style-ext-html-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const autoprefixer = require('autoprefixer');

const staticSourcePath = path.join(__dirname, 'static');
const sourcePath = path.join(__dirname);
const buildPath = path.join(__dirname, 'dist');

module.exports = {
    stats: {
        warnings: false
    },
    devtool: 'cheap-eval-source-map',
          devServer: {    
          historyApiFallback: true,
          contentBase: './'
      },
    entry: {
        app: path.resolve(sourcePath, 'index.js')
    },
    output: {
        path: path.join(__dirname, 'dist'),
        filename: '[name].[chunkhash].js',
        publicPath: '/'
    },
    resolve: {
        extensions: ['.webpack-loader.js', '.web-loader.js', '.loader.js', '.js', '.jsx'],
        modules: [
            sourcePath,
            path.resolve(__dirname, 'node_modules')
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify('production')
        }),
        new webpack.optimize.ModuleConcatenationPlugin(),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            filename: 'vendor.[chunkhash].js',
            minChunks: Infinity
        }),
        new webpack.LoaderOptionsPlugin({
            options: {
                postcss: [
                    autoprefixer({
                        browsers: [
                            'last 3 version',
                            'ie >= 10'
                        ]
                    })
                ],
                context: staticSourcePath
            }
        }),
        new webpack.HashedModuleIdsPlugin(),
        new HtmlWebpackPlugin({
            template: path.join(__dirname, 'index.html'),
            path: buildPath,
            excludeChunks: ['base'],
            filename: 'index.html',
            minify: {
                collapseWhitespace: true,
                collapseInlineTagWhitespace: true,
                removeComments: true,
                removeRedundantAttributes: true
            }
        }),
        new PreloadWebpackPlugin({
            rel: 'preload',
            as: 'script',
            include: 'all',
            fileBlacklist: [/\.(css|map)$/, /base?.+/]
        }),
        new webpack.NoEmitOnErrorsPlugin(),
        new CompressionPlugin({
            asset: '[path].gz[query]',
            algorithm: 'gzip',
            test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
            threshold: 10240,
            minRatio: 0.8
        })      
    ],
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                  loader: 'babel-loader',
                  options: {
                    presets: ['env', 'react', 'es2015'],
                    plugins: ["transform-es2015-arrow-functions"]
                  }
                },
                include: sourcePath
            },
            {                
                test: /\.css$/,
                exclude: /node_modules/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: [
                        { loader: 'css-loader', options: { minimize: true } },
                        'postcss-loader',
                        'sass-loader'
                    ]
                })
            },
            {
                test: /\.(eot?.+|svg?.+|ttf?.+|otf?.+|woff?.+|woff2?.+)$/,
                use: 'file-loader?name=assets/[name]-[hash].[ext]'
            },
            {
                test: /\.(png|gif|jpg|svg)$/,
                use: [
                    'url-loader?limit=20480&name=assets/[name]-[hash].[ext]'
                ],
                include: staticSourcePath
            }
        ]
    }
}; 

在这里我还添加了 es2015 到预设:['env', 'react', 'es2015']plugins: ["transform-es2015-arrow-functions"],但这没有任何意义.

Here additionally I've added the es2015 to presets: ['env', 'react', 'es2015'] and plugins: ["transform-es2015-arrow-functions"] but it made no sense.

如果babel加载程序在所有错误配置或其他任何原因下均无法正常工作,我认为整个应用程序将无法启动.我认为应该使用预设或它们的顺序来完成某些工作...需要经验丰富的开发人员的建议

Well in case when the babel loader won't work at all of misconfiguration or something else, I think that the whole application won't start. I believe that something should be done with presets or their order... Need advice from experienced developer

更新 我已将devtool更改为inline-cheap-module-source-map,并获得了指向overlay.js-> const ansiHTML = require('ansi-html');

UPDATE I've changed devtool to inline-cheap-module-source-map and got error point to overlay.js -> const ansiHTML = require('ansi-html');

推荐答案

在您的 package.json 文件中

webpack-dev-server 的版本更改为版本"2.7.1"(或更早版本).

change the version of webpack-dev-server to version "2.7.1" (or earlier).

"webpack-dev-server": "2.7.1"

然后进行 npm安装等.

那为我解决了这个问题.

That solved the problem for me.

2.7.1之后的所有版本都会给我一个类似于您的错误.

All versions after 2.7.1 gives me an error similar to yours.

这篇关于Internet Explorer 10及以下版本的React,WebPack和Babel产生了SCRIPT1002:语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆