fix(medusa-react): Fix production.min.js causing invalid hook usage error in CJS environments (#3144)

This commit is contained in:
Kasper Fabricius Kristensen
2023-02-01 14:07:02 +01:00
committed by GitHub
parent b242e22326
commit 4fbf6b7ad3
8 changed files with 1557 additions and 1322 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"medusa-react": patch
---
fix(medusa-react): Fix production.min.js causing invalid hook usage error in CJS environments
+5
View File
@@ -11,4 +11,9 @@ module.exports = {
}, },
setupFilesAfterEnv: ["./jest.setup.js"], setupFilesAfterEnv: ["./jest.setup.js"],
testEnvironment: "jsdom", testEnvironment: "jsdom",
moduleNameMapper: {
// Force module uuid to resolve with the CJS entry point, because Jest does not support package.json.exports. See https://github.com/uuidjs/uuid/issues/451
// Issue is fixed in newer versions of UUID, but since it's a transitive dependency, we need to force it here.
"^uuid$": "uuid",
},
} }
+24 -42
View File
@@ -2,52 +2,36 @@
"version": "4.0.3", "version": "4.0.3",
"license": "MIT", "license": "MIT",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "module": "dist/index.mjs",
"files": [ "types": "dist/index.d.ts",
"dist", "exports": {
"src" ".": {
], "import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./package.json": "./package.json"
},
"engines": { "engines": {
"node": ">=10" "node": ">=10"
}, },
"scripts": { "scripts": {
"start": "dts watch", "build": "tsup src/index.ts",
"build": "dts build", "prepare": "cross-env NODE_ENV=production yarn run build",
"prepare": "dts build",
"test": "dts test --passWithNoTests",
"lint": "dts lint",
"size": "size-limit",
"analyze": "size-limit --why",
"storybook": "start-storybook -p 6006 -s public", "storybook": "start-storybook -p 6006 -s public",
"build-storybook": "build-storybook" "build-storybook": "build-storybook",
"test": "jest --passWithNoTests"
}, },
"peerDependencies": { "peerDependencies": {
"@medusajs/medusa": "^1.7.4", "@medusajs/medusa": "^1.7.4",
"@tanstack/react-query": ">=4.22.0", "@tanstack/react-query": ">=4.22.0",
"react": ">=16" "react": ">=16",
}, "react-dom": ">=16"
"husky": {
"hooks": {
"pre-commit": "tsdx lint"
}
}, },
"name": "medusa-react", "name": "medusa-react",
"author": "Zakaria S. El Asri", "author": "Zakaria S. El Asri",
"module": "dist/medusa-react.esm.js",
"size-limit": [
{
"path": "dist/medusa-react.cjs.production.min.js",
"limit": "10 KB"
},
{
"path": "dist/medusa-react.esm.js",
"limit": "10 KB"
}
],
"devDependencies": { "devDependencies": {
"@babel/core": "^7.16.0", "@babel/core": "^7.16.0",
"@medusajs/medusa": "^1.7.4", "@medusajs/medusa": "^1.7.4",
"@size-limit/preset-small-lib": "^6.0.4",
"@storybook/addon-contexts": "^5.3.21", "@storybook/addon-contexts": "^5.3.21",
"@storybook/addon-essentials": "^6.3.12", "@storybook/addon-essentials": "^6.3.12",
"@storybook/addon-info": "^5.3.21", "@storybook/addon-info": "^5.3.21",
@@ -55,30 +39,28 @@
"@storybook/addons": "^6.3.12", "@storybook/addons": "^6.3.12",
"@storybook/react": "^6.3.12", "@storybook/react": "^6.3.12",
"@tanstack/react-query": "^4.22.0", "@tanstack/react-query": "^4.22.0",
"@testing-library/react": "^12.1.2", "@testing-library/react": "^13.4.0",
"@testing-library/react-hooks": "^7.0.2", "@testing-library/react-hooks": "^8.0.1",
"@types/jest": "^27.0.3", "@types/jest": "^27.0.3",
"@types/lodash": "^4.14.177",
"@types/react": "^17.0.33", "@types/react": "^17.0.33",
"@types/react-dom": "^17.0.10", "@types/react-dom": "^17.0.10",
"axios": "^0.24.0", "axios": "^0.24.0",
"babel-loader": "^8.2.3", "babel-loader": "^8.2.3",
"dts-cli": "^1.5.1", "cross-env": "^7.0.3",
"husky": "^7.0.4", "jest": "^29.4.1",
"jest-environment-jsdom": "^29.4.1",
"msw": "^0.35.0", "msw": "^0.35.0",
"msw-storybook-addon": "^1.5.0", "msw-storybook-addon": "^1.5.0",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2", "react-dom": "^17.0.2",
"react-is": "^17.0.2", "react-is": "^17.0.2",
"react-json-view": "^1.21.3", "react-json-view": "^1.21.3",
"size-limit": "^6.0.4", "ts-jest": "^29.0.5",
"ts-jest": "^27.1.4", "tslib": "^2.3.1",
"tslib": "^2.3.1" "tsup": "^6.5.0"
}, },
"dependencies": { "dependencies": {
"@medusajs/medusa-js": "^1.3.7", "@medusajs/medusa-js": "^1.3.7"
"lodash": "^4.17.21",
"lodash-es": "^4.17.21"
}, },
"msw": { "msw": {
"workerDirectory": "public" "workerDirectory": "public"
@@ -1,8 +1,8 @@
import React, { useContext, useEffect } from "react" import React, { useContext, useEffect } from "react"
import { useLocalStorage } from "../hooks/utils"
import { RegionInfo, ProductVariant } from "../types"
import { getVariantPrice } from "../helpers" import { getVariantPrice } from "../helpers"
import { isArray, isEmpty, isObject } from "lodash" import { useLocalStorage } from "../hooks/utils"
import { ProductVariant, RegionInfo } from "../types"
import { isArray, isEmpty, isObject } from "../utils"
interface Item { interface Item {
variant: ProductVariant variant: ProductVariant
@@ -72,7 +72,7 @@ const reducer = (state: SessionCartState, action: Action) => {
} }
case ACTION_TYPES.ADD_ITEM: { case ACTION_TYPES.ADD_ITEM: {
const duplicateVariantIndex = state.items.findIndex( const duplicateVariantIndex = state.items.findIndex(
item => item.variant.id === action.payload?.variant?.id (item) => item.variant.id === action.payload?.variant?.id
) )
if (duplicateVariantIndex !== -1) { if (duplicateVariantIndex !== -1) {
state.items.splice(duplicateVariantIndex, 1) state.items.splice(duplicateVariantIndex, 1)
@@ -81,7 +81,7 @@ const reducer = (state: SessionCartState, action: Action) => {
return generateCartState(state, items) return generateCartState(state, items)
} }
case ACTION_TYPES.UPDATE_ITEM: { case ACTION_TYPES.UPDATE_ITEM: {
const items = state.items.map(item => const items = state.items.map((item) =>
item.variant.id === action.payload.id item.variant.id === action.payload.id
? { ...item, ...action.payload.item } ? { ...item, ...action.payload.item }
: item : item
@@ -91,7 +91,7 @@ const reducer = (state: SessionCartState, action: Action) => {
} }
case ACTION_TYPES.REMOVE_ITEM: { case ACTION_TYPES.REMOVE_ITEM: {
const items = state.items.filter( const items = state.items.filter(
item => item.variant.id !== action.payload.id (item) => item.variant.id !== action.payload.id
) )
return generateCartState(state, items) return generateCartState(state, items)
} }
@@ -122,7 +122,7 @@ export const generateCartState = (state: SessionCartState, items: Item[]) => {
} }
const generateItems = (region: RegionInfo, items: Item[]) => { const generateItems = (region: RegionInfo, items: Item[]) => {
return items.map(item => ({ return items.map((item) => ({
...item, ...item,
total: getVariantPrice(item.variant, region), total: getVariantPrice(item.variant, region),
})) }))
@@ -171,7 +171,7 @@ export const SessionCartProvider = ({
} }
const getItem = (id: string) => { const getItem = (id: string) => {
return state.items.find(item => item.variant.id === id) return state.items.find((item) => item.variant.id === id)
} }
const setItems = (items: Item[]) => { const setItems = (items: Item[]) => {
+4 -3
View File
@@ -1,5 +1,5 @@
import { isEmpty } from "lodash" import { ProductVariantInfo, RegionInfo } from "../types"
import { RegionInfo, ProductVariantInfo } from "../types" import { isEmpty } from "../utils"
type FormatVariantPriceParams = { type FormatVariantPriceParams = {
variant: ProductVariantInfo variant: ProductVariantInfo
@@ -65,7 +65,8 @@ export const getVariantPrice = (
region: RegionInfo region: RegionInfo
) => { ) => {
let price = variant?.prices?.find( let price = variant?.prices?.find(
p => p.currency_code.toLowerCase() === region?.currency_code?.toLowerCase() (p) =>
p.currency_code.toLowerCase() === region?.currency_code?.toLowerCase()
) )
return price?.amount || 0 return price?.amount || 0
+11
View File
@@ -0,0 +1,11 @@
export const isObject = (input: any) => input instanceof Object
export const isArray = (input: any) => Array.isArray(input)
export const isEmpty = (input: any) => {
return (
input === null ||
input === undefined ||
(isObject(input) && Object.keys(input).length === 0) ||
(isArray(input) && (input as any[]).length === 0) ||
(typeof input === "string" && input.trim().length === 0)
)
}
+16
View File
@@ -0,0 +1,16 @@
import type { Options } from "tsup"
const config: Options = {
entry: ["src/**/*.ts"],
dts: true,
clean: true,
minify: true,
bundle: true,
sourcemap: true,
format: ["cjs", "esm"],
target: "es2020",
skipNodeModulesBundle: true,
tsconfig: "./tsconfig.json",
}
export default config
+1484 -1269
View File
File diff suppressed because it is too large Load Diff