11import process from 'node:process' ;
2- import { isCSSRequest , preprocessCSS , resolveConfig , transformWithEsbuild } from 'vite' ;
2+ import * as vite from 'vite' ;
33import { 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 } }
0 commit comments