diff options
author | 2022-05-24 11:34:08 -0400 | |
---|---|---|
committer | 2022-05-24 11:34:08 -0400 | |
commit | 463a1c214779e0558bcd99c30294e1a14a97232e (patch) | |
tree | 0e751042419c2320a45336251f1817adea913764 | |
parent | af179c0d896e143d4c72868675fa3e7cc566dfcd (diff) | |
download | astro-463a1c214779e0558bcd99c30294e1a14a97232e.tar.gz astro-463a1c214779e0558bcd99c30294e1a14a97232e.tar.zst astro-463a1c214779e0558bcd99c30294e1a14a97232e.zip |
Fix: Relative imports inside of hosited script on windows (#3423)
* Failing test
* Add some debugging
* Normalize filename to remove @fs prefix
* Adds a changeset
* Break it again
* Fix it
* Update assertion
Diffstat (limited to '')
5 files changed, 17 insertions, 4 deletions
diff --git a/.changeset/friendly-ways-collect.md b/.changeset/friendly-ways-collect.md new file mode 100644 index 000000000..8d65c2b0b --- /dev/null +++ b/.changeset/friendly-ways-collect.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix relative inline scripts on Windows diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts index 404e21476..790262249 100644 --- a/packages/astro/src/vite-plugin-astro/index.ts +++ b/packages/astro/src/vite-plugin-astro/index.ts @@ -62,8 +62,9 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu // If resolving from an astro subresource such as a hoisted script, // we need to resolve relative paths ourselves. if (from) { - const { query: fromQuery, filename } = parseAstroRequest(from); - if (fromQuery.astro && isRelativePath(id) && fromQuery.type === 'script') { + const parsedFrom = parseAstroRequest(from); + if (parsedFrom.query.astro && isRelativePath(id) && parsedFrom.query.type === 'script') { + const filename = normalizeFilename(parsedFrom.filename); const resolvedURL = new URL(id, `file://${filename}`); const resolved = resolvedURL.pathname; if (isBrowserPath(resolved)) { diff --git a/packages/astro/test/astro-scripts.test.js b/packages/astro/test/astro-scripts.test.js index 28c33fc51..bda290243 100644 --- a/packages/astro/test/astro-scripts.test.js +++ b/packages/astro/test/astro-scripts.test.js @@ -1,6 +1,5 @@ import { expect } from 'chai'; import * as cheerio from 'cheerio'; -import path from 'path'; import { loadFixture } from './test-utils.js'; describe('Scripts (hoisted and not)', () => { @@ -50,6 +49,9 @@ describe('Scripts (hoisted and not)', () => { // test 3: the JS exists expect(inlineEntryJS).to.be.ok; + + // test 4: Inline imported JS is included + expect(inlineEntryJS).to.contain("I AM IMPORTED INLINE", "The inline imported JS is included in the bundle"); }); it('External page builds the hoisted scripts to a single bundle', async () => { diff --git a/packages/astro/test/fixtures/astro-scripts/src/pages/inline.astro b/packages/astro/test/fixtures/astro-scripts/src/pages/inline.astro index e3de6198a..03026e659 100644 --- a/packages/astro/test/fixtures/astro-scripts/src/pages/inline.astro +++ b/packages/astro/test/fixtures/astro-scripts/src/pages/inline.astro @@ -12,5 +12,9 @@ import Inline from '../components/Inline.astro'; <Inline /> <Inline /> <Inline /> + <script> + // This must be relative for the test + import '../scripts/hoist_external_imported_inline.js'; + </script> </body> -</html>
\ No newline at end of file +</html> diff --git a/packages/astro/test/fixtures/astro-scripts/src/scripts/hoist_external_imported_inline.js b/packages/astro/test/fixtures/astro-scripts/src/scripts/hoist_external_imported_inline.js new file mode 100644 index 000000000..cc4e11ae8 --- /dev/null +++ b/packages/astro/test/fixtures/astro-scripts/src/scripts/hoist_external_imported_inline.js @@ -0,0 +1 @@ +console.log("I AM IMPORTED INLINE"); |