简体   繁体   English

如何将Babel插件添加到create-react-app项目?

[英]How do I add a Babel plugin to a create-react-app project?

I'm following LearnCode.academy video tutorial and on video #7 at some point the tutor/presenter add the following code 我正在关注LearnCode.academy视频教程,并在视频#7上的某个时候,导师/演示者添加了以下代码

@connect((store) => {
    return {user: store.user.user, tweets: store.tweers.tweers}
})

I understand I need to configure Webpack by adding babel-plugin-transform-decorators-legacy but create-react-app is not showing any of the configuration files. 我了解我需要通过添加babel-plugin-transform-decorators-legacy来配置Webpack,但是create-react-app没有显示任何配置文件。 What is the solution here? 这里有什么解决方案?

While you could eject for decorators it is completely unnecessary and comes with all downsides of ejecting (you don't get future updates to the tooling automatically). 尽管您可以弹出装饰器,但这完全没有必要,并且具有弹出的所有缺点(您不会在将来自动获得该工具的更新)。

Instead I recommend either using learning resources that don't rely on experimental features (which decorators currently are), or learning how to write equivalent code without them. 相反,我建议您使用不依赖实验功能的学习资源(当前是装饰器),或者学习如何在没有实验功能的情况下编写等效代码。

For example: 例如:

class MyComponent extends React.Component {
  // ...
}

export default connect((store) => {
    return {user: store.user.user, tweets: store.tweers.tweers}
})(MyComponent);

If you can't figure out how to write some example without decorators, create a new question and link to it in comments, and I'll try to answer. 如果您不知道如何在不使用修饰符的情况下编写示例,请创建一个新问题并在注释中链接到该问题,然后尝试回答。

In this case, you'll need to run the eject script to get all the configuration, and to change it from its defaults ( this is irreversible, backup before attempting! ). 在这种情况下,您将需要运行弹出脚本来获取所有配置,并将其更改为默认值( 这是不可逆的,请在尝试前进行备份! )。 To do this, run: 为此,请运行:

npm run eject

This should add all the configuration files into your project, such as the Babel where you can add the plugin. 这会将所有配置文件添加到您的项目中,例如可以在其中添加插件的Babel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置 jest 以在 create-react-app 项目中使用自定义 babel 插件? - How do I setup jest to use custom babel plugin in create-react-app project? 我想在 create-react-app 中添加一个 babel 插件 - I want to add a babel plugin inside create-react-app 如何使用create-react-app创建一个新项目 - how do I create a new project with create-react-app 如何在 create-react-app 中添加构造函数? - How do I add Constructor in create-react-app? 在create-react-app中安装babel-plugin-styled-components - Install babel-plugin-styled-components in create-react-app 如何在create-react-app中设置babel-plugin-react-css-modules? - How to setup babel-plugin-react-css-modules in create-react-app? 如何将Redux添加到使用Create-React-App创建的React项目中? - How should I add Redux to a React project created with Create-React-App? 如何在create-react-app生成的项目中将babel装饰器用于mobx - how to use babel decorators for mobx in project generated by create-react-app 如何将 Preact 添加到现有的 create-react-app 项目中? - How to add Preact to existing create-react-app project? 如何在 create-react-app React 项目中使用 useEffect() 引用本地数据文件? - How do I refer to a local data file using useEffect() in a create-react-app React project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM