diff --git a/__tests__/e2e/data-loading/data.test.ts b/__tests__/e2e/data-loading/data.test.ts index 394ab1d2..fafc403a 100644 --- a/__tests__/e2e/data-loading/data.test.ts +++ b/__tests__/e2e/data-loading/data.test.ts @@ -43,8 +43,7 @@ describe('static data file support in vite 3', () => { `) }) - // TODO: make it `.runIf(!process.env.VITE_TEST_BUILD)` -- it currently works, but is skipped to avoid vite's ecosystem-ci from failing (https://github.com/vitejs/vite/pull/16471#issuecomment-2308437187) - test.skip('hmr works', async () => { + test.runIf(!process.env.VITE_TEST_BUILD)('hmr works', async () => { const a = fileURLToPath(new URL('./data/a.json', import.meta.url)) const b = fileURLToPath(new URL('./data/b.json', import.meta.url)) diff --git a/src/node/plugins/staticDataPlugin.ts b/src/node/plugins/staticDataPlugin.ts index 63b83c3e..6d63030b 100644 --- a/src/node/plugins/staticDataPlugin.ts +++ b/src/node/plugins/staticDataPlugin.ts @@ -2,6 +2,7 @@ import { isMatch } from 'micromatch' import path, { dirname, resolve } from 'node:path' import { glob } from 'tinyglobby' import { + type EnvironmentModuleNode, type Plugin, type ViteDevServer, loadConfigFromFile, @@ -122,44 +123,27 @@ export const staticDataPlugin: Plugin = { } }, - transform(_code, id) { - if (server && loaderMatch.test(id) && '_importGlobMap' in server) { - // register this module as a glob importer - const { watch } = idToLoaderModulesMap[id]! - if (watch) { - ;(server as any)._importGlobMap.set( - id, - [Array.isArray(watch) ? watch : [watch]].map((globs) => { - const affirmed: string[] = [] - const negated: string[] = [] - - for (const glob of globs) { - ;(glob[0] === '!' ? negated : affirmed).push(glob) - } - return { affirmed, negated } - }) - ) - } - } - return null - }, + hotUpdate(ctx) { + if (ctx.type === 'update') return - handleHotUpdate(ctx) { const file = ctx.file + const modules: EnvironmentModuleNode[] = [] // dependency of data loader changed // (note the dep array includes the loader file itself) if (file in depToLoaderModuleIdMap) { const id = depToLoaderModuleIdMap[file]! delete idToLoaderModulesMap[id] - ctx.modules.push(server.moduleGraph.getModuleById(id)!) + modules.push(this.environment.moduleGraph.getModuleById(id)!) } for (const id in idToLoaderModulesMap) { const { watch } = idToLoaderModulesMap[id]! if (watch && isMatch(file, watch)) { - ctx.modules.push(server.moduleGraph.getModuleById(id)!) + modules.push(this.environment.moduleGraph.getModuleById(id)!) } } + + return modules.length > 0 ? [...ctx.modules, ...modules] : undefined } }