我已阅读了有关类似问题的多个主题,并尝试了一些命题,但没有任何结果。对于Internet Explorer 10及以下版本的React,WebPack和Babel会产生SCRIPT1002:语法错误
我已经关注了几个与相关的教程React.js和WebPack 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');
我已经成功地使用[UglifyJS](https://github.com/mishoo/UglifyJS2)和'--ie8'选项来使您的代码非常有效。 –
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
您可以将* package.json *添加到问题中吗? –