diff --git a/examples/configured/.vitepress/config.js b/examples/configured/.vitepress/config.js index f487db6d5c38..e027a483b72c 100644 --- a/examples/configured/.vitepress/config.js +++ b/examples/configured/.vitepress/config.js @@ -15,6 +15,15 @@ export default defineConfig({ link: '/frontmatter/multiple-levels-outline' } ] + }, + { + text: 'Static Data', + items: [ + { + text: 'Test Page', + link: '/static-data/data' + } + ] } ] } diff --git a/examples/configured/package.json b/examples/configured/package.json index 10314a854e98..5f59c8300b21 100644 --- a/examples/configured/package.json +++ b/examples/configured/package.json @@ -1,5 +1,6 @@ { "private": true, + "type": "module", "name": "vitepress-example-configured", "scripts": { "dev": "vitepress dev", diff --git a/examples/configured/static-data/data.md b/examples/configured/static-data/data.md new file mode 100644 index 000000000000..d92433ee5215 --- /dev/null +++ b/examples/configured/static-data/data.md @@ -0,0 +1,5 @@ + + +{{ data }} diff --git a/examples/configured/static-data/data/bar.json b/examples/configured/static-data/data/bar.json new file mode 100644 index 000000000000..01309803beec --- /dev/null +++ b/examples/configured/static-data/data/bar.json @@ -0,0 +1 @@ +{ "bar": true } diff --git a/examples/configured/static-data/data/foo.json b/examples/configured/static-data/data/foo.json new file mode 100644 index 000000000000..46a10aa7fea1 --- /dev/null +++ b/examples/configured/static-data/data/foo.json @@ -0,0 +1 @@ +{ "foo": true } diff --git a/examples/configured/static-data/static.data.js b/examples/configured/static-data/static.data.js new file mode 100644 index 000000000000..9c66771b3e83 --- /dev/null +++ b/examples/configured/static-data/static.data.js @@ -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)] + } +} diff --git a/src/node/staticDataPlugin.ts b/src/node/staticDataPlugin.ts index 7ff9dc916d42..0112f1b50296 100644 --- a/src/node/staticDataPlugin.ts +++ b/src/node/staticDataPlugin.ts @@ -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)!) } }