feat: support array of patterns in data loaders

pull/478/head
Evan You 3 years ago
parent 6ca3c97ab9
commit f5308d746f

@ -9,12 +9,17 @@ const loaderMatch = /\.data\.(j|t)s$/
let server: ViteDevServer let server: ViteDevServer
interface LoaderModule { interface LoaderModule {
watch: string[] | string | undefined
load: () => any
}
interface CachedLoaderModule {
base: string base: string
pattern: string | undefined pattern: string[] | undefined
loader: () => any loader: () => any
} }
const idToLoaderModulesMap: Record<string, LoaderModule | undefined> = const idToLoaderModulesMap: Record<string, CachedLoaderModule | undefined> =
Object.create(null) Object.create(null)
// During build, the load hook will be called on the same file twice // During build, the load hook will be called on the same file twice
@ -50,7 +55,7 @@ export const staticDataPlugin: Plugin = {
} }
const base = dirname(id) const base = dirname(id)
let pattern: string | undefined let pattern: string[] | undefined
let loader: () => any let loader: () => any
const existing = idToLoaderModulesMap[id] const existing = idToLoaderModulesMap[id]
@ -60,10 +65,15 @@ export const staticDataPlugin: Plugin = {
// use vite's load config util as a away to load Node.js file with // use vite's load config util as a away to load Node.js file with
// TS & native ESM support // TS & native ESM support
const loaderModule = (await loadConfigFromFile({} as any, id)) const loaderModule = (await loadConfigFromFile({} as any, id))
?.config as any ?.config as LoaderModule
pattern = loaderModule.watch pattern =
if (pattern && pattern.startsWith('./')) { typeof loaderModule.watch === 'string'
pattern = pattern.slice(2) ? [loaderModule.watch]
: loaderModule.watch
if (pattern) {
pattern = pattern.map((p) => {
return p.startsWith('./') ? p.slice(2) : p
})
} }
loader = loaderModule.load loader = loaderModule.load
} }
@ -91,7 +101,7 @@ export const staticDataPlugin: Plugin = {
const { base, pattern } = idToLoaderModulesMap[id]! const { base, pattern } = idToLoaderModulesMap[id]!
;(server as any)._globImporters[id] = { ;(server as any)._globImporters[id] = {
module: server.moduleGraph.getModuleById(id), module: server.moduleGraph.getModuleById(id),
importGlobs: [{ base, pattern }] importGlobs: pattern?.map((pattern) => ({ base, pattern }))
} }
} }
return null return null

Loading…
Cancel
Save