Skip to content

Commit

Permalink
fix: fix static data file support in vite 3
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Aug 17, 2022
1 parent 43469a2 commit 19ec22c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 14 deletions.
9 changes: 9 additions & 0 deletions examples/configured/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export default defineConfig({
link: '/frontmatter/multiple-levels-outline'
}
]
},
{
text: 'Static Data',
items: [
{
text: 'Test Page',
link: '/static-data/data'
}
]
}
]
}
Expand Down
1 change: 1 addition & 0 deletions examples/configured/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"private": true,
"type": "module",
"name": "vitepress-example-configured",
"scripts": {
"dev": "vitepress dev",
Expand Down
5 changes: 5 additions & 0 deletions examples/configured/static-data/data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script setup>
import { data } from './static.data.js'
</script>

{{ data }}
1 change: 1 addition & 0 deletions examples/configured/static-data/data/bar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "bar": true }
1 change: 1 addition & 0 deletions examples/configured/static-data/data/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "foo": true }
20 changes: 20 additions & 0 deletions examples/configured/static-data/static.data.js
Original file line number Diff line number Diff line change
@@ -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)]
}
}
27 changes: 13 additions & 14 deletions src/node/staticDataPlugin.ts
Original file line number Diff line number Diff line change
@@ -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$/
Expand All @@ -14,7 +14,6 @@ interface LoaderModule {
}

interface CachedLoaderModule {
base: string
pattern: string[] | undefined
loader: () => any
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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(
Expand All @@ -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)!)
}
}
Expand Down

0 comments on commit 19ec22c

Please sign in to comment.