【问题标题】:react-script causes TypeScript to try to find the wrong file to importreact-script 导致 TypeScript 尝试查找要导入的错误文件
【发布时间】:2020-11-03 12:10:07
【问题描述】:

我使用 React,我还使用 Relay,它会生成一些我的应用程序包含的 TypeScript 文件,并且在更改生成这些文件的目录后,TypeScript 抱怨它找不到它们,但有些奇怪。

例如,TypeScript 编译器是这样说的:

> react-scripts build
Creating an optimized production build...

.\src\pages\MyAccount.tsx
Cannot find file './__generated__/MyAccountMutation.graphql' in '.\src\pages'.


error Command failed with exit code 1.

这是真的,没有/src/pages/__generated__/MyAccountMutation.graphql,但MyAccount.tsx 不需要该文件。导入行是:

import {MyAccountMutation} from "../__generated__/MyAccountMutation.graphql"

没错,有一个/src/pages/../__generated__/MyAccountMutation.graphql(即/src/__generated__/MyAccountMutation.graphql)存在。

运行tsc 工作正常。 React 开发服务器也会出现这个问题。

任何想法为什么 React/TypeScript 会尝试获取错误的文件?

【问题讨论】:

    标签: reactjs typescript relayjs relay


    【解决方案1】:

    在使用 react-scripts 时,您可能也在使用 babel-plugin-relay/macro,对吗?宏也需要配置--artifactDirectory,但在使用 react-scripts 时这是不可能的。您有三个选择:

    1. 从反应脚本中弹出并配置宏
    2. 使用像 CRACO 这样的“配置覆盖”来执行此操作(我没有这方面的经验)。
    3. 不要将--artifactDirectory 用于中继编译器。

    【讨论】:

      猜你喜欢
      • 2019-06-30
      • 2021-12-25
      • 2022-10-06
      • 2018-04-14
      • 1970-01-01
      • 2020-10-07
      • 2019-06-28
      • 1970-01-01
      • 2019-06-06
      相关资源
      最近更新 更多