summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/hungry-houses-arrive.md5
-rw-r--r--packages/astro/e2e/astro-component.test.js3
-rw-r--r--packages/astro/e2e/fixtures/_deps/astro-linked-lib/HoistedScript.astro5
-rw-r--r--packages/astro/e2e/fixtures/_deps/astro-linked-lib/package.json3
-rw-r--r--packages/astro/e2e/fixtures/astro-component/src/pages/index.astro2
-rw-r--r--packages/astro/src/core/util.ts6
6 files changed, 23 insertions, 1 deletions
diff --git a/.changeset/hungry-houses-arrive.md b/.changeset/hungry-houses-arrive.md
new file mode 100644
index 000000000..f04d78b78
--- /dev/null
+++ b/.changeset/hungry-houses-arrive.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fix hoisted scripts path for linked package Astro components
diff --git a/packages/astro/e2e/astro-component.test.js b/packages/astro/e2e/astro-component.test.js
index ac5531fbe..7308ea292 100644
--- a/packages/astro/e2e/astro-component.test.js
+++ b/packages/astro/e2e/astro-component.test.js
@@ -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!'
diff --git a/packages/astro/e2e/fixtures/_deps/astro-linked-lib/HoistedScript.astro b/packages/astro/e2e/fixtures/_deps/astro-linked-lib/HoistedScript.astro
new file mode 100644
index 000000000..90d390bdf
--- /dev/null
+++ b/packages/astro/e2e/fixtures/_deps/astro-linked-lib/HoistedScript.astro
@@ -0,0 +1,5 @@
+<div id="hoisted-script"></div>
+
+<script>
+document.getElementById('hoisted-script').innerHTML = 'Hoisted success';
+</script>
diff --git a/packages/astro/e2e/fixtures/_deps/astro-linked-lib/package.json b/packages/astro/e2e/fixtures/_deps/astro-linked-lib/package.json
index 55578c8ae..0c74adc2d 100644
--- a/packages/astro/e2e/fixtures/_deps/astro-linked-lib/package.json
+++ b/packages/astro/e2e/fixtures/_deps/astro-linked-lib/package.json
@@ -5,6 +5,9 @@
"exports": {
".": {
"astro": "./Component.astro"
+ },
+ "./HoistedScript": {
+ "astro": "./HoistedScript.astro"
}
},
"dependencies": {
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 527f33a74..3ada093e7 100644
--- a/packages/astro/e2e/fixtures/astro-component/src/pages/index.astro
+++ b/packages/astro/e2e/fixtures/astro-component/src/pages/index.astro
@@ -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>
diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts
index ddc20bff5..5d8868115 100644
--- a/packages/astro/src/core/util.ts
+++ b/packages/astro/src/core/util.ts
@@ -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) {