2017-10-09 858 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將在很多情況下幫助老式瀏覽器。 –