问题出在构建钩子的导入和在任何主机上构建时。

huangapple go评论70阅读模式
英文:

Problem with build hooks imports and building on any host

问题

我在Vercel上构建项目时遇到了问题。它在我的本地机器上成功构建,但是当我部署到任何主机上时,我遇到了一个导入问题,我无法解决。

以下是在构建过程中收到的错误消息:

[12:07:15.658] src/Components/PlatformSelector/PlatformSelector.tsx(5,26): error TS2307: 无法找到模块'../../Hooks/usePlatforms'或其相应的类型声明。
[12:07:15.659] src/Hooks/usePlatform.ts(1,26): error TS2307: 无法找到模块'./usePlatforms'或其相应的类型声明。
[12:07:15.694] 错误: 命令“npm run build”退出代码2

这是usePlatform钩子的代码:

import Platform from "../Entities/Platform";
import usePlatforms from "./usePlatforms";

const usePlatform = (id?: number) => {
    const {data: platforms} = usePlatforms();

    return platforms?.results.find((p: Platform) => p.id === id)
}

export default usePlatform;

这是usePlatforms钩子的代码:

import { useQuery } from '@tanstack/react-query';
import ms from 'ms';
import platforms from '../data/platforms';
import Platform from '../entities/Platform';
import APIClient from '../services/api-client';

const apiClient = new APIClient<Platform>(
  '/platforms/lists/parents'
);

const usePlatforms = () =>
  useQuery({
    queryKey: ['platforms'],
    queryFn: apiClient.getAll,
    staleTime: ms('24h'),
    initialData: platforms,
  });

export default usePlatforms;

最后,这是PlatformSelector组件的代码:

import { Button, Menu, MenuButton, MenuItem, MenuList } from "@chakra-ui/react";
import { BsChevronDown } from "react-icons/bs";
import Platform from "../../Entities/Platform";
import usePlatform from "../../Hooks/usePlatform";
import usePlatforms from "../../Hooks/usePlatforms";
import useGameQueryStore from "../../store";

const PlatformSelector = () => {
  const setPlatformId = useGameQueryStore((state) => state.setPlatformId);
  const selectedPlatformId = useGameQueryStore(
    (state) => state.gameQuery.platformId
  );

  const selectedPlatform = usePlatform(selectedPlatformId);
  const { data, error } = usePlatforms();

  if (error) return null;

  return (
    <Menu>
      <MenuButton as={Button} rightIcon={<BsChevronDown />}>
        {selectedPlatform?.name || "Platforms"}
      </MenuButton>
      <MenuList>
        {data?.results.map((platform: Platform) => (
          <MenuItem
            onClick={() => setPlatformId(platform.id)}
            key={platform.id}
          >
            {platform.name}
          </MenuItem>
        ))}
      </MenuList>
    </Menu>
  );
};

export default PlatformSelector;

之前,我遇到了usePlatforms钩子的大小写问题,最初它以大写字母命名。我更改了标题并像这样调整了TypeScript配置:

"forceConsistentCasingInFileNames": false

我收到了上述提到的错误。如何解决这个问题?

英文:

I'm having trouble building my project on Vercel. It builds successfully on my local machine, but when I deploy it on any host, I encounter an import issue that I can't figure out.

Here is the error message I receive during the build process:

[12:07:15.658] src/Components/PlatformSelector/PlatformSelector.tsx(5,26): error TS2307: Cannot find module &#39;../../Hooks/usePlatforms&#39; or its corresponding type declarations.
[12:07:15.659] src/Hooks/usePlatform.ts(1,26): error TS2307: Cannot find module &#39;./usePlatforms&#39; or its corresponding type declarations.
[12:07:15.694] Error: Command &quot;npm run build&quot; exited with 2

Here is the code for the usePlatform hook:

import Platform from &quot;../Entities/Platform&quot;;
import usePlatforms from &quot;./usePlatforms&quot;;

const usePlatform = (id?: number) =&gt; {
    const {data: platforms} = usePlatforms();

    return platforms?.results.find((p: Platform) =&gt; p.id === id)
}

export default usePlatform;

And here is the code for the usePlatforms hook:

import { useQuery } from &#39;@tanstack/react-query&#39;;
import ms from &#39;ms&#39;;
import platforms from &#39;../data/platforms&#39;;
import Platform from &#39;../entities/Platform&#39;;
import APIClient from &#39;../services/api-client&#39;;

const apiClient = new APIClient&lt;Platform&gt;(
  &#39;/platforms/lists/parents&#39;
);

const usePlatforms = () =&gt;
  useQuery({
    queryKey: [&#39;platforms&#39;],
    queryFn: apiClient.getAll,
    staleTime: ms(&#39;24h&#39;),
    initialData: platforms,
  });

export default usePlatforms;

Finally, here is the code for the PlatformSelector component:

import { Button, Menu, MenuButton, MenuItem, MenuList } from &quot;@chakra-ui/react&quot;;
import { BsChevronDown } from &quot;react-icons/bs&quot;;
import Platform from &quot;../../Entities/Platform&quot;;
import usePlatform from &quot;../../Hooks/usePlatform&quot;;
import usePlatforms from &quot;../../Hooks/usePlatforms&quot;;
import useGameQueryStore from &quot;../../store&quot;;

const PlatformSelector = () =&gt; {
  const setPlatformId = useGameQueryStore((state) =&gt; state.setPlatformId);
  const selectedPlatformId = useGameQueryStore(
    (state) =&gt; state.gameQuery.platformId
  );

  const selectedPlatform = usePlatform(selectedPlatformId);
  const { data, error } = usePlatforms();

  if (error) return null;

  return (
    &lt;Menu&gt;
      &lt;MenuButton as={Button} rightIcon={&lt;BsChevronDown /&gt;}&gt;
        {selectedPlatform?.name || &quot;Platforms&quot;}
      &lt;/MenuButton&gt;
      &lt;MenuList&gt;
        {data?.results.map((platform: Platform) =&gt; (
          &lt;MenuItem
            onClick={() =&gt; setPlatformId(platform.id)}
            key={platform.id}
          &gt;
            {platform.name}
          &lt;/MenuItem&gt;
        ))}
      &lt;/MenuList&gt;
    &lt;/Menu&gt;
  );
};

export default PlatformSelector;

Previously, I had a problem with the case of the usePlatforms hook, as it was initially named with a capital letter. I changed the title and adjusted the TypeScript configuration like this:

&quot;forceConsistentCasingInFileNames&quot;: false

I'm receiving the error mentioned above. How can I resolve this issue?

答案1

得分: 1

你可能会在Vercel上遇到导入错误,其中文件系统区分大小写。

在你的代码中,例如:

// usePlatform hook
import Platform from "../Entities/Platform";
// usePlatforms hook
import Platform from '../entities/Platform';

你需要确保你的文件名和导入语句在大小写上完全匹配。

英文:

You might encounter an import error on Vercel, where the filesystem is case-sensitive.

In your code, for example:

// usePlatform hook
import Platform from &quot;../Entities/Platform&quot;;
// usePlatforms hook
import Platform from &#39;../entities/Platform&#39;;

You need to make sure that your filenames and import statements match exactly in letter-casing.

答案2

得分: 0

我不要回答我要翻译的问题。以下是要翻译的内容:

我不得不使用git config core.ignorecase false来使git看到大小写的更改。在我使用它并更改了一些文件和导入的大小写之后,一切正常。

英文:

I had to use git config core.ignorecase false to make git see changes in casing. After I used it and change casing in some files and imports , everything works.

huangapple
  • 本文由 发表于 2023年7月10日 17:28:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定