From 5c96d2122582100b66b07becef1f48f0ebd24348 Mon Sep 17 00:00:00 2001 From: Florian LEFEBVRE Date: Wed, 18 Oct 2023 16:54:42 +0200 Subject: [PATCH 1/8] fix(astro): terminal HMR path (close #8831) --- .changeset/two-lamps-tell.md | 5 +++++ packages/astro/src/vite-plugin-astro/hmr.ts | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 .changeset/two-lamps-tell.md diff --git a/.changeset/two-lamps-tell.md b/.changeset/two-lamps-tell.md new file mode 100644 index 000000000000..5a1d52945cbb --- /dev/null +++ b/.changeset/two-lamps-tell.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes the terminal HMR path diff --git a/packages/astro/src/vite-plugin-astro/hmr.ts b/packages/astro/src/vite-plugin-astro/hmr.ts index 6600b2f42271..8feadd06ee92 100644 --- a/packages/astro/src/vite-plugin-astro/hmr.ts +++ b/packages/astro/src/vite-plugin-astro/hmr.ts @@ -1,4 +1,5 @@ import { fileURLToPath } from 'node:url'; +import { relative } from "node:path"; import type { HmrContext, ModuleNode } from 'vite'; import type { AstroConfig } from '../@types/astro.js'; import { @@ -91,7 +92,7 @@ export async function handleHotUpdate( // Bugfix: sometimes style URLs get normalized and end with `lang.css=` // These will cause full reloads, so filter them out here const mods = [...filtered].filter((m) => !m.url.endsWith('=')); - const file = ctx.file.replace(config.root.pathname, '/'); + const file = relative(process.cwd(), ctx.file.replace(config.root.pathname, '/')); // If only styles are changed, remove the component file from the update list if (isStyleOnlyChange) { From 85903f447bd8f5b2c3ea0a29475d2ecb910b7b87 Mon Sep 17 00:00:00 2001 From: Florian LEFEBVRE Date: Wed, 18 Oct 2023 17:45:34 +0200 Subject: [PATCH 2/8] fix: prepend slash if needed --- packages/astro/src/vite-plugin-astro/hmr.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/vite-plugin-astro/hmr.ts b/packages/astro/src/vite-plugin-astro/hmr.ts index 8feadd06ee92..191189378330 100644 --- a/packages/astro/src/vite-plugin-astro/hmr.ts +++ b/packages/astro/src/vite-plugin-astro/hmr.ts @@ -1,5 +1,4 @@ import { fileURLToPath } from 'node:url'; -import { relative } from "node:path"; import type { HmrContext, ModuleNode } from 'vite'; import type { AstroConfig } from '../@types/astro.js'; import { @@ -92,7 +91,11 @@ export async function handleHotUpdate( // Bugfix: sometimes style URLs get normalized and end with `lang.css=` // These will cause full reloads, so filter them out here const mods = [...filtered].filter((m) => !m.url.endsWith('=')); - const file = relative(process.cwd(), ctx.file.replace(config.root.pathname, '/')); + let file = ctx.file; + if (!file.startsWith('/')) { + file = `/${file}` + } + file = file.replace(config.root.pathname, '/') // If only styles are changed, remove the component file from the update list if (isStyleOnlyChange) { From 97993255e2314cc20bac72f2d5414425fc9778ae Mon Sep 17 00:00:00 2001 From: Florian LEFEBVRE Date: Wed, 18 Oct 2023 19:18:10 +0200 Subject: [PATCH 3/8] fix: handle spaces --- packages/astro/src/vite-plugin-astro/hmr.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/vite-plugin-astro/hmr.ts b/packages/astro/src/vite-plugin-astro/hmr.ts index 191189378330..09c4a219c069 100644 --- a/packages/astro/src/vite-plugin-astro/hmr.ts +++ b/packages/astro/src/vite-plugin-astro/hmr.ts @@ -95,7 +95,7 @@ export async function handleHotUpdate( if (!file.startsWith('/')) { file = `/${file}` } - file = file.replace(config.root.pathname, '/') + file = file.replaceAll(' ', '%20').replace(config.root.pathname, '/') // If only styles are changed, remove the component file from the update list if (isStyleOnlyChange) { From e968db820b057aad7b60acfc9c14188809d72798 Mon Sep 17 00:00:00 2001 From: Florian LEFEBVRE Date: Wed, 18 Oct 2023 22:28:47 +0200 Subject: [PATCH 4/8] fix: handle any encoded url --- packages/astro/src/vite-plugin-astro/hmr.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/astro/src/vite-plugin-astro/hmr.ts b/packages/astro/src/vite-plugin-astro/hmr.ts index 09c4a219c069..174c3ed2b0a2 100644 --- a/packages/astro/src/vite-plugin-astro/hmr.ts +++ b/packages/astro/src/vite-plugin-astro/hmr.ts @@ -91,11 +91,9 @@ export async function handleHotUpdate( // Bugfix: sometimes style URLs get normalized and end with `lang.css=` // These will cause full reloads, so filter them out here const mods = [...filtered].filter((m) => !m.url.endsWith('=')); - let file = ctx.file; - if (!file.startsWith('/')) { - file = `/${file}` - } - file = file.replaceAll(' ', '%20').replace(config.root.pathname, '/') + const file = fileURLToPath(`file:///${ctx.file}`) + .replace(fileURLToPath(config.root), '/') + .replace(/\\/g, '/'); // If only styles are changed, remove the component file from the update list if (isStyleOnlyChange) { From c3c604f0244a027aa379daa1b2cb79535a373175 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 19 Oct 2023 08:43:25 +0200 Subject: [PATCH 5/8] Update two-lamps-tell.md --- .changeset/two-lamps-tell.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/two-lamps-tell.md b/.changeset/two-lamps-tell.md index 5a1d52945cbb..eb66f7a556ea 100644 --- a/.changeset/two-lamps-tell.md +++ b/.changeset/two-lamps-tell.md @@ -2,4 +2,4 @@ 'astro': patch --- -Fixes the terminal HMR path +Fixed an issue where the dev server logged the full file path on updates. From 213e1c4d91c314d6a355035017ddd9a96f11a72c Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Thu, 19 Oct 2023 14:25:29 +0200 Subject: [PATCH 6/8] Update .changeset/two-lamps-tell.md Co-authored-by: Sarah Rainsberger --- .changeset/two-lamps-tell.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/two-lamps-tell.md b/.changeset/two-lamps-tell.md index eb66f7a556ea..c79d1764b3ce 100644 --- a/.changeset/two-lamps-tell.md +++ b/.changeset/two-lamps-tell.md @@ -2,4 +2,4 @@ 'astro': patch --- -Fixed an issue where the dev server logged the full file path on updates. +Fixes an issue where the dev server logged the full file path on updates. From ec46ffafefc5f5bbaddd76283443ee89e97bd92f Mon Sep 17 00:00:00 2001 From: Florian LEFEBVRE Date: Thu, 19 Oct 2023 17:18:24 +0200 Subject: [PATCH 7/8] feat: simplify --- packages/astro/src/vite-plugin-astro/hmr.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/astro/src/vite-plugin-astro/hmr.ts b/packages/astro/src/vite-plugin-astro/hmr.ts index 174c3ed2b0a2..bbd991ce5835 100644 --- a/packages/astro/src/vite-plugin-astro/hmr.ts +++ b/packages/astro/src/vite-plugin-astro/hmr.ts @@ -91,9 +91,7 @@ export async function handleHotUpdate( // Bugfix: sometimes style URLs get normalized and end with `lang.css=` // These will cause full reloads, so filter them out here const mods = [...filtered].filter((m) => !m.url.endsWith('=')); - const file = fileURLToPath(`file:///${ctx.file}`) - .replace(fileURLToPath(config.root), '/') - .replace(/\\/g, '/'); + const file = ctx.file.replace(fileURLToPath(config.root).replace(/\\/g, '/'), '/'); // If only styles are changed, remove the component file from the update list if (isStyleOnlyChange) { From a3de19c0429108b34ef81991613787b86a216c6f Mon Sep 17 00:00:00 2001 From: Florian LEFEBVRE Date: Fri, 20 Oct 2023 09:30:39 +0200 Subject: [PATCH 8/8] feat: use slash helper --- packages/astro/src/vite-plugin-astro/hmr.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/vite-plugin-astro/hmr.ts b/packages/astro/src/vite-plugin-astro/hmr.ts index bbd991ce5835..27cc2d10fca8 100644 --- a/packages/astro/src/vite-plugin-astro/hmr.ts +++ b/packages/astro/src/vite-plugin-astro/hmr.ts @@ -1,3 +1,4 @@ +import { slash } from '@astrojs/internal-helpers/path'; import { fileURLToPath } from 'node:url'; import type { HmrContext, ModuleNode } from 'vite'; import type { AstroConfig } from '../@types/astro.js'; @@ -91,7 +92,7 @@ export async function handleHotUpdate( // Bugfix: sometimes style URLs get normalized and end with `lang.css=` // These will cause full reloads, so filter them out here const mods = [...filtered].filter((m) => !m.url.endsWith('=')); - const file = ctx.file.replace(fileURLToPath(config.root).replace(/\\/g, '/'), '/'); + const file = ctx.file.replace(slash(fileURLToPath(config.root)), '/'); // If only styles are changed, remove the component file from the update list if (isStyleOnlyChange) {