我们正在更新我们的API,来自Babel的Henry Zhu提醒我这个名为babel-preset-env
的预设,以取代对babel-preset-es2015
和babel-preset-es2018
的需求。
现在,我在理解处理所有事情的最简单方法时遇到了困难。
如果我只显示配置的当前位置,这将更容易:
.babelrc
{
"presets": [
"env",
{
"targets": {
"node": "current"
}
},
"jest"
]
}
的package.json
{
"scripts": {
"test": "node --harmony-async-await node_modules/jest/bin/jest.js",
"start:local": "NODE_ENV=localhost npm run babel-node -- warpcore/server.js",
"start": "npm run babel-node -- warpcore/server.js",
"babel-node": "babel-node --presets=es2015,stage-2"
},
"dependencies": {
"babel-polyfill": "^6.23.0"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.3",
"babel-jest": "^20.0.3",
"babel-preset-env": "^1.6.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2018": "^1.0.0",
"babel-preset-stage-2": "^6.24.1",
"jest": "^20.0.4"
},
"jest": {
"testURL": "http://localhost:8080",
"testEnvironment": "node"
}
}
我不确定如何组织这些事情以最好地实现上面的子弹清单。
我应该做出哪些改变?
.babelrc
文件不是最佳答案 0 :(得分:4)
我想我已经开始工作了。这是解决方案:
.babelrc
问题中发布的内容有语法错误,因为env预设需要包含在方括号[]中(来自:http://babeljs.io/docs/plugins/preset-env/)
正确:
{
"presets": [
["env",
{
"targets": {
"node": "current"
}
}],
"jest"
]
}
的package.json
问题中发布的内容有一些可以删除的内容:
{
"scripts": {
"test": "jest --verbose",
"start:local": "cross-env NODE_ENV=localhost babel-node -- app.js",
"babel-node": "babel-node --presets=env"
},
"dependencies": {
"babel-cli": "^6.24.1",
"babel-preset-env": "^1.6.0"
},
"devDependencies": {
"babel-eslint": "^7.2.3",
"babel-jest": "^20.0.3",
"jest": "^20.0.4"
},
"jest": {
"testURL": "http://localhost:8080",
"testEnvironment": "node"
}
}
我认为更清洁。如果要明确包含或排除任何文件,可以从.babelrc
文件调整预设,或指定要支持的浏览器。
答案 1 :(得分:4)
如果您想使用babel-preset-env替换Jest的babel-preset-es2015(已弃用),那么您必须确保" modules&#34 ;在你的" env"配置设置为" commonjs"。
以下是示例性配置:
<强> .babelrc 强>
{
"env": {
"test": {
"plugins": [
"transform-class-properties",
"transform-object-rest-spread"
],
"presets": [
"jest",
"react",
[
"env",
{
"debug": false,
"modules": "commonjs",
"targets": {
"node": "current"
},
"useBuiltIns": true
}
]
]
}
}
}
您可以在env.test
中看到预设env
(&#34; babel-preset-env&#34;配置)有&#34;模块&#34;设置为&#34; commonjs&#34;。这很重要,否则你会得到#34; SyntaxError:意外的令牌导入&#34;。
为了完整性,这是一个简单的测试:
<强> ExampleButton.test.jsx 强>
import ExampleButton from './ExampleButton';
import React from 'react';
import renderer from 'react-test-renderer';
test('Example Test', () => {
const component = renderer.create(<ExampleButton />);
const json = component.toJSON();
expect(json.type).toBe('button');
});
<强> ExampleButton.jsx 强>
import React from 'react';
class ExampleButton extends React.Component {
render() {
return (
<button onClick={this.props.onClick}>
{this.props.text}
</button>
)
}
}
export default ExampleButton;
对于我的Babel设置,我使用了以下依赖项:
"babel-core": "6.26.0",
"babel-jest": "21.2.0",
"babel-plugin-transform-class-properties": "6.24.1",
"babel-plugin-transform-object-rest-spread": "6.26.0",
"babel-preset-env": "1.6.1",
"babel-preset-react": "6.24.1",
"jest": "21.2.1",
"react-test-renderer": "16.1.1",
答案 2 :(得分:0)
这是我找到的解决方案:
{
"presets": ["@babel/env", "@babel/react"]
}