Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include astro components for HMR invalidation #4240

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nasty-gifts-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Properly invalidate Astro modules when a child script updates in HMR
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@e2e/invalidate-script-deps",
"version": "0.0.0",
"private": true,
"devDependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<title>Test</title>
</head>
<body>
<h1></h1>
<script>
import '../scripts/heading.js';
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

document.querySelector('h1').textContent = 'before';
31 changes: 31 additions & 0 deletions packages/astro/e2e/invalidate-script-deps.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect } from '@playwright/test';
import { testFactory } from './test-utils.js';

const test = testFactory({
root: './fixtures/invalidate-script-deps/',
});

let devServer;

test.beforeEach(async ({ astro }) => {
devServer = await astro.startDevServer();
});

test.afterEach(async () => {
await devServer.stop();
});

test.describe('Scripts with dependencies', () => {
test('refresh with HMR', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));

const h = page.locator('h1');
await expect(h, 'original text set').toHaveText('before');

await astro.editFile('./src/scripts/heading.js', (original) =>
original.replace('before', 'after')
);

await expect(h, 'text changed').toHaveText('after');
});
});
17 changes: 15 additions & 2 deletions packages/astro/src/vite-plugin-astro/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { LogOptions } from '../core/logger/core.js';
import { info } from '../core/logger/core.js';
import * as msg from '../core/messages.js';
import { cachedCompilation, invalidateCompilation, isCached } from './compile.js';
import { isAstroScript } from './query.js';

interface TrackCSSDependenciesOptions {
viteDevServer: ViteDevServer | null;
Expand Down Expand Up @@ -59,11 +60,12 @@ export interface HandleHotUpdateOptions {
config: AstroConfig;
logging: LogOptions;
compile: () => ReturnType<typeof cachedCompilation>;
viteServer: ViteDevServer;
}

export async function handleHotUpdate(
ctx: HmrContext,
{ config, logging, compile }: HandleHotUpdateOptions
{ config, logging, compile, viteServer }: HandleHotUpdateOptions
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
) {
let isStyleOnlyChange = false;
if (ctx.file.endsWith('.astro')) {
Expand Down Expand Up @@ -141,12 +143,23 @@ export async function handleHotUpdate(
// Add hoisted scripts so these get invalidated
for (const mod of mods) {
for (const imp of mod.importedModules) {
if (imp.id?.includes('?astro&type=script')) {
if (imp.id && isAstroScript(imp.id)) {
mods.push(imp);
}
}
}

// If this is a module that is imported from a <script>, invalidate the Astro
// component so that it is cached by the time the script gets transformed.
for(const mod of filtered) {
if(mod.id && isAstroScript(mod.id) && mod.file) {
const astroMod = viteServer.moduleGraph.getModuleById(mod.file);
if(astroMod) {
mods.unshift(astroMod);
}
}
}

// TODO: Svelte files should be marked as `isSelfAccepting` but they don't appear to be
const isSelfAccepting = mods.every((m) => m.isSelfAccepting || m.url.endsWith('.svelte'));
if (isSelfAccepting) {
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 @@ -359,7 +359,12 @@ ${source}
pluginContext: this,
};
const compile = () => cachedCompilation(compileProps);
return handleHotUpdate.call(this, context, { config, logging, compile });
return handleHotUpdate.call(this, context, {
config,
logging,
compile,
viteServer: context.server
});
},
};
}
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/src/vite-plugin-astro/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ export function parseAstroRequest(id: string): ParsedRequestResult {
query,
};
}

export function isAstroScript(id: string): boolean {
const parsed = parseAstroRequest(id);
return parsed.query.type === 'script';
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.