Skip to content

Commit

Permalink
[v5] breaking: drop default exports (#2238)
Browse files Browse the repository at this point in the history
* fix: drop default exports for v5

* chore: remove default from cjs build

* refactor: export shallow in v5

* fix: remove `addModuleExport` option for cjs.
  • Loading branch information
charkour committed Dec 10, 2023
1 parent 105bc57 commit 8030aee
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 70 deletions.
24 changes: 2 additions & 22 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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'),
Expand Down
4 changes: 1 addition & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type WithoutCallSignature<T> = { [K in keyof T]: T[K] }
/**
* @deprecated Use `createStore` and `useStore` for context usage
*/
function createContext<S extends StoreApi<unknown>>() {
export function createContext<S extends StoreApi<unknown>>() {
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.",
Expand Down Expand Up @@ -98,5 +98,3 @@ function createContext<S extends StoreApi<unknown>>() {
useStoreApi,
}
}

export default createContext
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './vanilla.ts'
export * from './react.ts'
export { default } from './react.ts'
12 changes: 0 additions & 12 deletions src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,3 @@ const createImpl = <T>(createState: StateCreator<T, [], []>) => {

export const create = (<T>(createState: StateCreator<T, [], []> | 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
21 changes: 2 additions & 19 deletions src/shallow.ts
Original file line number Diff line number Diff line change
@@ -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'
12 changes: 0 additions & 12 deletions src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

// ---------------------------------------------------------

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/context.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8030aee

Please sign in to comment.