diff --git a/packages/core/src/virtual-content.ts b/packages/core/src/virtual-content.ts index cfa8b7745a..0bab62f802 100644 --- a/packages/core/src/virtual-content.ts +++ b/packages/core/src/virtual-content.ts @@ -19,7 +19,7 @@ export function virtualContent(filename: string, resolver: Resolver): { src: str src: `export default [ ${files.map(f => `"${f}"`).join(',')} ]`, - watches: [join(resolver.options.appRoot, 'app', 'app.js')], + watches: [join(resolver.options.appRoot, 'app')], }; } diff --git a/packages/vite/src/resolver.ts b/packages/vite/src/resolver.ts index ddcd47557a..e95da4e509 100644 --- a/packages/vite/src/resolver.ts +++ b/packages/vite/src/resolver.ts @@ -1,5 +1,5 @@ import type { PluginContext, ResolveIdResult } from 'rollup'; -import type { Plugin } from 'vite'; +import type { Plugin, ViteDevServer } from 'vite'; import type { Resolution, ResolverFunction } from '@embroider/core'; import { virtualContent, ResolverLoader } from '@embroider/core'; import { RollupModuleRequest, virtualPrefix } from './request'; @@ -7,10 +7,31 @@ import assertNever from 'assert-never'; export function resolver(): Plugin { let resolverLoader = new ResolverLoader(process.cwd()); + let server: ViteDevServer; + let virtualDeps: Map = new Map(); return { name: 'embroider-resolver', enforce: 'pre', + + configureServer(s) { + server = s; + server.watcher.on('all', (_eventName, path) => { + for (let [id, watches] of virtualDeps) { + for (let watch of watches) { + if (path.startsWith(watch)) { + console.log(`Invalidate ${id} because ${path}`); + server.moduleGraph.onFileChange(id); + let m = server.moduleGraph.getModuleById(id); + if (m) { + server.reloadModule(m); + } + } + } + } + }); + }, + async resolveId(source, importer, options) { let request = RollupModuleRequest.from(source, importer, options.custom); if (!request) { @@ -30,9 +51,8 @@ export function resolver(): Plugin { load(id) { if (id.startsWith(virtualPrefix)) { let { src, watches } = virtualContent(id.slice(virtualPrefix.length), resolverLoader.resolver); - for (let watch of watches) { - this.addWatchFile(watch); - } + virtualDeps.set(id, watches); + server.watcher.add(watches); return src; } }, diff --git a/tests/vite-app/app/components/example.hbs b/tests/vite-app/app/components/example.hbs index 6224387eed..b6b23fd110 100644 --- a/tests/vite-app/app/components/example.hbs +++ b/tests/vite-app/app/components/example.hbs @@ -1 +1 @@ -
hey {{@message}}
\ No newline at end of file +
{{@message}}
\ No newline at end of file diff --git a/tests/vite-app/app/routes/application.ts b/tests/vite-app/app/routes/application.ts index 8081a29552..5e0b96be59 100644 --- a/tests/vite-app/app/routes/application.ts +++ b/tests/vite-app/app/routes/application.ts @@ -1,7 +1,8 @@ import Route from '@ember/routing/route'; +import files from '#the-file-list'; export default class extends Route { async model() { - return { message: 'Hello world' }; + return { message: files.join('\n') }; } }