I'm trying to prevent @babel/preset-env + @babel/plugin-transform-runtime + @babel/runtime-corejs3 from replacing Date.now(), in order to work around an issue preventing @sinonjs/fake-timers from working correctly.
According to the documentation for core-js, there should be a way to prevent it from applying the es.date.now polyfill, but I cannot figure out how to apply this configuration.
Example
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"])());
Note the last line of the traspilation output — I want it to be console.log(Date.now());
After reviewing the docs for @babel/preset-env, I feel like the exclude option is what I'm looking for, but I can't get it to work:
module.exports = {
presets: [
['@babel/preset-env', { exclude: ['es.date.now'] }]
],
plugins: [
['@babel/plugin-transform-runtime', { corejs: 3 }]
]
}
The above gives me an error:
> $(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")
...
How do I configure Babel to exclude the es.date.now polyfill during traspilation?
Project Files
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 }]
]
}