Skip to content

Commit

Permalink
fix(hmr): HMR regression related to .astro updates (#2681)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Feb 28, 2022
1 parent bfaff25 commit 046af36
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-birds-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix HMR regression related to fine-grained `.astro` HMR. This fixes HMR for Tailwind and CSS styles when `.astro` files are edited.
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"htmlparser2": "^7.1.2",
"kleur": "^4.1.4",
"magic-string": "^0.25.7",
"micromorph": "^0.0.2",
"micromorph": "^0.1.1",
"mime": "^3.0.0",
"parse5": "^6.0.1",
"path-to-regexp": "^6.2.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/runtime/client/hmr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
if (import.meta.hot) {
// signal to Vite that we accept HMR
import.meta.hot.accept((mod) => mod);
const parser = new DOMParser();
import.meta.hot.on('astro:update', async ({ file }) => {
const { default: diff } = await import('micromorph');
Expand Down
7 changes: 6 additions & 1 deletion packages/astro/src/vite-plugin-astro/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ export function handleHotUpdate(ctx: HmrContext, config: AstroConfig, logging: L
const filtered = new Set<ModuleNode>(ctx.modules);
const files = new Set<string>();
for (const mod of ctx.modules) {
// This is always the HMR script, we skip it to avoid spamming
// the browser console with HMR updates about this file
if (mod.id?.endsWith('.astro?html-proxy&index=0.js')) {
filtered.delete(mod);
continue;
}
if (mod.file && isCached(config, mod.file)) {
filtered.add(mod);
files.add(mod.file);
Expand All @@ -77,7 +83,6 @@ export function handleHotUpdate(ctx: HmrContext, config: AstroConfig, logging: L
const file = ctx.file.replace(config.projectRoot.pathname, '/');
logger.info('astro', green('hmr'), `${file}`);
ctx.server.ws.send({ type: 'custom', event: 'astro:update', data: { file } });
return [];
}

return Array.from(filtered);
Expand Down
7 changes: 6 additions & 1 deletion packages/astro/src/vite-plugin-astro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
return slash(fileURLToPath(url)) + url.search;
}

let isProduction: boolean;
let viteTransform: TransformHook;
let viteDevServer: vite.ViteDevServer | null = null;

Expand All @@ -46,6 +47,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
enforce: 'pre', // run transforms before other plugins can
configResolved(resolvedConfig) {
viteTransform = getViteTransform(resolvedConfig);
isProduction = resolvedConfig.isProduction;
},
configureServer(server) {
viteDevServer = server;
Expand Down Expand Up @@ -140,8 +142,11 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
sourcefile: id,
});

// Signal to Vite that we accept HMR updates
const SUFFIX = isProduction ? '' : `\nif (import.meta.hot) import.meta.hot.accept((mod) => mod);`;

return {
code,
code: `${code}${SUFFIX}`,
map,
};
} catch (err: any) {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5867,10 +5867,10 @@ micromatch@^4.0.2, micromatch@^4.0.4:
braces "^3.0.1"
picomatch "^2.2.3"

micromorph@^0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/micromorph/-/micromorph-0.0.2.tgz#5cfbaea66ae5fb8629c8041897e88d9e827afc2f"
integrity sha512-hfy/OA8rtwI/vPRm4L6a3/u6uDvqsPmTok7pPmtfv2a7YfaTVfxd9HX2Kdn/SZ8rGMKkKVJ9A0WcBnzs0bjLXw==
micromorph@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/micromorph/-/micromorph-0.1.1.tgz#1772b07f1be6cae10b3210a0f84fe5364987f029"
integrity sha512-VIAYxW6D9yOkcMJK/G6xS1fh8r1gD+mmD4VLPKki7Xqzfrq1qlCfQAA6ITIbUnLDAHv8UpPPhCWzFJ29fiUjZQ==

mime@1.6.0:
version "1.6.0"
Expand Down

0 comments on commit 046af36

Please sign in to comment.