Skip to content

Commit

Permalink
feat: ultilize _importGlobMap to hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanimodori committed Nov 4, 2022
1 parent 30135dd commit a8c8595
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/types/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ export interface HmrData extends WatchItem {
timestamp: number;
}

declare module 'vite' {
interface ViteDevServer {
/** vite internal _importGlobMap for detemine glob hmr */
_importGlobMap: Map<string, string[]>;
}
}

export interface ViteBurnerServer extends Omit<ViteDevServer, 'config'> {
config: ResolvedConfig;
pathToId(file: string): string;
Expand Down
35 changes: 34 additions & 1 deletion src/ws/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import {
import { WsManager } from './manager';
import fs from 'fs';
import pc from 'picocolors';
import path, { resolve } from 'path';
import path, { relative, resolve } from 'path';
import { slash } from 'vite-node/utils';
import fg from 'fast-glob';
import { fixImportPath } from './import';
import { ViteBurnerServer, HmrData } from '@/types';
import { match } from 'micromatch';

export const formatUpload = (from: string, to: string, serverName: string) => {
to = forceStartingSlash(to);
Expand Down Expand Up @@ -87,12 +88,44 @@ export class WsAdapter {
logger.error(`error getting dts file: ${e}`);
}
}
async checkDependencies(data: HmrData[]) {
for (const item of data) {
// change won't affect import glob generated files, skippping
if (item.event === 'change') {
continue;
}
const resolvedFile = slash(resolve(this.server.config.root, item.file));
this.server._importGlobMap?.forEach((value, key) => {
if (value.some((pattern) => match([resolvedFile], pattern).length > 0)) {
// push key to data
const importer = slash(relative(this.server.config.root, key));
// recursive import, skipping
if (data.some((item) => item.file === importer)) {
return;
}
const importerData = this.server.watchManager.findItem(importer);
if (importerData?.transform) {
data.push({
file: importer,
timestamp: item.timestamp,
initial: item.initial,
event: 'change',
...importerData,
});
}
}
});
}
return data;
}
async handleHmrMessage(data?: HmrData | HmrData[]) {
if (!data) {
data = [];
} else if (!Array.isArray(data)) {
data = [data];
}
// check deps
data = await this.checkDependencies(data);
const connected = this.manager.connected;
for (const item of data) {
this.buffers.set(item.file, item);
Expand Down

0 comments on commit a8c8595

Please sign in to comment.