Fix hoisted scripts path for linked package Astro components (#6872)

This commit is contained in:
Bjorn Lu 2023-04-21 10:42:23 +02:00 committed by GitHub
parent 8cc53090ca
commit b6154d2d57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix hoisted scripts path for linked package Astro components

View file

@ -44,6 +44,9 @@ test.describe('Astro component HMR', () => {
await page.goto(astro.resolveUrl('/'));
await initialLog;
const el = page.locator('#hoisted-script');
expect(await el.innerText()).toContain('Hoisted success');
const updatedLog = page.waitForEvent(
'console',
(message) => message.text() === 'Hello, updated Astro!'

View file

@ -0,0 +1,5 @@
<div id="hoisted-script"></div>
<script>
document.getElementById('hoisted-script').innerHTML = 'Hoisted success';
</script>

View file

@ -5,6 +5,9 @@
"exports": {
".": {
"astro": "./Component.astro"
},
"./HoistedScript": {
"astro": "./HoistedScript.astro"
}
},
"dependencies": {

View file

@ -1,6 +1,7 @@
---
import Hero from '../components/Hero.astro';
import LinkedLib from '@e2e/astro-linked-lib'
import HoistedScript from '@e2e/astro-linked-lib/HoistedScript'
---
<html>
@ -13,6 +14,7 @@ import LinkedLib from '@e2e/astro-linked-lib'
Lorem ipsum, dolor sit amet consectetur adipisicing elit.
</Hero>
<LinkedLib />
<HoistedScript />
</main>
</body>
</html>

View file

@ -158,7 +158,11 @@ export function rootRelativePath(root: URL, idOrUrl: URL | string) {
} else {
id = idOrUrl;
}
return prependForwardSlash(id.slice(normalizePath(fileURLToPath(root)).length));
const normalizedRoot = normalizePath(fileURLToPath(root));
if (id.startsWith(normalizedRoot)) {
id = id.slice(normalizedRoot.length);
}
return prependForwardSlash(id);
}
export function emoji(char: string, fallback: string) {