【问题标题】:How to add GIF loader in Webpack config with Typescript/React/Nextjs app如何使用 Typescript/React/Nextjs 应用在 Webpack 配置中添加 GIF 加载器
【发布时间】:2021-12-04 16:52:20
【问题描述】:

在我的 Typescript React 应用程序中添加 .gif 文件后出现错误,如下所示。

./src/gif/moving.gif 1:6
Module parse failed: Unexpected token (1:6)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

下载“文件加载器”后,我在 webpack 设置中添加了一些配置

{
      test: /\.scss$/,
      use: ['style-loader', 'css-loader', 'sass-loader'],
    }, 
// new setting for gif loader
    {
      test: /\.(gif|svg|jpg|png)$/,
      loader: "file-loader",
    },

我仍然收到配置错误的任何原因?

【问题讨论】:

    标签: reactjs typescript web webpack next.js


    【解决方案1】:

    https://www.npmjs.com/package/next-images 可以解决您的问题。否则你需要这样做:

    import Image from 'next/image'
    
    export default function Page() {
      return (
        <div>
          <Image
            src="/src/gif/moving.gif"
            alt="Moving Image"
            width={500}
            height={500}
          />
        </div>
      )
    }
    

    注意:你可以先做const movingGif = "/src/gif/moving.gif"然后在Image组件里src={movingGif}

    【讨论】:

    • 导入下一个图像不起作用 - 导入 next.image 的方式也不起作用,因为错误说 gif 是类型 stringType '{ src: string; }' is not assignable to type 'IntrinsicAttributes &amp; ImageProps'. 我的 GIF 的任何原因识别为字符串?
    • src= "{gif}" 应该可以工作
    猜你喜欢
    • 1970-01-01
    • 2022-08-03
    • 2021-12-08
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2022-01-24
    相关资源
    最近更新 更多