Skip to content

Commit 973ba75

Browse files
authored
feat: support ?inline query on svelte style virtual modules (#1024)
* feat: support `?inline` query on svelte style virtual modules * chore: add workaround * refactor: use query.inline
1 parent ec158f8 commit 973ba75

File tree

6 files changed

+46
-4
lines changed

6 files changed

+46
-4
lines changed

.changeset/sixty-chefs-relate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
Support `?inline` query on Svelte style virtual modules
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
button.svelte-d8vj6a {
2+
color: #000099;
3+
}

packages/e2e-tests/import-queries/__tests__/import-queries.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,11 @@ describe.runIf(!isBuild)('ssrLoadModule', () => {
162162
const result = await ssrLoadDummy('?raw&svelte&type=style');
163163
await expect(result).toMatchFileSnapshot(snapshotFilename('ssr-style'));
164164
});
165+
test('?inline&svelte&type=style&lang.css', async () => {
166+
// Preload Dummy.svelte first so its CSS is processed in the module graph, otherwise loading
167+
// its css inlined url directly will return the raw svelte file rather than the style
168+
await ssrLoadDummy('');
169+
const result = await ssrLoadDummy('?inline&svelte&type=style&lang.css');
170+
await expect(result).toMatchFileSnapshot(snapshotFilename('ssr-inline-style'));
171+
});
165172
});

packages/e2e-tests/kit-node/vite.config.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { sveltekit } from '@sveltejs/kit/vite';
22
import { transformValidation, writeResolvedConfig } from 'e2e-test-dep-vite-plugins';
33

44
/** @type {import('vite').UserConfig} */
5-
const config = {
5+
export default {
66
server: {
77
watch: {
88
// During tests we edit the files too fast and sometimes chokidar
@@ -15,11 +15,33 @@ const config = {
1515
minify: false,
1616
sourcemap: true // must be true for hermetic build test!
1717
},
18-
plugins: [transformValidation(), sveltekit(), writeResolvedConfig()],
18+
plugins: [
19+
transformValidation(),
20+
sveltekit(),
21+
writeResolvedConfig(),
22+
workaroundInlineSvelteCssIssue()
23+
],
1924
optimizeDeps: {
2025
// eagerly include these, otherwise vite optimizer might interfere with restarting while the test is running
2126
include: ['svelte-i18n', 'e2e-test-dep-svelte-api-only']
2227
}
2328
};
2429

25-
export default config;
30+
/**
31+
* Workaround until https://github.com/sveltejs/kit/pull/13007 is merged
32+
* @returns {import('vite').Plugin}
33+
*/
34+
function workaroundInlineSvelteCssIssue() {
35+
return {
36+
name: 'workaround-inline-svelte-css-issue',
37+
enforce: 'pre',
38+
resolveId(id) {
39+
// SvelteKit relies on a previous behaviour in v-p-s where it strips out the inline
40+
// query to get the CSS result, however this no longer works in Vite 6 and should be
41+
// fixed in SvelteKit instead, otherwise FOUC will happen in dev.
42+
if (id.includes('?svelte')) {
43+
return id.replace(/&inline=$/, '');
44+
}
45+
}
46+
};
47+
}

packages/vite-plugin-svelte/src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ export function svelte(inlineOptions) {
136136
const ssr = !!opts?.ssr;
137137
const svelteRequest = requestParser(importee, ssr);
138138
if (svelteRequest?.query.svelte) {
139-
if (svelteRequest.query.type === 'style' && !svelteRequest.raw) {
139+
if (
140+
svelteRequest.query.type === 'style' &&
141+
!svelteRequest.raw &&
142+
!svelteRequest.query.inline
143+
) {
140144
// return cssId with root prefix so postcss pipeline of vite finds the directory correctly
141145
// see https://github.com/sveltejs/vite-plugin-svelte/issues/14
142146
log.debug(

packages/vite-plugin-svelte/src/types/id.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface RequestQuery {
1515
url?: boolean;
1616
raw?: boolean;
1717
direct?: boolean;
18+
inline?: boolean;
1819
}
1920

2021
export interface SvelteRequest {

0 commit comments

Comments
 (0)