From 8030aeeb91f22369041d12d156e79b6318ea5d06 Mon Sep 17 00:00:00 2001 From: Charles Kornoelje <33156025+charkour@users.noreply.github.com> Date: Sun, 10 Dec 2023 09:09:46 -0500 Subject: [PATCH] [v5] breaking: drop default exports (#2238) * fix: drop default exports for v5 * chore: remove default from cjs build * refactor: export shallow in v5 * fix: remove `addModuleExport` option for cjs. --- rollup.config.js | 24 ++---------------------- src/context.ts | 4 +--- src/index.ts | 1 - src/react.ts | 12 ------------ src/shallow.ts | 21 ++------------------- src/vanilla.ts | 12 ------------ tests/context.test.tsx | 2 +- 7 files changed, 6 insertions(+), 70 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 2ad17f5e35..a0c6e3ca29 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -78,22 +78,13 @@ function createESMConfig(input, output) { } } -function createCommonJSConfig(input, output, options) { +function createCommonJSConfig(input, output) { return { input, output: { file: `${output}.js`, format: 'cjs', esModule: false, - outro: options.addModuleExport - ? [ - `module.exports = ${options.addModuleExport.default};`, - ...Object.entries(options.addModuleExport) - .filter(([key]) => key !== 'default') - .map(([key, value]) => `module.exports.${key} = ${value};`), - `exports.default = module.exports;`, - ].join('\n') - : '', }, external, plugins: [ @@ -176,18 +167,7 @@ module.exports = function (args) { } return [ ...(c === 'index' ? [createDeclarationConfig(`src/${c}.ts`, 'dist')] : []), - createCommonJSConfig(`src/${c}.ts`, `dist/${c}`, { - addModuleExport: { - index: { - default: 'react', - create: 'create', - useStore: 'useStore', - createStore: 'vanilla.createStore', - }, - vanilla: { default: 'vanilla', createStore: 'createStore' }, - shallow: { default: 'shallow', shallow: 'shallow$1' }, - }[c], - }), + createCommonJSConfig(`src/${c}.ts`, `dist/${c}`), createESMConfig(`src/${c}.ts`, `dist/esm/${c}.js`), createESMConfig(`src/${c}.ts`, `dist/esm/${c}.mjs`), createUMDConfig(`src/${c}.ts`, `dist/umd/${c}`, 'development'), diff --git a/src/context.ts b/src/context.ts index 01db464c33..c80c4da462 100644 --- a/src/context.ts +++ b/src/context.ts @@ -37,7 +37,7 @@ type WithoutCallSignature = { [K in keyof T]: T[K] } /** * @deprecated Use `createStore` and `useStore` for context usage */ -function createContext>() { +export function createContext>() { if (import.meta.env?.MODE !== 'production') { console.warn( "[DEPRECATED] `context` will be removed in a future version. Instead use `import { createStore, useStore } from 'zustand'`. See: https://github.com/pmndrs/zustand/discussions/1180.", @@ -98,5 +98,3 @@ function createContext>() { useStoreApi, } } - -export default createContext diff --git a/src/index.ts b/src/index.ts index bc6544c82f..27b7a4f91f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,2 @@ export * from './vanilla.ts' export * from './react.ts' -export { default } from './react.ts' diff --git a/src/react.ts b/src/react.ts index 48a35e3f89..e3dae514d3 100644 --- a/src/react.ts +++ b/src/react.ts @@ -120,15 +120,3 @@ const createImpl = (createState: StateCreator) => { export const create = ((createState: StateCreator | undefined) => createState ? createImpl(createState) : createImpl) as Create - -/** - * @deprecated Use `import { create } from 'zustand'` - */ -export default ((createState: any) => { - if (import.meta.env?.MODE !== 'production') { - console.warn( - "[DEPRECATED] Default export is deprecated. Instead use `import { create } from 'zustand'`.", - ) - } - return create(createState) -}) as Create diff --git a/src/shallow.ts b/src/shallow.ts index 09e3ba5b3b..2fb316beae 100644 --- a/src/shallow.ts +++ b/src/shallow.ts @@ -1,19 +1,2 @@ -import { shallow } from './vanilla/shallow.ts' - -// We will export this in v5 and remove default export -// export { shallow } from './vanilla/shallow.ts' -// export { useShallow } from './react/shallow.ts' - -/** - * @deprecated Use `import { shallow } from 'zustand/shallow'` - */ -export default ((objA, objB) => { - if (import.meta.env?.MODE !== 'production') { - console.warn( - "[DEPRECATED] Default export is deprecated. Instead use `import { shallow } from 'zustand/shallow'`.", - ) - } - return shallow(objA, objB) -}) as typeof shallow - -export { shallow } +export { shallow } from './vanilla/shallow.ts' +export { useShallow } from './react/shallow.ts' diff --git a/src/vanilla.ts b/src/vanilla.ts index d5d9c092f0..6c82a682d0 100644 --- a/src/vanilla.ts +++ b/src/vanilla.ts @@ -105,18 +105,6 @@ const createStoreImpl: CreateStoreImpl = (createState) => { export const createStore = ((createState) => createState ? createStoreImpl(createState) : createStoreImpl) as CreateStore -/** - * @deprecated Use `import { createStore } from 'zustand/vanilla'` - */ -export default ((createState) => { - if (import.meta.env?.MODE !== 'production') { - console.warn( - "[DEPRECATED] Default export is deprecated. Instead use import { createStore } from 'zustand/vanilla'.", - ) - } - return createStore(createState) -}) as CreateStore - // --------------------------------------------------------- /** diff --git a/tests/context.test.tsx b/tests/context.test.tsx index 5944f11773..6ccabd2606 100644 --- a/tests/context.test.tsx +++ b/tests/context.test.tsx @@ -10,7 +10,7 @@ import { render } from '@testing-library/react' import { afterEach, it, vi } from 'vitest' import { create } from 'zustand' import type { StoreApi } from 'zustand' -import createContext from 'zustand/context' +import { createContext } from 'zustand/context' import { subscribeWithSelector } from 'zustand/middleware' const consoleError = console.error