Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack, IE8 and ES6 modules

Recent versions of webpack do not support IE8. I have tried with 1.12.12 (which I believed was the last version to support IE8) but still get errors from the un-shimmable Object.defineProperty.

https://github.com/webpack/webpack/issues/2085

What was the last version of webpack to support IE8? Did it ever work with ES6 modules?

webpack.config.js:

var webpack = require("webpack");
var es3ifyPlugin = require('es3ify-webpack-plugin');
var productionPlugin = new webpack.DefinePlugin({
    'process.env': {
        'NODE_ENV': JSON.stringify('production')
    }
});
var devPlugin = new webpack.DefinePlugin({
    "process.env": {
        NODE_ENV: JSON.stringify("dev")
    }
});

module.exports = {
    entry: {
        assessment: "./src/aaa/app.js"
    },
    //devtool: "source-map",
    output: {
        path: "../AAA/wwwroot/js",
        filename: "[name].bundle.js",
        publicPath: "/"
    },
    resolve: {
        extensions: ["", ".js"]
    },
    module: {
        preLoaders: [
            {
                test: /\.js$/,
                loader: "eslint-loader",
                exclude: "node_modules"
            }
        ],
        loaders: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            },
            {
                // todo: move less compiling to web project 
                test: /\.less$/,
                loader: "style-loader!css-loader!less-loader",
                exclude: "node_modules"
            },
        ]
    },
    devServer: {
        historyApiFallback: true,
        inline: true,
        proxy: {
            "/api": {
                "target": {
                    host: "localhost",
                    protocol: "http:",
                    port: "58211",
                },
                changeOrigin: true,
                pathRewrite: { "^/api": "" }
            }
        },
        publicPath: "/assets/"
    },
    plugins: [
        new es3ifyPlugin(),
        new webpack.optimize.CommonsChunkPlugin({
            name: "vendor",
            minChunks: isExternal
        }),
        productionPlugin
        //devPlugin
    ]
}

function isExternal(module) {
    var userRequest = module.userRequest;

    if (typeof userRequest !== "string") {
        return false;
    }

    return userRequest.indexOf("node_modules") >= 0;
}

Transpiled code looks like this:

exports.test = '123';

webpackJsonp([1, 0], [

  function (module, exports, __webpack_require__) {

    'use strict';

    var _imported = __webpack_require__(1);


    alert('test ' + _imported.test);

    function(module, exports) {

        "use strict";

        Object.defineProperty(exports, "__esModule", {
            value: true
        });
        var test = exports.test = 123;

      }
  ]);
like image 585
Tom Avatar asked Dec 20 '16 17:12

Tom


1 Answers

The problem is how babel translates ES2015 code by default. In the default (non-loose) mode it uses Object.defineProperty, but this can be configured by using loose-mode.

// webpack.config.js
module: {
   loaders: [
     {
       test: /\.js$/,
       loader: 'babel-loader',
       exclude: /node_modules/,
       query: {
         presets: [ ['es2015', {"loose": true}] ]
       }
     }
   ]
 }

This leads to the following compiled code:

/* 0 */
/***/ function(module, exports, __webpack_require__) {

    'use strict';

    var _imported = __webpack_require__(1);

    var _imported2 = _interopRequireDefault(_imported);

    function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

    console.log('test ' + _imported2.default);

/***/ },
/* 1 */
/***/ function(module, exports) {

    "use strict";

    exports.__esModule = true;
    var test = 123;

    exports.default = test;

/***/ }
/******/ ])

Old answer

From the discussion as this has solved your issue:

1.13.0 was released on 15th April 2016. So It seems that this should work. Are you sure that you're not using getters/setters within your own application code? Maybe you can create a super simple example with just a hello world sample code and try creating a build using the 1.13 version. The so created code should not use Object.defineProperty I guess.

like image 94
dotcs Avatar answered Oct 19 '22 08:10

dotcs