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 page.goto(astro.resolveUrl('/'));
await initialLog; await initialLog;
const el = page.locator('#hoisted-script');
expect(await el.innerText()).toContain('Hoisted success');
const updatedLog = page.waitForEvent( const updatedLog = page.waitForEvent(
'console', 'console',
(message) => message.text() === 'Hello, updated Astro!' (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": { "exports": {
".": { ".": {
"astro": "./Component.astro" "astro": "./Component.astro"
},
"./HoistedScript": {
"astro": "./HoistedScript.astro"
} }
}, },
"dependencies": { "dependencies": {

View file

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

View file

@ -158,7 +158,11 @@ export function rootRelativePath(root: URL, idOrUrl: URL | string) {
} else { } else {
id = idOrUrl; 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) { export function emoji(char: string, fallback: string) {