简体   繁体   English

如何将Babel转换为es5而不是es5.1

[英]How to Babel transpile to es5 not es5.1

I am using npm, webpack and babel writing my library in es6 and transpiling + minifying. 我正在使用npm,webpack和babel在es6中编写我的库并进行转换+缩小。 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. 但结果转换为使用Object(Object.defineProperty)的ecmaScript 5.1,但我的目标是不支持Object的ecmaScript 5,或者如果发现其他限制则降低ecmaScript。 (The version of javascript i need is the one used in Rhino .) (I am targeting Rhino 1.7R3 ) (我需要的javascript版本是Rhino中使用的版本。)(我的目标是Rhino 1.7R3

My question is, how to configure babel to do it? 我的问题是,如何配置babel来做呢? I found out polyfill but i am not sure how to use it to achieve my goal. 我发现了polyfill,但我不知道如何使用它来实现我的目标。

babelrc babelrc

{  "presets": ["env"]  }

webpack.config 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. 使用babel-preset-env时 ,您可以使用配置选项指定要支持的浏览器版本。

Unfortunately, it is not possible to specify Rhino, but you could try if targeting a really old browser, eg Internet Explorer 7, works. 遗憾的是,无法指定Rhino,但如果定位一个非常古老的浏览器(例如Internet Explorer 7),则可以尝试使用。

.babelrc .babelrc

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

The configuration options are parsed using a module called browserslist . 使用名为browserslist的模块解析配置选项。

You can try out browserslist queries online here . 您可以在此处在线试用browserslist查询。

Another useful tool that may help you is this compatibility table . 另一个可以帮助您的有用工具是此兼容性表

Interestingly, this states that Rhino 1.7 does support Object.defineProperty . 有趣的是,这表明Rhino 1.7确实支持Object.defineProperty

As Patrick mentioned, Rhino 1.7 and lower doesn't support Object.defineProperty. 正如Patrick所说,Rhino 1.7及更低版本不支持Object.defineProperty。 Appropriate solution for me was to use this simple polyfil . 对我来说合适的解决方案是使用这种简单的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. 否则,其他功能必须用egsham es5 polyfill进行多层填充,这是高度实验性的。 (its equivalent shim es5 is not containg eg Object feature) (它的等效垫片es5不包含例如对象特征)

Transpiling into sub-versions of javascript is not possible. 无法透明化为javascript的子版本。 It is either 5.1 or nothing. 这是5.1或什么都没有。 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. 也可以使用闭包编译器(已经是旧工具)转换到以前的主要版本es3,但它可能会导致一些重大问题,因为现在这是非常古老的js规范。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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