Fix linked Astro library style HMR (#5460)

* Fix linked Astro library style HMR

* Update comment

* Try fix windows
This commit is contained in:
Bjorn Lu 2022-11-30 23:58:09 +08:00 committed by GitHub
parent 299ae9bb6a
commit 57888e0690
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 81 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix linked Astro library style HMR

View file

@ -1,6 +1,5 @@
import { expect } from '@playwright/test';
import os from 'os';
import { testFactory } from './test-utils.js';
import { getColor, testFactory } from './test-utils.js';
const test = testFactory({ root: './fixtures/astro-component/' });
@ -79,4 +78,32 @@ test.describe('Astro component HMR', () => {
await updatedLog;
});
test('update linked dep Astro html', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));
let h1 = page.locator('#astro-linked-lib');
expect(await h1.textContent()).toBe('astro-linked-lib');
await Promise.all([
page.waitForLoadState('networkidle'),
await astro.editFile('../_deps/astro-linked-lib/Component.astro', (content) =>
content.replace('>astro-linked-lib<', '>astro-linked-lib-update<')
),
]);
h1 = page.locator('#astro-linked-lib');
expect(await h1.textContent()).toBe('astro-linked-lib-update');
});
test('update linked dep Astro style', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));
let h1 = page.locator('#astro-linked-lib');
expect(await getColor(h1)).toBe('rgb(255, 0, 0)');
await Promise.all([
page.waitForLoadState('networkidle'),
await astro.editFile('../_deps/astro-linked-lib/Component.astro', (content) =>
content.replace('color: red', 'color: green')
),
]);
h1 = page.locator('#astro-linked-lib');
expect(await getColor(h1)).toBe('rgb(0, 128, 0)');
});
});

View file

@ -0,0 +1,9 @@
<!-- NOTE: this dep is in `_deps` to test HMR with the `/@fs` id -->
<h1 id="astro-linked-lib">astro-linked-lib</h1>
<style>
h1 {
color: red;
}
</style>

View file

@ -0,0 +1,13 @@
{
"name": "@e2e/astro-linked-lib",
"version": "0.0.0",
"private": true,
"exports": {
".": {
"astro": "./Component.astro"
}
},
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"@astrojs/preact": "^1.1.0",
"@e2e/astro-linked-lib": "link:../_deps/astro-linked-lib",
"astro": "workspace:*",
"preact": "^10.11.0"
}

View file

@ -1,5 +1,6 @@
---
import Hero from '../components/Hero.astro';
import LinkedLib from '@e2e/astro-linked-lib'
---
<html>
@ -11,6 +12,7 @@ import Hero from '../components/Hero.astro';
<Hero title="Astro Components">
Lorem ipsum, dolor sit amet consectetur adipisicing elit.
</Hero>
<LinkedLib />
</main>
</body>
</html>

View file

@ -9,7 +9,12 @@ import esbuild from 'esbuild';
import slash from 'slash';
import { fileURLToPath } from 'url';
import { cachedCompilation, CompileProps, getCachedSource } from '../core/compile/index.js';
import { isRelativePath, prependForwardSlash, startsWithForwardSlash } from '../core/path.js';
import {
isRelativePath,
prependForwardSlash,
removeLeadingForwardSlashWindows,
startsWithForwardSlash,
} from '../core/path.js';
import { viteID } from '../core/util.js';
import { getFileInfo } from '../vite-plugin-utils/index.js';
import { handleHotUpdate } from './hmr.js';
@ -80,12 +85,20 @@ export default function astro({ settings, logging }: AstroPluginOptions): vite.P
// serve sub-part requests (*?astro) as virtual modules
const { query } = parseAstroRequest(id);
if (query.astro) {
// TODO: Try to remove these custom resolve so HMR is more predictable.
// Convert /src/pages/index.astro?astro&type=style to /Users/name/
// Because this needs to be the id for the Vite CSS plugin to property resolve
// relative @imports.
if (query.type === 'style' && isBrowserPath(id)) {
return relativeToRoot(id);
}
// Strip `/@fs` from linked dependencies outside of root so we can normalize
// it in the condition below. This ensures that the style module shared the same is
// part of the same "file" as the main Astro module in the module graph.
// "file" refers to `moduleGraph.fileToModulesMap`.
if (query.type === 'style' && id.startsWith('/@fs')) {
id = removeLeadingForwardSlashWindows(id.slice(4));
}
// Convert file paths to ViteID, meaning on Windows it omits the leading slash
if (isFullFilePath(id)) {
return viteID(new URL('file://' + id));

View file

@ -606,13 +606,21 @@ importers:
chai-as-promised: 7.1.1_chai@4.3.7
mocha: 9.2.2
packages/astro/e2e/fixtures/_deps/astro-linked-lib:
specifiers:
astro: workspace:*
dependencies:
astro: link:../../../..
packages/astro/e2e/fixtures/astro-component:
specifiers:
'@astrojs/preact': ^1.1.0
'@e2e/astro-linked-lib': link:../_deps/astro-linked-lib
astro: workspace:*
preact: ^10.11.0
dependencies:
'@astrojs/preact': link:../../../../integrations/preact
'@e2e/astro-linked-lib': link:../_deps/astro-linked-lib
astro: link:../../..
preact: 10.11.3