-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
156 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,8 @@ | ||
import react from '@vitejs/plugin-react' | ||
import { init, parse } from 'es-module-lexer' | ||
import vike from 'vike/plugin' | ||
import type { Plugin, UserConfig } from 'vite' | ||
import type { UserConfig } from 'vite' | ||
import { vikeReactZustand } from 'vike-react-zustand/plugin' | ||
|
||
export default { | ||
plugins: [react(), vike(), vikeReactZustandPlugin()] | ||
plugins: [react(), vike(), vikeReactZustand()] | ||
} satisfies UserConfig | ||
|
||
function vikeReactZustandPlugin(): Plugin { | ||
const idToStoreKeys: { [id: string]: Set<string> } = {} | ||
return { | ||
name: 'vikeReactZustand', | ||
enforce: 'post', | ||
transform(code, id) { | ||
if (id.includes('node_modules')) { | ||
return | ||
} | ||
const res = parse(code) | ||
const match = res[0].find((line) => line.n === 'vike-react-zustand') | ||
if (!match) { | ||
return | ||
} | ||
const importLine = code.slice(match.ss, match.se) | ||
const imports = importLine | ||
.substring(importLine.indexOf('{') + 1, importLine.indexOf('}')) | ||
.split(',') | ||
.map((s) => s.trim()) | ||
.filter((s) => { | ||
const split = s.split(' as ') | ||
return ( | ||
split.length === 1 || | ||
// create as create | ||
split[0] === split[1] | ||
) | ||
}) | ||
if (!imports.includes('create')) { | ||
return | ||
} | ||
|
||
// 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) { | ||
continue | ||
} | ||
const key = simpleHash(`${id}:${idx}`) | ||
idToStoreKeys[id] ??= new Set([key]) | ||
idToStoreKeys[id].add(key) | ||
code = | ||
match.input.substring(0, match.index) + | ||
`${match[0]}'${key}',` + | ||
match.input.substring(match.index + match[0].length) | ||
idx++ | ||
} | ||
|
||
return code | ||
}, | ||
async buildStart() { | ||
await init | ||
}, | ||
async handleHotUpdate(ctx) { | ||
const modules = ctx.modules.filter((m) => m.id && m.id in idToStoreKeys) | ||
if (!modules.length) return | ||
|
||
for (const module of modules) { | ||
if (!module.id) { | ||
continue | ||
} | ||
const storeKeysInFile = idToStoreKeys[module.id] | ||
for (const key of storeKeysInFile) { | ||
//@ts-ignore | ||
if (globalThis.__vite_plugin_ssr?.['VikeReactZustandContext.ts']) { | ||
//@ts-ignore | ||
delete globalThis.__vite_plugin_ssr['VikeReactZustandContext.ts'].initializers[key] | ||
} | ||
} | ||
delete idToStoreKeys[module.id] | ||
} | ||
|
||
ctx.server.ws.send({ type: 'full-reload' }) | ||
return [] | ||
} | ||
} | ||
} | ||
|
||
function simpleHash(str: string) { | ||
let hash = 0 | ||
for (let i = 0; i < str.length; i++) { | ||
hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0 | ||
} | ||
return (hash >>> 0).toString(36) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
export { vikeReactZustand } | ||
|
||
import type { Plugin } from 'vite' | ||
import { initializers_remove } from './renderer/context.js' | ||
import { assert } from './utils.js' | ||
import { init, parse } from 'es-module-lexer' | ||
|
||
function vikeReactZustand(): Plugin { | ||
const idToStoreKeys: { [id: string]: Set<string> } = {} | ||
return { | ||
name: 'vikeReactZustand', | ||
enforce: 'post', | ||
async transform(code, id) { | ||
if (id.includes('node_modules')) { | ||
return | ||
} | ||
const res = parse(code) | ||
const match = res[0].find((line) => line.n === 'vike-react-zustand') | ||
if (!match) { | ||
return | ||
} | ||
const importLine = code.slice(match.ss, match.se) | ||
const imports = importLine | ||
.substring(importLine.indexOf('{') + 1, importLine.indexOf('}')) | ||
.split(',') | ||
.map((s) => s.trim()) | ||
.filter((s) => { | ||
const split = s.split(' as ') | ||
return ( | ||
split.length === 1 || | ||
// create as create | ||
split[0] === split[1] | ||
) | ||
}) | ||
if (!imports.includes('create')) { | ||
return | ||
} | ||
|
||
// 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) { | ||
continue | ||
} | ||
const key = simpleHash(`${id}:${idx}`) | ||
idToStoreKeys[id] ??= new Set([key]) | ||
idToStoreKeys[id]!.add(key) | ||
code = | ||
match.input.substring(0, match.index) + | ||
`${match[0]}'${key}',` + | ||
match.input.substring(match.index + match[0].length) | ||
idx++ | ||
} | ||
|
||
return code | ||
}, | ||
async buildStart() { | ||
await init | ||
}, | ||
handleHotUpdate(ctx) { | ||
const modules = ctx.modules.filter((m) => m.id && m.id in idToStoreKeys) | ||
if (!modules.length) return | ||
|
||
for (const module of modules.filter((m) => m.id)) { | ||
assert(module.id) | ||
const storeKeysInFile = idToStoreKeys[module.id] | ||
assert(storeKeysInFile) | ||
for (const key of storeKeysInFile) { | ||
initializers_remove(key) | ||
} | ||
delete idToStoreKeys[module.id] | ||
} | ||
|
||
ctx.server.ws.send({ type: 'full-reload' }) | ||
return [] | ||
} | ||
} | ||
} | ||
|
||
function simpleHash(str: string) { | ||
let hash = 0 | ||
for (let i = 0; i < str.length; i++) { | ||
hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0 | ||
} | ||
return (hash >>> 0).toString(36) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.