dayhaysoos / use-shopping-cart

Shopping cart state and logic for Stripe
MIT License
913 stars 118 forks source link

feat: update package.json exports #249

Closed ctjlewis closed 2 years ago

ctjlewis commented 2 years ago

This allows us to import or require from any arbitrary entry point without breaking the overrides above it.

Currently, for instance, it is not possible to do import { validateCartItems } from 'use-shopping-cart/utilities/serverless'. This is fixed by this PR.

netlify[bot] commented 2 years ago

👷 Deploy request for useshoppingcart pending review. Visit the deploys page to approve it

🔨 Explore the source changes: 49aa1d1de192cce1522ef73cb091fb0230cc4248

ctjlewis commented 2 years ago

Originally, I was just going to map any fallback export ./* to ./*.js, but the majority of the code is actually emitted TS output in dist/, so I updated such that:

Also, what's sneaky is that TypeScript will act like it's a valid import, but Node will (correctly) throw at runtime:

Failed to compile
./src/pages/api/checkout_sessions/cart.ts:12:0
Module not found: Package path ./utilities/serverless.js is not exported from package /home/christian/PersonalProjects/tezeta/node_modules/use-shopping-cart (see exports field in /home/christian/PersonalProjects/tezeta/node_modules/use-shopping-cart/package.json)
  10 |  */
  11 | import inventory from "../../../../data/products.json";
> 12 | import { validateCartItems } from "use-shopping-cart/utilities/serverless.js";

If there were no exports field, it would resolve okay, because ESM resolution would just let you specify the full entry point. But when you add exports, ESM resolution defers 100% to it, and it's impossible to import entry points which aren't specified there.

dayhaysoos commented 2 years ago

Thank you @ctjlewis !!!

bryandandan commented 1 year ago

import { validateCartItems } from 'use-shopping-cart/utilities/serverless' still doesn't work on my end Version: 3.1.2

dayhaysoos commented 1 year ago

Hey @bryandandan, are you using TypeScript?

With TypeScript you have to import like this:

import { validateCartItems } from 'use-shopping-cart/utilities'

Sorry for the confusion!