gpt4 book ai didi

javascript - 如何将 Babel 转译为 es5 而不是 es5.1

转载 作者:行者123 更新时间:2023-11-29 10:29:48 25 4
gpt4 key购买 nike

我正在使用 npm、webpack 和 babel 在 es6 中编写我的库并转译 + 缩小。但是结果被转换为使用对象(Object.defineProperty)的 ecmaScript 5.1,但我的目标是不支持对象的 ecmaScript 5,或者如果发现其他一些限制则降低 ecmaScript。(我需要的 javascript 版本是 Rhino 中使用的版本。)(我的目标是 Rhino 1.7R3 )

我的问题是,如何配置 babel 来做到这一点?我找到了 polyfill,但我不确定如何使用它来实现我的目标。

巴别尔

{  "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
};

};

最佳答案

使用 babel-preset-env 时,您可以使用配置选项来指定要支持的浏览器版本。

不幸的是,无法指定 Rhino,但如果针对的是非常旧的浏览器,例如Internet Explorer 7,有效。

.babelrc

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

配置选项使用名为 browserslist 的模块进行解析.

您可以在线试用browserslist 查询here .

另一个可能对您有帮助的有用工具是 compatibility table .

有趣的是,这表明 Rhino 1.7 确实支持 Object.defineProperty

关于javascript - 如何将 Babel 转译为 es5 而不是 es5.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48803257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com