Skip to content

Commit f5308d7

Browse files
committed
feat: support array of patterns in data loaders
1 parent 6ca3c97 commit f5308d7

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/node/staticDataPlugin.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ const loaderMatch = /\.data\.(j|t)s$/
99
let server: ViteDevServer
1010

1111
interface LoaderModule {
12+
watch: string[] | string | undefined
13+
load: () => any
14+
}
15+
16+
interface CachedLoaderModule {
1217
base: string
13-
pattern: string | undefined
18+
pattern: string[] | undefined
1419
loader: () => any
1520
}
1621

17-
const idToLoaderModulesMap: Record<string, LoaderModule | undefined> =
22+
const idToLoaderModulesMap: Record<string, CachedLoaderModule | undefined> =
1823
Object.create(null)
1924

2025
// During build, the load hook will be called on the same file twice
@@ -50,7 +55,7 @@ export const staticDataPlugin: Plugin = {
5055
}
5156

5257
const base = dirname(id)
53-
let pattern: string | undefined
58+
let pattern: string[] | undefined
5459
let loader: () => any
5560

5661
const existing = idToLoaderModulesMap[id]
@@ -60,10 +65,15 @@ export const staticDataPlugin: Plugin = {
6065
// use vite's load config util as a away to load Node.js file with
6166
// TS & native ESM support
6267
const loaderModule = (await loadConfigFromFile({} as any, id))
63-
?.config as any
64-
pattern = loaderModule.watch
65-
if (pattern && pattern.startsWith('./')) {
66-
pattern = pattern.slice(2)
68+
?.config as LoaderModule
69+
pattern =
70+
typeof loaderModule.watch === 'string'
71+
? [loaderModule.watch]
72+
: loaderModule.watch
73+
if (pattern) {
74+
pattern = pattern.map((p) => {
75+
return p.startsWith('./') ? p.slice(2) : p
76+
})
6777
}
6878
loader = loaderModule.load
6979
}
@@ -91,7 +101,7 @@ export const staticDataPlugin: Plugin = {
91101
const { base, pattern } = idToLoaderModulesMap[id]!
92102
;(server as any)._globImporters[id] = {
93103
module: server.moduleGraph.getModuleById(id),
94-
importGlobs: [{ base, pattern }]
104+
importGlobs: pattern?.map((pattern) => ({ base, pattern }))
95105
}
96106
}
97107
return null

0 commit comments

Comments
 (0)