diff options
7 files changed, 68 insertions, 3 deletions
diff --git a/.changeset/kind-baboons-melt.md b/.changeset/kind-baboons-melt.md new file mode 100644 index 000000000..018148358 --- /dev/null +++ b/.changeset/kind-baboons-melt.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix <script type="module"> resolution diff --git a/packages/astro/src/build.ts b/packages/astro/src/build.ts index ef26c5a5c..1f11a6acf 100644 --- a/packages/astro/src/build.ts +++ b/packages/astro/src/build.ts @@ -110,6 +110,9 @@ export async function build(astroConfig: AstroConfig, logging: LogOptions = defa scanPromises.push( runtime.load(url).then((result) => { if (result.statusCode !== 200) { + if (result.statusCode === 404) { + throw new Error(`${buildState[id].srcPath.href}: could not find "${path.basename(url)}"`); + } // there shouldn’t be a build error here throw (result as any).error || new Error(`unexpected status ${result.statusCode} when loading ${url}`); } diff --git a/packages/astro/src/compiler/transform/module-scripts.ts b/packages/astro/src/compiler/transform/module-scripts.ts index 46defb1bd..98385b105 100644 --- a/packages/astro/src/compiler/transform/module-scripts.ts +++ b/packages/astro/src/compiler/transform/module-scripts.ts @@ -24,8 +24,23 @@ export default function ({ compileOptions, filename }: { compileOptions: Compile } let src = getAttrValue(node.attributes, 'src'); - if (!src || !src.startsWith('.')) { + + // scenario 1: if missing "src", ignore + if (!src) { + return; + } + + // scenario 2: if absolute path, ignore + if (src.startsWith('/')) { + return; + } + + // scenario 3: if remote URL, ignore + try { + new URL(src); // if this succeeds, this is a complete, valid URL return; + } catch (err) { + // do nothing } const srcUrl = new URL(src, fileUrl); diff --git a/packages/astro/test/astro-resolve.test.js b/packages/astro/test/astro-resolve.test.js index ea9a307e6..d98573120 100644 --- a/packages/astro/test/astro-resolve.test.js +++ b/packages/astro/test/astro-resolve.test.js @@ -1,5 +1,6 @@ import { suite } from 'uvu'; import * as assert from 'uvu/assert'; +import { doc } from './test-utils.js'; import { setupBuild } from './helpers.js'; const Resolution = suite('Astro Resolution'); @@ -9,11 +10,30 @@ setupBuild(Resolution, './fixtures/astro-resolve'); Resolution('Assets', async (context) => { await context.build(); - // public/ asset resolved + // test 1: public/ asset resolved assert.ok(await context.readFile('/svg.svg')); - // asset in src/pages resolved (and didn’t overwrite /svg.svg) + // test 2: asset in src/pages resolved (and didn’t overwrite /svg.svg) assert.ok(await context.readFile('/_astro/src/pages/svg.svg')); }); +Resolution('<script type="module">', async (context) => { + await context.build(); + + // public/ asset resolved + const $ = doc(await context.readFile('/scripts/index.html')); + + // test 1: not `type="module"` left alone + assert.equal($('script[src="./relative.js"]').attr('type'), undefined); + + // test 2: inline script left alone + assert.equal($('script:not([type]):not([src])').length, 1); + + // test 3: relative script resolved + assert.equal($('script[type="module"][src="/_astro/src/pages/relative.js"]').length, 2); // we have 2 of these! + + // test 4: absolute script left alone + assert.equal($('script[type="module"][src="/absolute.js"]').length, 1); +}); + Resolution.run(); diff --git a/packages/astro/test/fixtures/astro-resolve/public/absolute.js b/packages/astro/test/fixtures/astro-resolve/public/absolute.js new file mode 100644 index 000000000..9892d29e5 --- /dev/null +++ b/packages/astro/test/fixtures/astro-resolve/public/absolute.js @@ -0,0 +1 @@ +console.log('I’m absolute'); diff --git a/packages/astro/test/fixtures/astro-resolve/src/pages/relative.js b/packages/astro/test/fixtures/astro-resolve/src/pages/relative.js new file mode 100644 index 000000000..c264d183c --- /dev/null +++ b/packages/astro/test/fixtures/astro-resolve/src/pages/relative.js @@ -0,0 +1 @@ +console.log('I’m relative'); diff --git a/packages/astro/test/fixtures/astro-resolve/src/pages/scripts.astro b/packages/astro/test/fixtures/astro-resolve/src/pages/scripts.astro new file mode 100644 index 000000000..603fe6b91 --- /dev/null +++ b/packages/astro/test/fixtures/astro-resolve/src/pages/scripts.astro @@ -0,0 +1,20 @@ +<html> + <head></head> + + <body> + <!-- not `type="module"` --> + <script src="./relative.js"></script> + + <!-- no `src` --> + <script></script> + + <!-- type="module" with NO "./" --> + <script type="module" src="relative.js"></script> + + <!-- type="module" WITH leading "./" --> + <script type="module" src="./relative.js"></script> + + <!-- absolute URL --> + <script type="module" src="/absolute.js"></script> + </body> +</html> |