Skip to content

Commit 2851b0a

Browse files
authoredJun 12, 2024··
fix(astro): ignore query params when matching .astro extension (#11240)
* fix: ignore query params when matching .astro extension * Changeset * Add test
1 parent a8c7cec commit 2851b0a

File tree

7 files changed

+102
-1
lines changed

7 files changed

+102
-1
lines changed
 

‎.changeset/pink-experts-count.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Ignores query strings in module identifiers when matching ".astro" file extensions in Vite plugin

‎packages/astro/src/vite-plugin-astro/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export default function astro({ settings, logger }: AstroPluginOptions): vite.Pl
202202
async transform(source, id) {
203203
const parsedId = parseAstroRequest(id);
204204
// ignore astro file sub-requests, e.g. Foo.astro?astro&type=script&index=0&lang.ts
205-
if (!id.endsWith('.astro') || parsedId.query.astro) {
205+
if (!parsedId.filename.endsWith('.astro') || parsedId.query.astro) {
206206
return;
207207
}
208208

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import assert from 'node:assert/strict';
2+
import { before, describe, it } from 'node:test';
3+
import * as cheerio from 'cheerio';
4+
import { loadFixture } from './test-utils.js';
5+
6+
describe('Matching .astro modules', () => {
7+
let fixture;
8+
/** @type {string} */
9+
let output;
10+
11+
before(async () => {
12+
fixture = await loadFixture({
13+
root: './fixtures/extension-matching/',
14+
});
15+
await fixture.build();
16+
output = await fixture.readFile('./index.html');
17+
});
18+
19+
it('loads virtual modules with .astro in query string', async () => {
20+
const $ = cheerio.load(output);
21+
const title = $('h1').text();
22+
assert.strictEqual(title, 'true');
23+
});
24+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { defineConfig } from 'astro/config';
2+
3+
const MODULE_ID = 'virtual:test';
4+
const RESOLVED_MODULE_ID = '\0virtual:test';
5+
6+
export default defineConfig({
7+
integrations: [
8+
{
9+
name: 'astro-test-invalid-transform',
10+
hooks: {
11+
'astro:config:setup': ({ updateConfig }) => {
12+
updateConfig({
13+
vite: {
14+
plugins: [
15+
// -----------------------------------
16+
{
17+
name: 'vite-test-invalid-transform',
18+
resolveId(id) {
19+
if (id === MODULE_ID) {
20+
// Astro tries to transform this import because the query params can end with '.astro'
21+
return `${RESOLVED_MODULE_ID}?importer=index.astro`;
22+
}
23+
},
24+
load(id) {
25+
if (id.startsWith(RESOLVED_MODULE_ID)) {
26+
return `export default 'true';`;
27+
}
28+
},
29+
},
30+
// -----------------------------------
31+
],
32+
},
33+
});
34+
},
35+
},
36+
},
37+
],
38+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@test/extension-matching",
3+
"type": "module",
4+
"version": "0.0.1",
5+
"private": true,
6+
"scripts": {
7+
"dev": "astro dev",
8+
"start": "astro dev",
9+
"build": "astro build",
10+
"preview": "astro preview",
11+
"astro": "astro"
12+
},
13+
"dependencies": {
14+
"astro": "workspace:*"
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
let success = 'false'
3+
4+
try {
5+
success = (await import('virtual:test')).default
6+
} catch (e) {
7+
console.error('Failed to load virtual module:', e)
8+
}
9+
10+
console.log('Loaded virtual module:', success)
11+
---
12+
<h1>{String(success)}</h1>

‎pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.