Open tats-u opened 1 year ago
Indeed, it seems we never expect someone to actually use import statements with .png
files 😅 Our docs do recommend src={require("...").default}
because it's several lines shorter.
Unfortunately ambient declarations cannot override each other's default export. Do you know how this should work? Should we have declare module "*.png"
under a separate named export and let users choose between the declarations from module-name-aliases
or plugin-ideal-image
?
I think we can refer to Next.js.
https://nextjs.org/docs/basic-features/typescript#existing-projects
/image-types/global.d.ts
.next-env.d.ts
in the project's root. It says it should be ignored. However I have not understood how it's generated in Next.js CLI yet.As for applying this to Docusaurus, I don't think .d.ts
should be ignored. However i think it should be generated or modified if needed when yarn start
.
Note: the ideal plugin image returns string | object
because in dev it can be disabled while it's always enabled in prod.
Due to the modular nature of Docusaurus, if we were to generate something similar to next-env.d.ts
that would probably require a new plugin lifecycle so that the generated types are different if the image plugin is enabled/disabled.
I'd prefer to not rush into implementing this. Maybe let's see if we can find other plugins that would benefit from this. Adding local typedefs yourself is a good temporary workaround.
Have you read the Contributing Guidelines on issues?
Prerequisites
npm run clear
oryarn clear
command.rm -rf node_modules yarn.lock package-lock.json
and re-installing packages.Description
In current Docusaurus version (2.x), no
declare module "*.png"
,declare module "*.jpg"
, and so on are not defined and we get type erros when we try to import raster images unlike Next.js. We have to avoid this kind of errors by adding these ambient declarations tosrc/index.d.ts
.To make matters worse, the
plugin-ideal-image
changes the type of imported supported images fromstring
to{src: {src: string, width: ...}, preSrc: string}
. (related: #8684) We will have to select proper ambient declarations. Next.js, which has a similar feature, resolves this problem bynext-env.d.ts
like:Reproducible demo
https://codesandbox.io/s/docusaurus-bug-report1-kptbhq
Steps to reproduce
import picture from "../../blog/2021-08-26-welcome/docusaurus-plushie-banner.jpeg";
and<img src={picture} alt="" />
tosrc/pages/index.tsx
Expected behavior
No type errors
Actual behavior
Your environment
Self-service