11import 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' ;
44// eslint-disable-next-line node/no-missing-import
55import type { Preprocessor , PreprocessorGroup } from 'svelte/types/compiler/preprocess' ;
66
@@ -9,7 +9,7 @@ const supportedScriptLangs = ['ts'];
99
1010export function vitePreprocess ( opts ?: {
1111 script ?: boolean ;
12- style ?: boolean | vite . InlineConfig | vite . ResolvedConfig ;
12+ style ?: boolean | InlineConfig | ResolvedConfig ;
1313} ) {
1414 const preprocessor : PreprocessorGroup = { } ;
1515 if ( opts ?. script !== false ) {
@@ -27,7 +27,7 @@ function viteScript(): { script: Preprocessor } {
2727 async script ( { attributes, content, filename = '' } ) {
2828 const lang = attributes . lang as string ;
2929 if ( ! supportedScriptLangs . includes ( lang ) ) return ;
30- const transformResult = await vite . transformWithEsbuild ( content , filename , {
30+ const transformResult = await transformWithEsbuild ( content , filename , {
3131 loader : lang as ESBuildOptions [ 'loader' ] ,
3232 target : 'esnext' ,
3333 tsconfigRaw : {
@@ -46,23 +46,23 @@ function viteScript(): { script: Preprocessor } {
4646 } ;
4747}
4848
49- function viteStyle ( config : vite . InlineConfig | vite . ResolvedConfig = { } ) : {
49+ function viteStyle ( config : InlineConfig | ResolvedConfig = { } ) : {
5050 style : Preprocessor ;
5151} {
5252 let transform : CssTransform ;
5353 const style : Preprocessor = async ( { attributes, content, filename = '' } ) => {
5454 const lang = attributes . lang as string ;
5555 if ( ! supportedStyleLangs . includes ( lang ) ) return ;
5656 if ( ! transform ) {
57- let resolvedConfig : vite . ResolvedConfig ;
57+ let resolvedConfig : ResolvedConfig ;
5858 // @ts -expect-error special prop added if running in v-p-s
5959 if ( style . __resolvedConfig ) {
6060 // @ts -expect-error
6161 resolvedConfig = style . __resolvedConfig ;
6262 } else if ( isResolvedConfig ( config ) ) {
6363 resolvedConfig = config ;
6464 } else {
65- resolvedConfig = await vite . resolveConfig (
65+ resolvedConfig = await resolveConfig (
6666 config ,
6767 process . env . NODE_ENV === 'production' ? 'build' : 'serve'
6868 ) ;
@@ -89,26 +89,11 @@ function viteStyle(config: vite.InlineConfig | vite.ResolvedConfig = {}): {
8989type CssTransform = ( code : string , filename : string ) => Promise < { code : string ; map ?: any } > ;
9090
9191function 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+ } ;
11095}
11196
112- function isResolvedConfig ( config : any ) : config is vite . ResolvedConfig {
97+ function isResolvedConfig ( config : any ) : config is ResolvedConfig {
11398 return ! ! config . inlineConfig ;
11499}
0 commit comments