Search code examples
webpackbabel-polyfill

webpack babel prepend IE 11


i' using babel polyfill within webpack to transpile code. And i was surprised that IE 11 shows the error : IE 11 Object doesn't support property or method 'prepend' is not pollyfill supposed to be add this function ? i know there are some duplication i just didn't know how to configure webpack . i tried several ways , but i had to add prepend function manually so IE will find it.

packages related to 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' ]
        }
      }
    },

i was adding function like this in the entry file 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]);

Solution

  • i figured out babel , doesn't have those methods. i needed to add them manually.