Skip to content

Commit

Permalink
Update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Feb 20, 2024
1 parent 9af80c6 commit c677b82
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
23 changes: 12 additions & 11 deletions packages/astro/src/vite-plugin-astro/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,30 @@ export async function handleHotUpdate(
ctx: HmrContext,
{ logger, astroFileToCompileMetadata }: HandleHotUpdateOptions
) {
const compileMetadata = astroFileToCompileMetadata.get(ctx.file);

// If `ctx.file` is part of a CSS dependency of any Astro file, invalidate its `astroFileToCompileMetadata`
// so the next transform will re-generate it.
// HANDLING 1: Invalidate compile metadata if CSS dependency updated
//
// If any `ctx.file` is part of a CSS dependency of any Astro file, invalidate its `astroFileToCompileMetadata`
// so the next transform of the Astro file or Astro script/style virtual module will re-generate it
for (const [astroFile, compileData] of astroFileToCompileMetadata) {
const isUpdatedFileCssDep = compileData.css.some((css) => css.dependencies?.includes(ctx.file));
if (isUpdatedFileCssDep) {
astroFileToCompileMetadata.delete(astroFile);
}
}

// Bail if not Astro file, we only handle them below
if (compileMetadata == null || !ctx.file.endsWith('.astro')) return;

const oldCode = compileMetadata.originalCode;
const newCode = await ctx.read();

// HANDLING 2: Only invalidate Astro style virtual module if only style tags changed
//
// If only the style code has changed, e.g. editing the `color`, then we can directly invalidate
// the Astro CSS virtual modules only. The main Astro module's JS result will be the same and doesn't
// need to be invalidated.
const oldCode = astroFileToCompileMetadata.get(ctx.file)?.originalCode;
if (oldCode == null) return;
const newCode = await ctx.read();

if (isStyleOnlyChanged(oldCode, newCode)) {
logger.debug('watch', 'style-only change');
// Invalidate its `astroFileToCompileMetadata` so that its next transform will re-generate it
// Invalidate its `astroFileToCompileMetadata` so that the next transform of Astro style virtual module
// will re-generate it
astroFileToCompileMetadata.delete(ctx.file);
// Only return the Astro styles that have changed!
return ctx.modules.filter((mod) => mod.id?.includes('astro&type=style'));
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/vite-plugin-astro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default function astro({ settings, logger }: AstroPluginOptions): vite.Pl
// We try to re-compile the main Astro module (`filename`) first before retrieving the metadata again.
if (!compileMetadata && server) {
const code = await loadId(server.pluginContainer, filename);
// `compile` should re-set `filename` in `astroFileToCompileMetadata`
if (code != null) await compile(code, filename);
compileMetadata = astroFileToCompileMetadata.get(filename);
}
Expand Down

0 comments on commit c677b82

Please sign in to comment.