|
|
@ -1,7 +1,7 @@
|
|
|
|
// TODO figure out why it causes full page reload
|
|
|
|
// TODO figure out why it causes full page reload
|
|
|
|
|
|
|
|
|
|
|
|
import { Plugin, ViteDevServer, loadConfigFromFile, normalizePath } from 'vite'
|
|
|
|
import { Plugin, ViteDevServer, loadConfigFromFile, normalizePath } from 'vite'
|
|
|
|
import { dirname, relative } from 'path'
|
|
|
|
import { dirname, resolve } from 'path'
|
|
|
|
import { isMatch } from 'micromatch'
|
|
|
|
import { isMatch } from 'micromatch'
|
|
|
|
|
|
|
|
|
|
|
|
const loaderMatch = /\.data\.(j|t)s$/
|
|
|
|
const loaderMatch = /\.data\.(j|t)s$/
|
|
|
@ -14,7 +14,6 @@ interface LoaderModule {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface CachedLoaderModule {
|
|
|
|
interface CachedLoaderModule {
|
|
|
|
base: string
|
|
|
|
|
|
|
|
pattern: string[] | undefined
|
|
|
|
pattern: string[] | undefined
|
|
|
|
loader: () => any
|
|
|
|
loader: () => any
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -72,8 +71,11 @@ export const staticDataPlugin: Plugin = {
|
|
|
|
: loaderModule.watch
|
|
|
|
: loaderModule.watch
|
|
|
|
if (pattern) {
|
|
|
|
if (pattern) {
|
|
|
|
pattern = pattern.map((p) => {
|
|
|
|
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
|
|
|
|
loader = loaderModule.load
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -83,7 +85,7 @@ export const staticDataPlugin: Plugin = {
|
|
|
|
|
|
|
|
|
|
|
|
// record loader module for HMR
|
|
|
|
// record loader module for HMR
|
|
|
|
if (server) {
|
|
|
|
if (server) {
|
|
|
|
idToLoaderModulesMap[id] = { base, pattern, loader }
|
|
|
|
idToLoaderModulesMap[id] = { pattern, loader }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const result = `export const data = JSON.parse(${JSON.stringify(
|
|
|
|
const result = `export const data = JSON.parse(${JSON.stringify(
|
|
|
@ -98,27 +100,24 @@ export const staticDataPlugin: Plugin = {
|
|
|
|
transform(_code, id) {
|
|
|
|
transform(_code, id) {
|
|
|
|
if (server && loaderMatch.test(id)) {
|
|
|
|
if (server && loaderMatch.test(id)) {
|
|
|
|
// register this module as a glob importer
|
|
|
|
// register this module as a glob importer
|
|
|
|
const { base, pattern } = idToLoaderModulesMap[id]!
|
|
|
|
const { pattern } = idToLoaderModulesMap[id]!
|
|
|
|
;(server as any)._globImporters[id] = {
|
|
|
|
;(server as any)._importGlobMap.set(id, [pattern])
|
|
|
|
module: server.moduleGraph.getModuleById(id),
|
|
|
|
|
|
|
|
importGlobs: pattern?.map((pattern) => ({ base, pattern }))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
return null
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
handleHotUpdate(ctx) {
|
|
|
|
handleHotUpdate(ctx) {
|
|
|
|
for (const id in idToLoaderModulesMap) {
|
|
|
|
for (const id in idToLoaderModulesMap) {
|
|
|
|
const { base, pattern } = idToLoaderModulesMap[id]!
|
|
|
|
const { pattern } = idToLoaderModulesMap[id]!
|
|
|
|
const isLoaderFile = normalizePath(ctx.file) === id
|
|
|
|
const isLoaderFile = normalizePath(ctx.file) === id
|
|
|
|
if (isLoaderFile) {
|
|
|
|
if (isLoaderFile) {
|
|
|
|
// invalidate loader file
|
|
|
|
// invalidate loader file
|
|
|
|
delete idToLoaderModulesMap[id]
|
|
|
|
delete idToLoaderModulesMap[id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
if (pattern) {
|
|
|
|
isLoaderFile ||
|
|
|
|
console.log(pattern, isMatch(ctx.file, pattern))
|
|
|
|
(pattern && isMatch(relative(base, ctx.file), pattern))
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
if (isLoaderFile || (pattern && isMatch(ctx.file, pattern))) {
|
|
|
|
ctx.modules.push(server.moduleGraph.getModuleById(id)!)
|
|
|
|
ctx.modules.push(server.moduleGraph.getModuleById(id)!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|