Skip to content

Commit 19ec22c

Browse files
committed
fix: fix static data file support in vite 3
1 parent 43469a2 commit 19ec22c

File tree

7 files changed

+50
-14
lines changed

7 files changed

+50
-14
lines changed

examples/configured/.vitepress/config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ export default defineConfig({
1515
link: '/frontmatter/multiple-levels-outline'
1616
}
1717
]
18+
},
19+
{
20+
text: 'Static Data',
21+
items: [
22+
{
23+
text: 'Test Page',
24+
link: '/static-data/data'
25+
}
26+
]
1827
}
1928
]
2029
}

examples/configured/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"private": true,
3+
"type": "module",
34
"name": "vitepress-example-configured",
45
"scripts": {
56
"dev": "vitepress dev",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script setup>
2+
import { data } from './static.data.js'
3+
</script>
4+
5+
{{ data }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "bar": true }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "foo": true }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
import { fileURLToPath } from 'url'
4+
5+
const dirname = path.dirname(fileURLToPath(import.meta.url))
6+
7+
export default {
8+
watch: ['./data/*'],
9+
async load() {
10+
const foo = fs.readFileSync(
11+
path.resolve(dirname, './data/foo.json'),
12+
'utf-8'
13+
)
14+
const bar = fs.readFileSync(
15+
path.resolve(dirname, './data/bar.json'),
16+
'utf-8'
17+
)
18+
return [JSON.parse(foo), JSON.parse(bar)]
19+
}
20+
}

src/node/staticDataPlugin.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// TODO figure out why it causes full page reload
22

33
import { Plugin, ViteDevServer, loadConfigFromFile, normalizePath } from 'vite'
4-
import { dirname, relative } from 'path'
4+
import { dirname, resolve } from 'path'
55
import { isMatch } from 'micromatch'
66

77
const loaderMatch = /\.data\.(j|t)s$/
@@ -14,7 +14,6 @@ interface LoaderModule {
1414
}
1515

1616
interface CachedLoaderModule {
17-
base: string
1817
pattern: string[] | undefined
1918
loader: () => any
2019
}
@@ -72,8 +71,11 @@ export const staticDataPlugin: Plugin = {
7271
: loaderModule.watch
7372
if (pattern) {
7473
pattern = pattern.map((p) => {
75-
return p.startsWith('./') ? p.slice(2) : p
74+
return p.startsWith('.')
75+
? normalizePath(resolve(base, p))
76+
: normalizePath(p)
7677
})
78+
console.log(pattern)
7779
}
7880
loader = loaderModule.load
7981
}
@@ -83,7 +85,7 @@ export const staticDataPlugin: Plugin = {
8385

8486
// record loader module for HMR
8587
if (server) {
86-
idToLoaderModulesMap[id] = { base, pattern, loader }
88+
idToLoaderModulesMap[id] = { pattern, loader }
8789
}
8890

8991
const result = `export const data = JSON.parse(${JSON.stringify(
@@ -98,27 +100,24 @@ export const staticDataPlugin: Plugin = {
98100
transform(_code, id) {
99101
if (server && loaderMatch.test(id)) {
100102
// register this module as a glob importer
101-
const { base, pattern } = idToLoaderModulesMap[id]!
102-
;(server as any)._globImporters[id] = {
103-
module: server.moduleGraph.getModuleById(id),
104-
importGlobs: pattern?.map((pattern) => ({ base, pattern }))
105-
}
103+
const { pattern } = idToLoaderModulesMap[id]!
104+
;(server as any)._importGlobMap.set(id, [pattern])
106105
}
107106
return null
108107
},
109108

110109
handleHotUpdate(ctx) {
111110
for (const id in idToLoaderModulesMap) {
112-
const { base, pattern } = idToLoaderModulesMap[id]!
111+
const { pattern } = idToLoaderModulesMap[id]!
113112
const isLoaderFile = normalizePath(ctx.file) === id
114113
if (isLoaderFile) {
115114
// invalidate loader file
116115
delete idToLoaderModulesMap[id]
117116
}
118-
if (
119-
isLoaderFile ||
120-
(pattern && isMatch(relative(base, ctx.file), pattern))
121-
) {
117+
if (pattern) {
118+
console.log(pattern, isMatch(ctx.file, pattern))
119+
}
120+
if (isLoaderFile || (pattern && isMatch(ctx.file, pattern))) {
122121
ctx.modules.push(server.moduleGraph.getModuleById(id)!)
123122
}
124123
}

0 commit comments

Comments
 (0)