1
1
import process from 'node:process' ;
2
- import { isCSSRequest , preprocessCSS , resolveConfig , transformWithEsbuild } from 'vite' ;
2
+ import * as vite from 'vite' ;
3
3
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 ;
5
13
/**
6
14
* @typedef {(code: string, filename: string) => Promise<{ code: string; map?: any; deps?: Set<string> }> } CssTransform
7
15
*/
@@ -18,7 +26,7 @@ export function vitePreprocess(opts) {
18
26
/** @type {import('svelte/compiler').PreprocessorGroup } */
19
27
const preprocessor = { name : 'vite-preprocess' } ;
20
28
if ( opts ?. script === true ) {
21
- preprocessor . script = viteScript ( ) . script ;
29
+ preprocessor . script = rolldownVersion ? viteScriptOxc ( ) . script : viteScript ( ) . script ;
22
30
}
23
31
if ( opts ?. style !== false ) {
24
32
const styleOpts = typeof opts ?. style == 'object' ? opts ?. style : undefined ;
@@ -57,6 +65,37 @@ function viteScript() {
57
65
} ;
58
66
}
59
67
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
+
60
99
/**
61
100
* @param {import('vite').ResolvedConfig | import('vite').InlineConfig } config
62
101
* @returns {{ style: import('svelte/compiler').Preprocessor } }
0 commit comments