[英]Is it possible to use decorators with create-react-app?
I have understood that since Babel doesn't support decorators out of the box (because it's in early stages of definition) create-react-app doesn't support it as well.我了解到,由于 Babel 不支持开箱即用的装饰器(因为它处于定义的早期阶段),create-react-app 也不支持它。
I know that you can eject the generated app and configure Babel to support them, but I don't want to do that.我知道您可以弹出生成的应用程序并配置 Babel 以支持它们,但我不想那样做。
Finally, libraries like MobX allow you to use decorator's behavior without actually using them, with the help of some utility functions like described at https://mobx.js.org/best/decorators.html最后,在https://mobx.js.org/best/decorators.html中描述的一些实用函数的帮助下,像 MobX 这样的库允许您使用装饰器的行为而无需实际使用它们
Is there something similar for React? React 有类似的东西吗?
Yes, you can, but you need a couple of things,是的,你可以,但你需要一些东西,
Make sure you have react-app-rewired and customize-cra installed so you can override webpack and babel configs确保您已安装react-app-rewired和customize-cra ,以便您可以覆盖 webpack 和 babel 配置
install @babel/plugin-proposal-decorators安装@babel/plugin-proposal-decorators
and update your config-overrides.js
file:并更新您的
config-overrides.js
文件:
const { override, addBabelPlugin } = require("customize-cra");
const pluginProposalDecorators = require("@babel/plugin-proposal-decorators");
module.exports = override(
addBabelPlugin(pluginProposalDecorators)
);
It is also possible to use decorators with create-react-app by adding tsconfig.json file to the project that means enabling support for typescript.也可以通过将 tsconfig.json 文件添加到项目中来使用带有 create-react-app 的装饰器,这意味着启用对 typescript 的支持。
You need to set experimentalDecorators
to true
in compiler options of tsconfig.json您需要在
experimentalDecorators
的编译器选项中将 ExperimentDecorators 设置为true
Hope we are aware the.js and.tsx files can co-exist, so whichever file we want to use decorators we can convert to.tsx rest can remain.js files希望我们知道 .js 和 .tsx 文件可以共存,所以无论我们要使用装饰器的哪个文件都可以转换为 .tsx rest 可以保留 .js 文件
{
"compilerOptions": {
"target": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"noImplicitThis": false,
"experimentalDecorators": true, //<----
"types": ["cypress"]
},
"include": ["src"]
}
In my case, it was a combination of plugins就我而言,它是插件的组合
To enable config customization
启用配置自定义
To enable decorators
启用装饰器
npm install reflect-metadata --save-dev
import 'reflect-metadata'
in index.jsimport 'reflect-metadata'
Update configs
更新配置
babel
config from package.jsonbabel
配置config-overrides.js
file in project rootconfig-overrides.js
文件const { addDecoratorsLegacy, override } = require('customize-cra') module.exports = override(addDecoratorsLegacy())
scrips
section in package.json
scrips
中更改package.json
部分"scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-app-rewired eject" },
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.