简体   繁体   中英

How to Babel transpile to es5 not es5.1

I am using npm, webpack and babel writing my library in es6 and transpiling + minifying. But the result is transpiled to ecmaScript 5.1 which uses Object (Object.defineProperty), but my target is ecmaScript 5 which doesnt support Object, or lower ecmaScript if some other limitation is discovered. (The version of javascript i need is the one used in Rhino .) (I am targeting Rhino 1.7R3 )

My question is, how to configure babel to do it? I found out polyfill but i am not sure how to use it to achieve my goal.

babelrc

{  "presets": ["env"]  }

webpack.config

    module.exports = function(env, argv) {
    env = env || {};

    const webpack = require('webpack');
    const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin;

    let libraryName = 'jsgmc';
    let plugins = [], outputFile;

    if (env.prod) {
        plugins.push(new UglifyJsPlugin({minimize: true}));
        outputFile = libraryName + '.min.js';
    } else {
        outputFile = libraryName + '.js';
    }

    return {
        entry: __dirname + '/src/jsgmc.js',
        devtool: 'source-map',
        output: {
            path: __dirname + '/dist',
            filename: outputFile,
            library: libraryName,
            libraryTarget: 'this'
        },
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: /(node_modules|bower_components)/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: ['babel-preset-env'],
                            plugins: [require('babel-plugin-add-module-exports')]
                        }
                    }
                }
            ]
        },
        plugins: plugins
    };

};

When using babel-preset-env , you can use a configuration option to specify which browser versions you want to support.

Unfortunately, it is not possible to specify Rhino, but you could try if targeting a really old browser, eg Internet Explorer 7, works.

.babelrc

{
  "presets": [
    ["env", {
      "targets": {
        "browsers": ["ie < 8"]
      }
    }]
  ]
}

The configuration options are parsed using a module called browserslist .

You can try out browserslist queries online here .

Another useful tool that may help you is this compatibility table .

Interestingly, this states that Rhino 1.7 does support Object.defineProperty .

As Patrick mentioned, Rhino 1.7 and lower doesn't support Object.defineProperty. Appropriate solution for me was to use this simple polyfil . Note that this solution is viable only because the library i am building will not have advanced features. Otherwise other functions would have to be polyfilled with egsham es5 polyfill which is highly experimental. (its equivalent shim es5 is not containg eg Object feature)

Transpiling into sub-versions of javascript is not possible. It is either 5.1 or nothing. It is also possible to transpile to previous major version es3 with closure compiler (already old tool), but it could lead to some major issues as this is very old js spec nowadays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM