Skip to content

Commit

Permalink
Update plugin.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
JakiChen committed Nov 19, 2024
1 parent dad30db commit e47bb01
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { AstroConfig, AstroIntegrationLogger } from "astro";
import type { AstroConfig } from "astro";
import type { Plugin } from "vite";
import fs from "fs/promises";
import path from "path";
import { type SVGsOptions, name } from ".";
import { compose } from "./core";
import { virtual } from "./modules";

export function create(options: SVGsOptions, config: AstroConfig): Plugin {
const virtualModuleId = `virtual:${name}`;
Expand Down Expand Up @@ -54,18 +56,30 @@ export function create(options: SVGsOptions, config: AstroConfig): Plugin {
},

async handleHotUpdate({ file, server }) {
// const filePath = path.dirname(file);
// if (
// Array.isArray(options.input) &&
// options.input.some((input) => filePath.includes(input))
// ) {
const mod = server.moduleGraph.getModuleById(resolvedVirtualModuleId);
if (mod) {
server.moduleGraph.invalidateModule(mod);
const filePathDir = path.dirname(file);

if (
Array.isArray(options.input) &&
options.input.some((input) => filePathDir.includes(input))
) {
({ hash, data } = await compose(options)); // 更新 hash 和 data
filePath = `${base}?v=${hash}`; // 更新 filePath

const { filename, content } = await virtual(options);
const typeFile = new URL(
`.astro/integrations/${name}/${filename}`,
config.root,
);
await fs.writeFile(typeFile, content);

const mod = server.moduleGraph.getModuleById(resolvedVirtualModuleId);
if (mod) {
server.moduleGraph.invalidateModule(mod);
}

server.ws.send({ type: "full-reload" });
return [];
}
server.ws.send({ type: "full-reload" });
return [];
// }
},
};
}

0 comments on commit e47bb01

Please sign in to comment.