Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-enable watch tests #2126

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/vite/src/dep-tracker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { ViteDevServer } from 'vite';
import makeDebug from 'debug';

const debug = makeDebug('embroider:vite');

export class DepTracker {
#virtualDeps: Map<string, string[]> = new Map();
#server: ViteDevServer;

constructor(server: ViteDevServer) {
this.#server = server;
server.watcher.on('all', (_eventName, path) => {
for (let [id, watches] of this.#virtualDeps) {
for (let watch of watches) {
if (path.startsWith(watch)) {
debug('Invalidate %s because %s', id, path);
server.moduleGraph.onFileChange(id);
let m = server.moduleGraph.getModuleById(id);
if (m) {
server.reloadModule(m);
}
}
}
}
});
}

trackDeps(id: string, deps: string[]) {
this.#virtualDeps.set(id, deps);
this.#server.watcher.add(deps);
}
}
44 changes: 34 additions & 10 deletions packages/vite/src/hbs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createFilter } from '@rollup/pluginutils';
import type { PluginContext } from 'rollup';
import type { Plugin } from 'vite';

import {
hbsToJS,
ResolverLoader,
Expand All @@ -9,14 +10,22 @@ import {
templateOnlyComponentSource,
syntheticJStoHBS,
} from '@embroider/core';
import { DepTracker } from './dep-tracker.js';

const resolverLoader = new ResolverLoader(process.cwd());
const hbsFilter = createFilter('**/*.hbs?([?]*)');

export function hbs(): Plugin {
let depTracker: DepTracker | undefined;

return {
name: 'rollup-hbs-plugin',
enforce: 'pre',

configureServer(s) {
depTracker = new DepTracker(s);
},

async resolveId(source: string, importer: string | undefined, options) {
if (options.custom?.depScan) {
// during depscan we have a corresponding esbuild plugin that is
Expand Down Expand Up @@ -62,16 +71,31 @@ export function hbs(): Plugin {
}
}

let syntheticId = needsSyntheticComponentJS(source, resolution.id);
if (syntheticId && isInComponents(resolution.id, resolverLoader.resolver.packageCache)) {
return {
id: syntheticId,
meta: {
'rollup-hbs-plugin': {
type: 'template-only-component-js',
if (isInComponents(resolution.id, resolverLoader.resolver.packageCache)) {
let syntheticId = needsSyntheticComponentJS(source, resolution.id);
if (syntheticId) {
depTracker?.trackDeps(syntheticId, [resolution.id]);
return {
id: syntheticId,
meta: {
'rollup-hbs-plugin': {
type: 'template-only-component-js',
},
},
},
};
};
} else {
let correspondingHBS = syntheticJStoHBS(resolution.id);
if (correspondingHBS) {
depTracker?.trackDeps(resolution.id, [correspondingHBS]);
}
}
}

// we should be able to clear any earlier meta by returning
// resolution.meta here, but vite breaks that rollup feature.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use own cache then?
MetaCache = new Map();
And set the value for the id in the resolveId

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could but this is working now. I checked and the rollup docs say it's OK to mutate the meta object that comes back from getModuleInfo().meta.

let meta = getMeta(this, resolution.id);
if (meta) {
meta.type = null;
}

return resolution;
Expand All @@ -95,7 +119,7 @@ export function hbs(): Plugin {
}

type Meta = {
type: 'template-only-component-js';
type: 'template-only-component-js' | null;
};

function getMeta(context: PluginContext, id: string): Meta | null {
Expand Down
25 changes: 5 additions & 20 deletions packages/vite/src/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Plugin, ViteDevServer } from 'vite';
import type { Plugin } from 'vite';
import core, { type Resolver } from '@embroider/core';
const { virtualContent, ResolverLoader, explicitRelative, cleanUrl, tmpdir } = core;
import { RollupModuleRequest, virtualPrefix } from './request.js';
Expand All @@ -10,37 +10,23 @@ import type { PluginContext, ResolveIdResult } from 'rollup';
import { externalName } from '@embroider/reverse-exports';
import fs from 'fs-extra';
import { createHash } from 'crypto';
import { DepTracker } from './dep-tracker.js';

const { ensureSymlinkSync, outputJSONSync } = fs;

const debug = makeDebug('embroider:vite');

export function resolver(): Plugin {
const resolverLoader = new ResolverLoader(process.cwd());
let server: ViteDevServer;
const virtualDeps: Map<string, string[]> = new Map();
let depTracker: DepTracker | undefined;
const notViteDeps = new Set<string>();

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)) {
debug('Invalidate %s because %s', id, path);
server.moduleGraph.onFileChange(id);
let m = server.moduleGraph.getModuleById(id);
if (m) {
server.reloadModule(m);
}
}
}
}
});
depTracker = new DepTracker(s);
},

async resolveId(source, importer, options) {
Expand Down Expand Up @@ -73,8 +59,7 @@ export function resolver(): Plugin {
if (id.startsWith(virtualPrefix)) {
let { pathname } = new URL(id, 'http://example.com');
let { src, watches } = virtualContent(pathname.slice(virtualPrefix.length + 1), resolverLoader.resolver);
virtualDeps.set(id, watches);
server?.watcher.add(watches);
depTracker?.trackDeps(id, watches);
return src;
}
},
Expand Down
Loading
Loading