0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-17 22:44:24 -05:00

fix(astro): ignore query params when matching .astro extension (#11240)

* fix: ignore query params when matching .astro extension

* Changeset

* Add test
This commit is contained in:
Matt Kane 2024-06-12 12:44:44 +01:00 committed by GitHub
parent a8c7ceca43
commit 2851b0aa2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 102 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Ignores query strings in module identifiers when matching ".astro" file extensions in Vite plugin

View file

@ -202,7 +202,7 @@ export default function astro({ settings, logger }: AstroPluginOptions): vite.Pl
async transform(source, id) { async transform(source, id) {
const parsedId = parseAstroRequest(id); const parsedId = parseAstroRequest(id);
// ignore astro file sub-requests, e.g. Foo.astro?astro&type=script&index=0&lang.ts // 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; return;
} }

View file

@ -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');
});
});

View file

@ -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';`;
}
},
},
// -----------------------------------
],
},
});
},
},
},
],
});

View file

@ -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:*"
}
}

View file

@ -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>

6
pnpm-lock.yaml generated
View file

@ -2946,6 +2946,12 @@ importers:
specifier: workspace:* specifier: workspace:*
version: link:../../.. version: link:../../..
packages/astro/test/fixtures/extension-matching:
dependencies:
astro:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/fetch: packages/astro/test/fixtures/fetch:
dependencies: dependencies:
'@astrojs/preact': '@astrojs/preact':