fix: fix static data file support in vite 3

pull/1189/head
Evan You 2 years ago
parent 43469a2601
commit 19ec22cb40

@ -15,6 +15,15 @@ export default defineConfig({
link: '/frontmatter/multiple-levels-outline'
}
]
},
{
text: 'Static Data',
items: [
{
text: 'Test Page',
link: '/static-data/data'
}
]
}
]
}

@ -1,5 +1,6 @@
{
"private": true,
"type": "module",
"name": "vitepress-example-configured",
"scripts": {
"dev": "vitepress dev",

@ -0,0 +1,5 @@
<script setup>
import { data } from './static.data.js'
</script>
{{ data }}

@ -0,0 +1,20 @@
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const dirname = path.dirname(fileURLToPath(import.meta.url))
export default {
watch: ['./data/*'],
async load() {
const foo = fs.readFileSync(
path.resolve(dirname, './data/foo.json'),
'utf-8'
)
const bar = fs.readFileSync(
path.resolve(dirname, './data/bar.json'),
'utf-8'
)
return [JSON.parse(foo), JSON.parse(bar)]
}
}

@ -1,7 +1,7 @@
// TODO figure out why it causes full page reload
import { Plugin, ViteDevServer, loadConfigFromFile, normalizePath } from 'vite'
import { dirname, relative } from 'path'
import { dirname, resolve } from 'path'
import { isMatch } from 'micromatch'
const loaderMatch = /\.data\.(j|t)s$/
@ -14,7 +14,6 @@ interface LoaderModule {
}
interface CachedLoaderModule {
base: string
pattern: string[] | undefined
loader: () => any
}
@ -72,8 +71,11 @@ export const staticDataPlugin: Plugin = {
: loaderModule.watch
if (pattern) {
pattern = pattern.map((p) => {
return p.startsWith('./') ? p.slice(2) : p
return p.startsWith('.')
? normalizePath(resolve(base, p))
: normalizePath(p)
})
console.log(pattern)
}
loader = loaderModule.load
}
@ -83,7 +85,7 @@ export const staticDataPlugin: Plugin = {
// record loader module for HMR
if (server) {
idToLoaderModulesMap[id] = { base, pattern, loader }
idToLoaderModulesMap[id] = { pattern, loader }
}
const result = `export const data = JSON.parse(${JSON.stringify(
@ -98,27 +100,24 @@ export const staticDataPlugin: Plugin = {
transform(_code, id) {
if (server && loaderMatch.test(id)) {
// register this module as a glob importer
const { base, pattern } = idToLoaderModulesMap[id]!
;(server as any)._globImporters[id] = {
module: server.moduleGraph.getModuleById(id),
importGlobs: pattern?.map((pattern) => ({ base, pattern }))
}
const { pattern } = idToLoaderModulesMap[id]!
;(server as any)._importGlobMap.set(id, [pattern])
}
return null
},
handleHotUpdate(ctx) {
for (const id in idToLoaderModulesMap) {
const { base, pattern } = idToLoaderModulesMap[id]!
const { pattern } = idToLoaderModulesMap[id]!
const isLoaderFile = normalizePath(ctx.file) === id
if (isLoaderFile) {
// invalidate loader file
delete idToLoaderModulesMap[id]
}
if (
isLoaderFile ||
(pattern && isMatch(relative(base, ctx.file), pattern))
) {
if (pattern) {
console.log(pattern, isMatch(ctx.file, pattern))
}
if (isLoaderFile || (pattern && isMatch(ctx.file, pattern))) {
ctx.modules.push(server.moduleGraph.getModuleById(id)!)
}
}

Loading…
Cancel
Save