Skip to content

Commit

Permalink
working vite-specific invalidation of virtual files
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Jan 25, 2024
1 parent 2b57f5b commit 89cc2f8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/virtual-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')],
};
}

Expand Down
28 changes: 24 additions & 4 deletions packages/vite/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
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';
import assertNever from 'assert-never';

export function resolver(): Plugin {
let resolverLoader = new ResolverLoader(process.cwd());
let server: ViteDevServer;
let virtualDeps: Map<string, string[]> = 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) {
Expand All @@ -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;
}
},
Expand Down
2 changes: 1 addition & 1 deletion tests/vite-app/app/components/example.hbs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div>hey {{@message}} <Fancy /></div>
<pre>{{@message}}</pre>
3 changes: 2 additions & 1 deletion tests/vite-app/app/routes/application.ts
Original file line number Diff line number Diff line change
@@ -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') };
}
}

0 comments on commit 89cc2f8

Please sign in to comment.