diff --git a/examples/zustand/vite.config.ts b/examples/zustand/vite.config.ts index d28b483f..b232f22f 100644 --- a/examples/zustand/vite.config.ts +++ b/examples/zustand/vite.config.ts @@ -38,8 +38,8 @@ function vikeReactZustandPlugin(): Plugin { return } - // Playground: https://regex101.com/r/a1FcfP/1 - const matches = code.matchAll(/(?<=[\s:=])create\s*(?:\(\W*?[\w\s.]*?[^,\w]*?\))?\s*?\(/g) + // Playground: https://regex101.com/r/oDNRzp/1 + const matches = code.matchAll(/(?<=[\s:=,;])create\s*?\(/g) let idx = 0 for (const match of matches) { if (!match.index || !match.input) { diff --git a/packages/vike-react-zustand/src/index.tsx b/packages/vike-react-zustand/src/index.tsx index ba65db97..4497eaf4 100644 --- a/packages/vike-react-zustand/src/index.tsx +++ b/packages/vike-react-zustand/src/index.tsx @@ -11,11 +11,6 @@ import { assert } from './utils.js' * * The `devtools` middleware is included by default. * - * To create multiple stores, set a unique key: - * ```ts - * const useStore = create('store1')(...) - * const useStore2 = create('store2')(...) - * ``` * Usage examples: https://docs.pmnd.rs/zustand/guides/typescript#basic-usage * */ @@ -28,46 +23,38 @@ const createWrapped = ((...args: any[]) => { // create((set,get) => ...) // ^^^^^^^^^^^^^^^ (typeof args[0] === 'function' && args[0]) || + // create('keyFromTransform', 'keyFromUser')((set,get) => ...) + // ^^^^^^^^^^^^^ // create('keyFromUser')((set,get) => ...) // ^^^^^^^^^^^^^ // create()((set,get) => ...) // ^ undefined - // create('keyFromUser')('keyFromTransform', (set,get) => ...) - // ^^^^^^^^^^^^^ - // create('keyFromUser')((set,get) => ...) - // ^^^^^^^^^^^^^ - const keyFromUser = typeof args[0] === 'string' && args[0] + const key = + // create('keyFromTransform', 'keyFromUser')((set,get) => ...) + // ^^^^^^^^^^^^^ + (typeof args[1] === 'string' && args[1]) || + // create('keyFromTransform')((set,get) => ...) + // ^^^^^^^^^^^^^ + // create('keyFromUser')((set,get) => ...) + // ^^^^^^^^^^^^^ + (typeof args[0] === 'string' && args[0]) || + // The transform didn't run for this call(skipped in node_modules) + // create()((set,get) => ...) + // create((set,get) => ...) + 'default' - const create_ = (...args: any[]) => { - const initializerFn_ = - // create('keyFromTransform', (set,get) => ...) - // ^^^^^^^^^^^^^^^ - (typeof args[1] === 'function' && args[1]) || - // The transform didn't run for this call(skipped in node_modules) - // create((set,get) => ...) - // ^^^^^^^^^^^^^^^ - (typeof args[0] === 'function' && args[0]) + assert(key) + const create_ = (initializerFn_: any) => { assert(initializerFn_) - - const key = - keyFromUser || - // create('keyFromTransform', (set,get) => ...) - // ^^^^^^^^^^^^^^^^^ - (typeof args[0] === 'string' && args[0]) || - // The transform didn't run for this call(skipped in node_modules) - // create((set,get) => ...) - 'default' - - assert(key) - initializers_set(key, initializerFn_) const useStore = getUseStore(key) useStore.__key__ = key return useStore } + if (initializerFn) { // create((set,get) => ...) return create_(initializerFn)