简体   繁体   English

是否可以将装饰器与 create-react-app 一起使用?

[英]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-rewiredcustomize-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启用配置自定义

  1. npm install customize-cra --save-dev npm 安装定制-cra --save-dev
  2. npm install react-app-rewired --save-dev npm 安装react-app- rewired --save-dev

To enable decorators启用装饰器

  1. npm install @babel/plugin-proposal-decorators --save-dev npm 安装@babel/plugin-proposal-decorators --save-dev
  2. npm install reflect-metadata --save-dev
  3. add import 'reflect-metadata' in index.js在 index.js 中添加import 'reflect-metadata'

Update configs更新配置

  1. remove babel config from package.json从 package.json 中删除babel配置
  2. create config-overrides.js file in project root在项目根目录中创建config-overrides.js文件
  3. add code:添加代码:
 const { addDecoratorsLegacy, override } = require('customize-cra') module.exports = override(addDecoratorsLegacy())
  1. Change scrips section in package.jsonscrips中更改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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM