配置`@babel/runtime-corejs3`以排除es.date.now



我正试图阻止@babel/preset-env+@babel/plugin-transform-runtime+@babel/runtime-corejs3替换Date.now(),以解决阻止@sinonjs/fake-timers正常工作的问题。

根据core-js的文档,应该有一种方法可以防止它应用es.date.nowpolyfill,但我不知道如何应用这个配置。

示例

index.js

import FakeTimers from '@sinonjs/fake-timers'
FakeTimers.install()
console.log(new Date().getTime())
console.log(Date.now())

Babel Transpile

$(npm bin)/babel index.js
...
console.log(new Date().getTime());
console.log((0, _now["default"])());

注意转换输出的最后一行——我希望它是console.log(Date.now());

在查看了@babel/preset-env的文档后,我觉得exclude选项是我想要的,但我无法让它发挥作用:

module.exports = {
presets: [
['@babel/preset-env', { exclude: ['es.date.now'] }]
],
plugins: [
['@babel/plugin-transform-runtime', { corejs: 3 }]
]
}

上面给了我一个错误:

> $(npm bin)/babel index.js
{ Invariant Violation: [BABEL] ./index.js: Invalid Option: The plugins/built-ins 'es.date.now' passed to the 'exclude' option are not
valid. Please check data/[plugin-features|built-in-features].js in babel-preset-env (While processing: "./node_modules/@babel/preset-env/lib/index.js")
...

如何配置Babel以在传输过程中排除es.date.nowpolyfill


项目文件

package.json

{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/plugin-transform-runtime": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@babel/register": "^7.9.0"
},
"dependencies": {
"@babel/runtime": "^7.9.2",
"@babel/runtime-corejs3": "^7.9.2",
"@sinonjs/fake-timers": "^6.0.1"
}
}

babel.config.js

module.exports = {
presets: ['@babel/preset-env'],
plugins: [
['@babel/plugin-transform-runtime', { corejs: 3 }]
]
}

请参阅https://github.com/babel/babel/issues/10008.插件转换运行时不支持浏览器目标。插件转换运行时和预设环境可以独立工作。

综上所述。目前不支持

最新更新