webpack babel 前置 IE 11
webpack babel prepend IE 11
i' 在 webpack 中使用 babel polyfill 来转换代码。我很惊讶 IE 11 显示错误:IE 11 Object doesn't support property or method 'prepend'
is not pollyfill should be add this function?我知道有一些重复我只是不知道如何配置 webpack 。我尝试了几种方法,但我必须手动添加前置函数,这样 IE 才能找到它。
与 babel 相关的包:
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/preset-env": "^7.3.1",
"ajv": "^6.7.0",
"autoprefixer": "^9.4.6",
"babel-loader": "^8.0.5",
"breakpoint-sass": "^2.7.1",
"browserslist": "^4.4.1",
"clean-webpack-plugin": "^1.0.1",
"copy-webpack-plugin": "^4.6.0",
"css-loader": "^2.1.0"
}
webpack.config.js:
module.exports = ( env, options ) => {
return {
entry : ['@babel/polyfill', './source/_assets/app.js'],
output : {
path : path.resolve( __dirname, 'public/dist' ),
filename : 'bundle.js',
chunkFilename : '[name].bundle.js',
publicPath : ( options.mode === 'production' ) ? '/themes/custom/avonis/public/dist/' : '../../dist/'
}, ...
{
test : /\.js$/,
exclude : /node_modules/,
use : {
loader : 'babel-loader',
options : {
presets : [ '@babel/preset-env' ],
plugins : [ '@babel/plugin-syntax-dynamic-import' ]
}
}
},
我在入口文件中添加了这样的函数app.js
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty('append')) {
return;
}
Object.defineProperty(item, 'append', {
configurable: true,
enumerable: true,
writable: true,
value: function append() {
var argArr = Array.prototype.slice.call(arguments),
docFrag = document.createDocumentFragment();
argArr.forEach(function (argItem) {
var isNode = argItem instanceof Node;
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
});
this.appendChild(docFrag);
}
});
});
})([Element.prototype, Document.prototype DocumentFragment.prototype]);
我发现 babel 没有这些方法。我需要手动添加它们。
i' 在 webpack 中使用 babel polyfill 来转换代码。我很惊讶 IE 11 显示错误:IE 11 Object doesn't support property or method 'prepend'
is not pollyfill should be add this function?我知道有一些重复我只是不知道如何配置 webpack 。我尝试了几种方法,但我必须手动添加前置函数,这样 IE 才能找到它。
与 babel 相关的包:
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/preset-env": "^7.3.1",
"ajv": "^6.7.0",
"autoprefixer": "^9.4.6",
"babel-loader": "^8.0.5",
"breakpoint-sass": "^2.7.1",
"browserslist": "^4.4.1",
"clean-webpack-plugin": "^1.0.1",
"copy-webpack-plugin": "^4.6.0",
"css-loader": "^2.1.0"
}
webpack.config.js:
module.exports = ( env, options ) => {
return {
entry : ['@babel/polyfill', './source/_assets/app.js'],
output : {
path : path.resolve( __dirname, 'public/dist' ),
filename : 'bundle.js',
chunkFilename : '[name].bundle.js',
publicPath : ( options.mode === 'production' ) ? '/themes/custom/avonis/public/dist/' : '../../dist/'
}, ...
{
test : /\.js$/,
exclude : /node_modules/,
use : {
loader : 'babel-loader',
options : {
presets : [ '@babel/preset-env' ],
plugins : [ '@babel/plugin-syntax-dynamic-import' ]
}
}
},
我在入口文件中添加了这样的函数app.js
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty('append')) {
return;
}
Object.defineProperty(item, 'append', {
configurable: true,
enumerable: true,
writable: true,
value: function append() {
var argArr = Array.prototype.slice.call(arguments),
docFrag = document.createDocumentFragment();
argArr.forEach(function (argItem) {
var isNode = argItem instanceof Node;
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
});
this.appendChild(docFrag);
}
});
});
})([Element.prototype, Document.prototype DocumentFragment.prototype]);
我发现 babel 没有这些方法。我需要手动添加它们。