Skip to content

Commit e708199

Browse files
committed
feat: support rolldown-vite in vitePreprocess
1 parent b74cd52 commit e708199

File tree

10 files changed

+239
-63
lines changed

10 files changed

+239
-63
lines changed

.changeset/heavy-dots-repeat.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': major
3+
---
4+
5+
add support for rolldown-vite in vitePreprocess. Using the typescript preprocessor requires a tsconfig.json with verbatimModuleSyntax enabled, eg @tsconfig/svelte

packages/e2e-tests/preprocess-with-vite/__tests__/preprocess-with-vite.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test('should render App', async () => {
77
expect(await getText('#foo-title')).toBe('Styles with stylus blub');
88
expect(await getColor('#foo-title')).toBe('magenta');
99
expect(await getColor('p.note')).toBe('rgb(255, 62, 0)');
10+
expect(await getText('#enum')).toBe('qoox');
1011
});
1112

1213
test('should not mangle code from esbuild pure annotations', async () => {

packages/e2e-tests/preprocess-with-vite/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"devDependencies": {
1111
"@sveltejs/vite-plugin-svelte": "workspace:^",
12+
"@tsconfig/svelte": "^5.0.4",
1213
"sass": "^1.89.1",
1314
"stylus": "^0.64.0",
1415
"svelte": "^5.33.18",

packages/e2e-tests/preprocess-with-vite/src/App.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
import Bar from './Bar.svelte';
44
export let hello: string;
55
const world: string = 'world'; // edit world and save to see hmr update
6+
enum Baz {
7+
Qoox = 'qoox'
8+
}
9+
let qoox = Baz.Qoox;
610
</script>
711

812
<h1 class="foo">{hello} {world}</h1>
913
<p id="app-scss">This is styled with scss using darken fn</p>
1014
<Foo />
1115
<Bar />
16+
<p id="enum">{qoox}</p>
1217

1318
<style lang="scss">
1419
@use 'sass:color';

packages/e2e-tests/preprocess-with-vite/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import App from './App.svelte';
2-
import { Hello } from './types.js';
2+
import { type Hello } from './types.ts';
33
import { mount } from 'svelte';
44

55
const hello: Hello = 'Hello';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@tsconfig/svelte"
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
22

33
export default {
4-
preprocess: [vitePreprocess()]
4+
preprocess: [vitePreprocess({ script: true })]
55
};

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import process from 'node:process';
2-
import { isCSSRequest, preprocessCSS, resolveConfig, transformWithEsbuild } from 'vite';
2+
import * as vite from 'vite';
33
import { mapToRelative, removeLangSuffix } from './utils/sourcemaps.js';
4-
4+
//@ts-expect-error rolldown types don't exist
5+
const {
6+
isCSSRequest,
7+
preprocessCSS,
8+
resolveConfig,
9+
transformWithEsbuild,
10+
rolldownVersion,
11+
transformWithOxc
12+
} = vite;
513
/**
614
* @typedef {(code: string, filename: string) => Promise<{ code: string; map?: any; deps?: Set<string> }>} CssTransform
715
*/
@@ -18,7 +26,7 @@ export function vitePreprocess(opts) {
1826
/** @type {import('svelte/compiler').PreprocessorGroup} */
1927
const preprocessor = { name: 'vite-preprocess' };
2028
if (opts?.script === true) {
21-
preprocessor.script = viteScript().script;
29+
preprocessor.script = rolldownVersion ? viteScriptOxc().script : viteScript().script;
2230
}
2331
if (opts?.style !== false) {
2432
const styleOpts = typeof opts?.style == 'object' ? opts?.style : undefined;
@@ -57,6 +65,37 @@ function viteScript() {
5765
};
5866
}
5967

68+
/**
69+
* @returns {{ script: import('svelte/compiler').Preprocessor }}
70+
*/
71+
function viteScriptOxc() {
72+
return {
73+
async script({ attributes, content, filename = '' }) {
74+
const lang = /** @type {string} */ (attributes.lang);
75+
if (!supportedScriptLangs.includes(lang)) return;
76+
const { code, map } = await transformWithOxc(content, filename, {
77+
lang,
78+
target: 'esnext'
79+
// TODO, how to pass tsconfig compilerOptions (or not needed as config is loaded for file
80+
/*tsconfigRaw: {
81+
compilerOptions: {
82+
// svelte typescript needs this flag to work with type imports
83+
importsNotUsedAsValues: 'preserve',
84+
preserveValueImports: true
85+
}
86+
}*/
87+
});
88+
89+
mapToRelative(map, filename);
90+
91+
return {
92+
code,
93+
map
94+
};
95+
}
96+
};
97+
}
98+
6099
/**
61100
* @param {import('vite').ResolvedConfig | import('vite').InlineConfig} config
62101
* @returns {{ style: import('svelte/compiler').Preprocessor }}

packages/vite-plugin-svelte/types/index.d.ts.map

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
null,
2727
null
2828
],
29-
"mappings": ";;;;aAIYA,OAAOA;;WAETC,mBAAmBA;;;;;;;;;;;kBAWZC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAgGbC,YAAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAiDnBC,mBAAmBA;;;;;;;;;;;;;;;;WAgBnBC,oBAAoBA;;;;;;;;;;;;;;;MAezBC,SAASA;;kBAEGC,qBAAqBA;;;;;;;;;;;;;iBClKtBC,MAAMA;iBCjBNC,cAAcA;iBCgBRC,gBAAgBA",
29+
"mappings": ";;;;aAIYA,OAAOA;;WAETC,mBAAmBA;;;;;;;;;;;kBAWZC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAgGbC,YAAYA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAiDnBC,mBAAmBA;;;;;;;;;;;;;;;;WAgBnBC,oBAAoBA;;;;;;;;;;;;;;;MAezBC,SAASA;;kBAEGC,qBAAqBA;;;;;;;;;;;;;iBClKtBC,MAAMA;iBCTNC,cAAcA;iBCQRC,gBAAgBA",
3030
"ignoreList": []
3131
}

0 commit comments

Comments
 (0)