简体   繁体   English

从 v2.0 开始,“injectBabelPlugin”助手已被弃用。 您可以使用自定义 cra 插件代替

[英]The “injectBabelPlugin” helper has been deprecated as of v2.0. You can use customize-cra plugins in replacement

I am trying to customize my imports using babel.我正在尝试使用 babel 自定义我的导入。 I am following this link:我正在关注这个链接:

https://medium.com/@leonardobrunolima/react-tips-working-with-relative-path-using-create-react-app-fe55c5f97a21 https://medium.com/@leonardobrunolima/react-tips-working-with-relative-path-using-create-react-app-fe55c5f97a21

This is my config-overrides.js这是我的 config-overrides.js

const { injectBabelPlugin } = require('react-app-rewired');

const rootImportConfig = [
    "root-import",
    {
        rootPathPrefix: "~",
        rootPathSuffix: "src"
    }
];

module.exports = config => injectBabelPlugin(rootImportConfig, config);

Package.json: Package.json:

  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",

Currently, this gives me an error that: The "injectBabelPlugin" helper has been deprecated as of v2.0.目前,这给了我一个错误:从 v2.0 开始,“injectBabelPlugin”助手已被弃用。 You can use customize-cra plugins in replacement您可以使用自定义 cra 插件代替

Hence, I installed因此,我安装了

nom install customize-cra react-app-rewired --dev

and changed 'react-app-rewired' to 'customize-cra' in my js file as suggested here: https://github.com/arackaf/customize-cra#available-plugins并按照此处的建议将我的 js 文件中的“react-app-rewired”更改为“customize-cra”: https://github.com/arackaf/customize-cra#available-plugins

However, that still doesn't work since the injectBabelPlugin is also depreciated.但是,这仍然不起作用,因为injectBabelPlugin也已贬值。 What the function should I use here then?那我应该在这里使用什么 function 呢? I tried the config files from here but it doesn't work from me either.我从这里尝试了配置文件,但它对我也不起作用。 It's src-functionality is also different.它的 src 功能也不同。

https://github.com/timarney/react-app-rewired/issues/348 https://github.com/timarney/react-app-rewired/issues/348

How can I fix my config file and imports?如何修复我的配置文件和导入? Instead of代替

import { ResultAlert } from '../../components/alerts/ResultAlert';

I want to do something like this:我想做这样的事情:

import {ResultAlert} from '~/components';

I think you can achieve what you want here without having to resort to hacking the react-scripts .我认为你可以在这里实现你想要的,而不必求助于破解react-scripts Simply use a baseUrl configuration in your jsconfig.js or tsconfig.ts file.只需在jsconfig.jstsconfig.ts文件中使用baseUrl配置。 More details in this blog post: https://dev.to/mr_frontend/absolute-imports-in-create-react-app-3ge8此博客文章中的更多详细信息: https://dev.to/mr_frontend/absolute-imports-in-create-react-app-3ge8

But essentially:但本质上:

{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

Then you can do things like import Home from "components/Home" and it'll just work.然后你可以做一些事情,比如import Home from "components/Home" ,它就可以工作了。

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

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