Skip to content

Commit

Permalink
feat(data-loader): pass watched files into load()
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 7, 2023
1 parent de84c01 commit e29b6a0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
10 changes: 3 additions & 7 deletions __tests__/e2e/static-data/static.data.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'

const dirname = path.dirname(fileURLToPath(import.meta.url))

type Data = Record<string, boolean>[]
export declare const data: Data

export default {
watch: ['./data/*'],
async load(): Promise<Data> {
async load(files: string[]): Promise<Data> {
const foo = fs.readFileSync(
path.resolve(dirname, './data/foo.json'),
files.find((f) => f.endsWith('foo.json'))!,
'utf-8'
)
const bar = fs.readFileSync(
path.resolve(dirname, './data/bar.json'),
files.find((f) => f.endsWith('bar.json'))!,
'utf-8'
)
return [JSON.parse(foo), JSON.parse(bar)]
Expand Down
50 changes: 28 additions & 22 deletions src/node/plugins/staticDataPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@ import {
} from 'vite'
import path, { dirname, resolve } from 'path'
import { isMatch } from 'micromatch'
import glob from 'fast-glob'

const loaderMatch = /\.data\.(j|t)s$/

let server: ViteDevServer

interface LoaderModule {
watch: string[] | string | undefined
load: () => any
watch?: string[] | string
load: (watchedFiles: string[]) => any
}

interface CachedLoaderModule {
pattern: string[] | undefined
loader: () => any
}

const idToLoaderModulesMap: Record<string, CachedLoaderModule | undefined> =
const idToLoaderModulesMap: Record<string, LoaderModule | undefined> =
Object.create(null)

const depToLoaderModuleIdMap: Record<string, string> = Object.create(null)
Expand Down Expand Up @@ -59,12 +55,12 @@ export const staticDataPlugin: Plugin = {
}

const base = dirname(id)
let pattern: string[] | undefined
let loader: () => any
let watch: LoaderModule['watch']
let load: LoaderModule['load']

const existing = idToLoaderModulesMap[id]
if (existing) {
;({ pattern, loader } = existing)
;({ watch, load } = existing)
} else {
// use vite's load config util as a away to load Node.js file with
// TS & native ESM support
Expand All @@ -78,26 +74,34 @@ export const staticDataPlugin: Plugin = {
}

const loaderModule = res?.config as LoaderModule
pattern =
watch =
typeof loaderModule.watch === 'string'
? [loaderModule.watch]
: loaderModule.watch
if (pattern) {
pattern = pattern.map((p) => {
if (watch) {
watch = watch.map((p) => {
return p.startsWith('.')
? normalizePath(resolve(base, p))
: normalizePath(p)
})
}
loader = loaderModule.load
load = loaderModule.load
}

// load the data
const data = await loader()
let watchedFiles
if (watch) {
watchedFiles = (
await glob(watch, {
ignore: ['**/node_modules/**', '**/dist/**']
})
).sort()
}
const data = await load(watchedFiles || [])

// record loader module for HMR
if (server) {
idToLoaderModulesMap[id] = { pattern, loader }
idToLoaderModulesMap[id] = { watch, load }
}

const result = `export const data = JSON.parse(${JSON.stringify(
Expand All @@ -112,9 +116,11 @@ export const staticDataPlugin: Plugin = {
transform(_code, id) {
if (server && loaderMatch.test(id)) {
// register this module as a glob importer
const { pattern } = idToLoaderModulesMap[id]!
if (pattern) {
;(server as any)._importGlobMap.set(id, [pattern])
const { watch } = idToLoaderModulesMap[id]!
if (watch) {
;(server as any)._importGlobMap.set(id, [
Array.isArray(watch) ? watch : [watch]
])
}
}
return null
Expand All @@ -132,8 +138,8 @@ export const staticDataPlugin: Plugin = {
}

for (const id in idToLoaderModulesMap) {
const { pattern } = idToLoaderModulesMap[id]!
if (pattern && isMatch(file, pattern)) {
const { watch } = idToLoaderModulesMap[id]!
if (watch && isMatch(file, watch)) {
ctx.modules.push(server.moduleGraph.getModuleById(id)!)
}
}
Expand Down

0 comments on commit e29b6a0

Please sign in to comment.