summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ben Holmes <hey@bholmes.dev> 2022-09-09 10:53:34 -0400
committerGravatar GitHub <noreply@github.com> 2022-09-09 10:53:34 -0400
commitf27ca6ab3edbf0ef55e213ffd09aac454ce07995 (patch)
tree3d667a12ec7c6ba4a7258ad5fd014bf09b130196
parentd136864bff1409031d68677564312eac78073e80 (diff)
downloadastro-f27ca6ab3edbf0ef55e213ffd09aac454ce07995.tar.gz
astro-f27ca6ab3edbf0ef55e213ffd09aac454ce07995.tar.zst
astro-f27ca6ab3edbf0ef55e213ffd09aac454ce07995.zip
Fix: Windows client-side script reloads on dev server (#4645)
* fix: append forward slash to script paths for "C:/" prob * chore: remove dead regex * chore: changeset * test: add client script test back to windows! * test: add inline script test for sanity * The actual fix Co-authored-by: Matthew Phillips <matthew@skypack.dev>
-rw-r--r--.changeset/two-cherries-drop.md5
-rw-r--r--packages/astro/e2e/astro-component.test.js27
-rw-r--r--packages/astro/e2e/fixtures/astro-component/src/pages/index.astro4
-rw-r--r--packages/astro/src/core/util.ts2
-rw-r--r--packages/astro/src/vite-plugin-astro/index.ts19
5 files changed, 40 insertions, 17 deletions
diff --git a/.changeset/two-cherries-drop.md b/.changeset/two-cherries-drop.md
new file mode 100644
index 000000000..c1f7fc6b6
--- /dev/null
+++ b/.changeset/two-cherries-drop.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fix client-side scripts reloads on dev server in windows
diff --git a/packages/astro/e2e/astro-component.test.js b/packages/astro/e2e/astro-component.test.js
index 8d6151f07..8ebbcd3a6 100644
--- a/packages/astro/e2e/astro-component.test.js
+++ b/packages/astro/e2e/astro-component.test.js
@@ -36,10 +36,7 @@ test.describe('Astro component HMR', () => {
);
});
- // TODO: Re-enable this test on windows when #3424 is fixed
- // https://github.com/withastro/astro/issues/3424
- const it = os.platform() === 'win32' ? test.skip : test;
- it('hoisted scripts', async ({ page, astro }) => {
+ test('hoisted scripts', async ({ page, astro }) => {
const initialLog = page.waitForEvent(
'console',
(message) => message.text() === 'Hello, Astro!'
@@ -60,4 +57,26 @@ test.describe('Astro component HMR', () => {
await updatedLog;
});
+
+ test('inline scripts', async ({ page, astro }) => {
+ const initialLog = page.waitForEvent(
+ 'console',
+ (message) => message.text() === 'Hello, inline Astro!'
+ );
+
+ await page.goto(astro.resolveUrl('/'));
+ await initialLog;
+
+ const updatedLog = page.waitForEvent(
+ 'console',
+ (message) => message.text() === 'Hello, updated inline Astro!'
+ );
+
+ // Edit the inline script on the page
+ await astro.editFile('./src/pages/index.astro', (content) =>
+ content.replace('Hello, inline Astro!', 'Hello, updated inline Astro!')
+ );
+
+ await updatedLog;
+ });
});
diff --git a/packages/astro/e2e/fixtures/astro-component/src/pages/index.astro b/packages/astro/e2e/fixtures/astro-component/src/pages/index.astro
index 76221b040..c7522dc54 100644
--- a/packages/astro/e2e/fixtures/astro-component/src/pages/index.astro
+++ b/packages/astro/e2e/fixtures/astro-component/src/pages/index.astro
@@ -18,3 +18,7 @@ import Hero from '../components/Hero.astro';
<script>
console.log('Hello, Astro!');
</script>
+
+<script is:inline>
+ console.log('Hello, inline Astro!');
+</script>
diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts
index 7fb5a4f5b..0abc9b40b 100644
--- a/packages/astro/src/core/util.ts
+++ b/packages/astro/src/core/util.ts
@@ -126,7 +126,7 @@ export function resolveDependency(dep: string, projectRoot: URL) {
* Windows: C:/Users/astro/code/my-project/src/pages/index.astro
*/
export function viteID(filePath: URL): string {
- return slash(fileURLToPath(filePath));
+ return slash(fileURLToPath(filePath) + filePath.search);
}
export const VALID_ID_PREFIX = `/@id/`;
diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts
index 3b98b6bee..b132afe1b 100644
--- a/packages/astro/src/vite-plugin-astro/index.ts
+++ b/packages/astro/src/vite-plugin-astro/index.ts
@@ -9,8 +9,9 @@ import ancestor from 'common-ancestor-path';
import esbuild from 'esbuild';
import slash from 'slash';
import { fileURLToPath } from 'url';
+import { isRelativePath, prependForwardSlash, startsWithForwardSlash } from '../core/path.js';
+import { viteID } from '../core/util.js';
import { cachedCompilation, CompileProps, getCachedSource } from '../core/compile/index.js';
-import { isRelativePath, startsWithForwardSlash } from '../core/path.js';
import { getFileInfo } from '../vite-plugin-utils/index.js';
import {
createTransformStyles,
@@ -48,6 +49,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
// Variables for determining if an id starts with /src...
const srcRootWeb = config.srcDir.pathname.slice(config.root.pathname.length - 1);
const isBrowserPath = (path: string) => path.startsWith(srcRootWeb);
+ const isFullFilePath = (path: string) => path.startsWith(prependForwardSlash(slash(fileURLToPath(config.root))));
function resolveRelativeFromAstroParent(id: string, parsedFrom: ParsedRequestResult): string {
const filename = normalizeFilename(parsedFrom.filename);
@@ -94,7 +96,10 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
if (query.type === 'style' && isBrowserPath(id)) {
return relativeToRoot(id);
}
-
+ // Convert file paths to ViteID, meaning on Windows it omits the leading slash
+ if(isFullFilePath(id)) {
+ return viteID(new URL('file://' + id));
+ }
return id;
}
},
@@ -245,18 +250,8 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
)};export { $$file as file, $$url as url };\n`;
// Add HMR handling in dev mode.
if (!resolvedConfig.isProduction) {
- // HACK: extract dependencies from metadata until compiler static extraction handles them
- const metadata = transformResult.code.split('$$createMetadata(')[1].split('});\n')[0];
- const pattern = /specifier:\s*'([^']*)'/g;
- const deps = new Set();
- let match;
- while ((match = pattern.exec(metadata)?.[1])) {
- deps.add(match);
- }
-
let i = 0;
while (i < transformResult.scripts.length) {
- deps.add(`${id}?astro&type=script&index=${i}&lang.ts`);
SUFFIX += `import "${id}?astro&type=script&index=${i}&lang.ts";`;
i++;
}