2017-10-09 855 views
4

我已阅读了有关类似问题的多个主题,并尝试了一些命题,但没有任何结果。对于Internet Explorer 10及以下版本的React,WebPack和Babel会产生SCRIPT1002:语法错误

我已经关注了几个与相关的教程React.jsWebPack 3。因此,除了IE 10及更低版本,此应用程序在所有浏览器(此刻)都能正常运行。错误指向bundle.js(一旦我使用的配置Nr.1):
SCRIPT1002: Syntax error和线 - const url = __webpack_require__(83);

随着配置Nr2时,本地服务器上 - :SCRIPT1002: Syntax error - 符合eval() 而相同的结构,但远程服务器上运行产生一个比特不同的错误:

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"]但它没有任何意义。

那么万一babel加载程序将不会在所有的配置错误或其他什么工作,我认为整个应用程序不会启动。我认为,有些事情应该预设或它们的顺序来完成...从有经验的开发需要咨询

UPDATE 我已经改变了devtool到inline-cheap-module-source-map并得到错误指向overlay.js中 - >const ansiHTML = require('ansi-html');

+0

我已经成功地使用[UglifyJS](https://github.com/mishoo/UglifyJS2)和'--ie8'选项来使您的代码非常有效。 –

+0

hm ..我添加了新的webpack.optimize。UglifyJsPlugin({ 压缩:{警告:假的, screw_ie8:真实, 条件句:真实, 未使用的:真正的, 比较:真, 序列:真实, dead_code:真实, 评价:真, if_return:真, join_vars:true },output:{comments:false} }),'to config ..现在错误指向'(module,exports,__ webpack_require __)'。我在本地运行此测试 - 'npm start' – Kuzma

+0

您可以将* package.json *添加到问题中吗? –

回答

2

在你的package.json文件

变化的WebPack-DEV-服务器的版本到另一个版本 “2.7.1”(或更早)。

"webpack-dev-server": "2.7.1" 

然后做一个NPM安装等就万事大吉了。

这解决了我的问题。

2.7.1之后的所有版本都给了我一个类似于你的错误。

+0

A按照您的建议安装和降级了2.9.1版本,现在它可以在IE10上运行。只有在IE9中抛出'set is undefine'。所以这是一个** webpack-dev-server **错误? – Kuzma

+1

酷!我不知道它是否是一个错误。我可以建议你:大多数教程都是废话,人们不知道他们在说什么,或者他们已经过时了。阅读官方的Webpack文档和教程是更好的。在你的第一个配置中你使用“加载器”和“规则”,但是加载器是针对Webpack 1的,而规则是针对Webpack 2+的,所以奇怪的组合会导致问题。 –

+1

对于IE9等在babel文档中查找* babel-polyfill *。所谓的polyfills将在很多情况下帮助老式浏览器。 –