1
1
import path from 'path' ;
2
- import * as vite from 'vite' ;
3
- import type { ESBuildOptions , ResolvedConfig } from 'vite' ;
2
+ import { preprocessCSS , resolveConfig , transformWithEsbuild } from 'vite' ;
3
+ import type { ESBuildOptions , InlineConfig , ResolvedConfig } from 'vite' ;
4
4
// eslint-disable-next-line node/no-missing-import
5
5
import type { Preprocessor , PreprocessorGroup } from 'svelte/types/compiler/preprocess' ;
6
6
@@ -9,7 +9,7 @@ const supportedScriptLangs = ['ts'];
9
9
10
10
export function vitePreprocess ( opts ?: {
11
11
script ?: boolean ;
12
- style ?: boolean | vite . InlineConfig | vite . ResolvedConfig ;
12
+ style ?: boolean | InlineConfig | ResolvedConfig ;
13
13
} ) {
14
14
const preprocessor : PreprocessorGroup = { } ;
15
15
if ( opts ?. script !== false ) {
@@ -27,7 +27,7 @@ function viteScript(): { script: Preprocessor } {
27
27
async script ( { attributes, content, filename = '' } ) {
28
28
const lang = attributes . lang as string ;
29
29
if ( ! supportedScriptLangs . includes ( lang ) ) return ;
30
- const transformResult = await vite . transformWithEsbuild ( content , filename , {
30
+ const transformResult = await transformWithEsbuild ( content , filename , {
31
31
loader : lang as ESBuildOptions [ 'loader' ] ,
32
32
target : 'esnext' ,
33
33
tsconfigRaw : {
@@ -46,23 +46,23 @@ function viteScript(): { script: Preprocessor } {
46
46
} ;
47
47
}
48
48
49
- function viteStyle ( config : vite . InlineConfig | vite . ResolvedConfig = { } ) : {
49
+ function viteStyle ( config : InlineConfig | ResolvedConfig = { } ) : {
50
50
style : Preprocessor ;
51
51
} {
52
52
let transform : CssTransform ;
53
53
const style : Preprocessor = async ( { attributes, content, filename = '' } ) => {
54
54
const lang = attributes . lang as string ;
55
55
if ( ! supportedStyleLangs . includes ( lang ) ) return ;
56
56
if ( ! transform ) {
57
- let resolvedConfig : vite . ResolvedConfig ;
57
+ let resolvedConfig : ResolvedConfig ;
58
58
// @ts -expect-error special prop added if running in v-p-s
59
59
if ( style . __resolvedConfig ) {
60
60
// @ts -expect-error
61
61
resolvedConfig = style . __resolvedConfig ;
62
62
} else if ( isResolvedConfig ( config ) ) {
63
63
resolvedConfig = config ;
64
64
} else {
65
- resolvedConfig = await vite . resolveConfig (
65
+ resolvedConfig = await resolveConfig (
66
66
config ,
67
67
process . env . NODE_ENV === 'production' ? 'build' : 'serve'
68
68
) ;
@@ -89,26 +89,11 @@ function viteStyle(config: vite.InlineConfig | vite.ResolvedConfig = {}): {
89
89
type CssTransform = ( code : string , filename : string ) => Promise < { code : string ; map ?: any } > ;
90
90
91
91
function getCssTransformFn ( config : ResolvedConfig ) : CssTransform {
92
- // API is only available in Vite 3.2 and above
93
- // TODO: Remove Vite plugin hack when bump peer dep to Vite 3.2
94
- if ( vite . preprocessCSS ) {
95
- return async ( code , filename ) => {
96
- return vite . preprocessCSS ( code , filename , config ) ;
97
- } ;
98
- } else {
99
- const pluginName = 'vite:css' ;
100
- const plugin = config . plugins . find ( ( p ) => p . name === pluginName ) ;
101
- if ( ! plugin ) {
102
- throw new Error ( `failed to find plugin ${ pluginName } ` ) ;
103
- }
104
- if ( ! plugin . transform ) {
105
- throw new Error ( `plugin ${ pluginName } has no transform` ) ;
106
- }
107
- // @ts -expect-error
108
- return plugin . transform . bind ( null ) ;
109
- }
92
+ return async ( code , filename ) => {
93
+ return preprocessCSS ( code , filename , config ) ;
94
+ } ;
110
95
}
111
96
112
- function isResolvedConfig ( config : any ) : config is vite . ResolvedConfig {
97
+ function isResolvedConfig ( config : any ) : config is ResolvedConfig {
113
98
return ! ! config . inlineConfig ;
114
99
}
0 commit comments