summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matt Kane <m@mk.gg> 2024-06-12 12:44:44 +0100
committerGravatar GitHub <noreply@github.com> 2024-06-12 12:44:44 +0100
commit2851b0aa2e2abe80ea603b53c67770e94980a8d3 (patch)
treeeb510950ba425a51d176db566986a34d98698e01
parenta8c7ceca430ff8050b9f81a08e17fdb158723bc7 (diff)
downloadastro-2851b0aa2e2abe80ea603b53c67770e94980a8d3.tar.gz
astro-2851b0aa2e2abe80ea603b53c67770e94980a8d3.tar.zst
astro-2851b0aa2e2abe80ea603b53c67770e94980a8d3.zip
fix(astro): ignore query params when matching .astro extension (#11240)
* fix: ignore query params when matching .astro extension * Changeset * Add test
-rw-r--r--.changeset/pink-experts-count.md5
-rw-r--r--packages/astro/src/vite-plugin-astro/index.ts2
-rw-r--r--packages/astro/test/extension-matching.test.js24
-rw-r--r--packages/astro/test/fixtures/extension-matching/astro.config.mjs38
-rw-r--r--packages/astro/test/fixtures/extension-matching/package.json16
-rw-r--r--packages/astro/test/fixtures/extension-matching/src/pages/index.astro12
-rw-r--r--pnpm-lock.yaml6
7 files changed, 102 insertions, 1 deletions
diff --git a/.changeset/pink-experts-count.md b/.changeset/pink-experts-count.md
new file mode 100644
index 000000000..3ebbfc6b7
--- /dev/null
+++ b/.changeset/pink-experts-count.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Ignores query strings in module identifiers when matching ".astro" file extensions in Vite plugin
diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts
index 9d6367b99..6c0f76b0f 100644
--- a/packages/astro/src/vite-plugin-astro/index.ts
+++ b/packages/astro/src/vite-plugin-astro/index.ts
@@ -202,7 +202,7 @@ export default function astro({ settings, logger }: AstroPluginOptions): vite.Pl
async transform(source, id) {
const parsedId = parseAstroRequest(id);
// ignore astro file sub-requests, e.g. Foo.astro?astro&type=script&index=0&lang.ts
- if (!id.endsWith('.astro') || parsedId.query.astro) {
+ if (!parsedId.filename.endsWith('.astro') || parsedId.query.astro) {
return;
}
diff --git a/packages/astro/test/extension-matching.test.js b/packages/astro/test/extension-matching.test.js
new file mode 100644
index 000000000..41d2e46e6
--- /dev/null
+++ b/packages/astro/test/extension-matching.test.js
@@ -0,0 +1,24 @@
+import assert from 'node:assert/strict';
+import { before, describe, it } from 'node:test';
+import * as cheerio from 'cheerio';
+import { loadFixture } from './test-utils.js';
+
+describe('Matching .astro modules', () => {
+ let fixture;
+ /** @type {string} */
+ let output;
+
+ before(async () => {
+ fixture = await loadFixture({
+ root: './fixtures/extension-matching/',
+ });
+ await fixture.build();
+ output = await fixture.readFile('./index.html');
+ });
+
+ it('loads virtual modules with .astro in query string', async () => {
+ const $ = cheerio.load(output);
+ const title = $('h1').text();
+ assert.strictEqual(title, 'true');
+ });
+});
diff --git a/packages/astro/test/fixtures/extension-matching/astro.config.mjs b/packages/astro/test/fixtures/extension-matching/astro.config.mjs
new file mode 100644
index 000000000..6083ccdcd
--- /dev/null
+++ b/packages/astro/test/fixtures/extension-matching/astro.config.mjs
@@ -0,0 +1,38 @@
+import { defineConfig } from 'astro/config';
+
+const MODULE_ID = 'virtual:test';
+const RESOLVED_MODULE_ID = '\0virtual:test';
+
+export default defineConfig({
+ integrations: [
+ {
+ name: 'astro-test-invalid-transform',
+ hooks: {
+ 'astro:config:setup': ({ updateConfig }) => {
+ updateConfig({
+ vite: {
+ plugins: [
+ // -----------------------------------
+ {
+ name: 'vite-test-invalid-transform',
+ resolveId(id) {
+ if (id === MODULE_ID) {
+ // Astro tries to transform this import because the query params can end with '.astro'
+ return `${RESOLVED_MODULE_ID}?importer=index.astro`;
+ }
+ },
+ load(id) {
+ if (id.startsWith(RESOLVED_MODULE_ID)) {
+ return `export default 'true';`;
+ }
+ },
+ },
+ // -----------------------------------
+ ],
+ },
+ });
+ },
+ },
+ },
+ ],
+});
diff --git a/packages/astro/test/fixtures/extension-matching/package.json b/packages/astro/test/fixtures/extension-matching/package.json
new file mode 100644
index 000000000..41e09a8c1
--- /dev/null
+++ b/packages/astro/test/fixtures/extension-matching/package.json
@@ -0,0 +1,16 @@
+{
+ "name": "@test/extension-matching",
+ "type": "module",
+ "version": "0.0.1",
+ "private": true,
+ "scripts": {
+ "dev": "astro dev",
+ "start": "astro dev",
+ "build": "astro build",
+ "preview": "astro preview",
+ "astro": "astro"
+ },
+ "dependencies": {
+ "astro": "workspace:*"
+ }
+}
diff --git a/packages/astro/test/fixtures/extension-matching/src/pages/index.astro b/packages/astro/test/fixtures/extension-matching/src/pages/index.astro
new file mode 100644
index 000000000..78299b13d
--- /dev/null
+++ b/packages/astro/test/fixtures/extension-matching/src/pages/index.astro
@@ -0,0 +1,12 @@
+---
+let success = 'false'
+
+try {
+ success = (await import('virtual:test')).default
+} catch (e) {
+ console.error('Failed to load virtual module:', e)
+}
+
+console.log('Loaded virtual module:', success)
+---
+<h1>{String(success)}</h1>
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 439dfd842..a47a30e5b 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -2946,6 +2946,12 @@ importers:
specifier: workspace:*
version: link:../../..
+ packages/astro/test/fixtures/extension-matching:
+ dependencies:
+ astro:
+ specifier: workspace:*
+ version: link:../../..
+
packages/astro/test/fixtures/fetch:
dependencies:
'@astrojs/preact':