gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:26:07 26 4
gpt4 key购买 nike

我正在尝试阻止 @babel/preset-env + @babel/plugin-transform-runtime + @babel/runtime-corejs3 替换 Date.now(),以解决 an issue preventing @sinonjs/fake-timers from working correctly .

根据documentation for core-js ,应该有办法阻止它应用 es.date.now polyfill,但我不知道如何应用此配置。

例子

index.js

import FakeTimers from '@sinonjs/fake-timers'

FakeTimers.install()

console.log(new Date().getTime())
console.log(Date.now())

Babel 转译

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

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

在查看了 @babel/preset-env 的文档后,我觉得 the exclude option是我要找的东西,但我无法让它工作:

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 在 traspilation 期间排除 es.date.now polyfill?


项目文件

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 . plugin-transform-runtime 不支持浏览器目标。 plugin-transform-runtime 和 preset-env 可以独立工作。

所以,总结一下。目前不支持

关于javascript - 配置 `@babel/runtime-corejs3` 排除 es.date.now,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61020746/

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