Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support ?inline query on svelte style virtual modules #1024

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-chefs-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': minor
---

Support `?inline` query on Svelte style virtual modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
button.svelte-d8vj6a {
color: #000099;
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,11 @@ describe.runIf(!isBuild)('ssrLoadModule', () => {
const result = await ssrLoadDummy('?raw&svelte&type=style');
await expect(result).toMatchFileSnapshot(snapshotFilename('ssr-style'));
});
test('?inline&svelte&type=style&lang.css', async () => {
// Preload Dummy.svelte first so its CSS is processed in the module graph, otherwise loading
// its css inlined url directly will return the raw svelte file rather than the style
await ssrLoadDummy('');
const result = await ssrLoadDummy('?inline&svelte&type=style&lang.css');
await expect(result).toMatchFileSnapshot(snapshotFilename('ssr-inline-style'));
});
});
28 changes: 25 additions & 3 deletions packages/e2e-tests/kit-node/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { sveltekit } from '@sveltejs/kit/vite';
import { transformValidation, writeResolvedConfig } from 'e2e-test-dep-vite-plugins';

/** @type {import('vite').UserConfig} */
const config = {
export default {
server: {
watch: {
// During tests we edit the files too fast and sometimes chokidar
Expand All @@ -15,11 +15,33 @@ const config = {
minify: false,
sourcemap: true // must be true for hermetic build test!
},
plugins: [transformValidation(), sveltekit(), writeResolvedConfig()],
plugins: [
transformValidation(),
sveltekit(),
writeResolvedConfig(),
workaroundInlineSvelteCssIssue()
],
optimizeDeps: {
// eagerly include these, otherwise vite optimizer might interfere with restarting while the test is running
include: ['svelte-i18n', 'e2e-test-dep-svelte-api-only']
}
};

export default config;
/**
* Workaround until https://github.com/sveltejs/kit/pull/13007 is merged
* @returns {import('vite').Plugin}
*/
function workaroundInlineSvelteCssIssue() {
return {
name: 'workaround-inline-svelte-css-issue',
enforce: 'pre',
resolveId(id) {
// SvelteKit relies on a previous behaviour in v-p-s where it strips out the inline
// query to get the CSS result, however this no longer works in Vite 6 and should be
// fixed in SvelteKit instead, otherwise FOUC will happen in dev.
if (id.includes('?svelte')) {
return id.replace(/&inline=$/, '');
}
}
};
}
6 changes: 5 additions & 1 deletion packages/vite-plugin-svelte/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ export function svelte(inlineOptions) {
const ssr = !!opts?.ssr;
const svelteRequest = requestParser(importee, ssr);
if (svelteRequest?.query.svelte) {
if (svelteRequest.query.type === 'style' && !svelteRequest.raw) {
if (
svelteRequest.query.type === 'style' &&
!svelteRequest.raw &&
!svelteRequest.query.inline
) {
// return cssId with root prefix so postcss pipeline of vite finds the directory correctly
// see https://github.com/sveltejs/vite-plugin-svelte/issues/14
log.debug(
Expand Down
1 change: 1 addition & 0 deletions packages/vite-plugin-svelte/src/types/id.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface RequestQuery {
url?: boolean;
raw?: boolean;
direct?: boolean;
inline?: boolean;
}

export interface SvelteRequest {
Expand Down