Shopify / hydrogen-v1

React-based framework for building dynamic, Shopify-powered custom storefronts.
https://shopify.github.io/hydrogen-v1/
MIT License
3.75k stars 325 forks source link

Support older moduleResolution for types #1776

Open frandiox opened 2 years ago

frandiox commented 2 years ago

We recently started using package.json#exports instead of facade files in Hydrogen, which requires TS to use compiler.moduleResolution: node16 to follow types from package.json.

However, it seems that node16 value might cause issues with older dependencies that don't use package.json#exports -- we got a report about this issue on another channel.

I think we could try copying our generated d.ts files after build to the root of the Hydrogen dist directory so that fallbacks to moduleResolution: node can at least find the types.

For example, we'd copy @shopify/hydrogen/dist/esnext/config.d.ts to @shopify/hydrogen/config.d.ts; @shopify/hydrogen/dist/esnext/framework/plugin.d.ts to @shopify/hydrogen/plugin.d.ts; etc. That way, when importing from @shopify/hydrogen/config, even an older module resolution should find the config.d.ts.

Otherwise, we can reintroduce facade files.

frehner commented 2 years ago

Alternatively, you can copy the pattern that hydrogen-ui uses, and use the typesVersions in package.json to map the types to their locations.

https://github.com/Shopify/hydrogen/blob/d3363c7889d2b5180578ffac274e52de4d31a831/packages/hydrogen-ui/package.json#L23-L38

Though I think hydrogen-ui should actually take out the <=4.6 qualifier and instead use '*', so that way even if someone is using node on TS 4.7 they still get the types.

frandiox commented 2 years ago

TIL typesVersions is a thing. That's a better way to solve this, thanks!