@@ -2102,69 +2102,70 @@ async function minifyCSS(
21022102 // regular CSS assets do end with a linebreak.
21032103 // See https://github.com/vitejs/vite/pull/13893#issuecomment-1678628198
21042104
2105- if ( config . build . cssMinify === 'lightningcss ' ) {
2105+ if ( config . build . cssMinify === 'esbuild ' ) {
21062106 try {
2107- const { code, warnings } = ( await importLightningCSS ( ) ) . transform ( {
2108- ...config . css . lightningcss ,
2109- targets : convertTargets ( config . build . cssTarget ) ,
2110- cssModules : undefined ,
2111- // TODO: Pass actual filename here, which can also be passed to esbuild's
2112- // `sourcefile` option below to improve error messages
2113- filename : defaultCssBundleName ,
2114- code : Buffer . from ( css ) ,
2115- minify : true ,
2107+ const { code, warnings } = await transform ( css , {
2108+ loader : 'css' ,
2109+ target : config . build . cssTarget || undefined ,
2110+ ...resolveMinifyCssEsbuildOptions ( config . esbuild || { } ) ,
21162111 } )
21172112 if ( warnings . length ) {
2118- const messages = warnings . map (
2119- ( warning ) =>
2120- `${ warning . message } \n` +
2121- generateCodeFrame ( css , {
2122- line : warning . loc . line ,
2123- column : warning . loc . column - 1 , // 1-based
2124- } ) ,
2125- )
2113+ const msgs = await formatMessages ( warnings , { kind : 'warning' } )
21262114 config . logger . warn (
2127- colors . yellow ( `warnings when minifying css:\n${ messages . join ( '\n' ) } ` ) ,
2115+ colors . yellow ( `warnings when minifying css:\n${ msgs . join ( '\n' ) } ` ) ,
21282116 )
21292117 }
2130-
2131- // NodeJS res.code = Buffer
2132- // Deno res.code = Uint8Array
2133- // For correct decode compiled css need to use TextDecoder
2134- // LightningCSS output does not return a linebreak at the end
2135- return decoder . decode ( code ) + ( inlined ? '' : '\n' )
2118+ // esbuild output does return a linebreak at the end
2119+ return inlined ? code . trimEnd ( ) : code
21362120 } catch ( e ) {
2137- e . message = `[lightningcss minify] ${ e . message } `
2138- if ( e . loc ) {
2139- e . loc = {
2140- line : e . loc . line ,
2141- column : e . loc . column - 1 , // 1-based
2142- }
2143- e . frame = generateCodeFrame ( css , e . loc )
2121+ if ( e . errors ) {
2122+ e . message = '[esbuild css minify] ' + e . message
2123+ const msgs = await formatMessages ( e . errors , { kind : 'error' } )
2124+ e . frame = '\n' + msgs . join ( '\n' )
2125+ e . loc = e . errors [ 0 ] . location
21442126 }
21452127 throw e
21462128 }
21472129 }
2130+
21482131 try {
2149- const { code, warnings } = await transform ( css , {
2150- loader : 'css' ,
2151- target : config . build . cssTarget || undefined ,
2152- ...resolveMinifyCssEsbuildOptions ( config . esbuild || { } ) ,
2132+ const { code, warnings } = ( await importLightningCSS ( ) ) . transform ( {
2133+ ...config . css . lightningcss ,
2134+ targets : convertTargets ( config . build . cssTarget ) ,
2135+ cssModules : undefined ,
2136+ // TODO: Pass actual filename here, which can also be passed to esbuild's
2137+ // `sourcefile` option below to improve error messages
2138+ filename : defaultCssBundleName ,
2139+ code : Buffer . from ( css ) ,
2140+ minify : true ,
21532141 } )
21542142 if ( warnings . length ) {
2155- const msgs = await formatMessages ( warnings , { kind : 'warning' } )
2143+ const messages = warnings . map (
2144+ ( warning ) =>
2145+ `${ warning . message } \n` +
2146+ generateCodeFrame ( css , {
2147+ line : warning . loc . line ,
2148+ column : warning . loc . column - 1 , // 1-based
2149+ } ) ,
2150+ )
21562151 config . logger . warn (
2157- colors . yellow ( `warnings when minifying css:\n${ msgs . join ( '\n' ) } ` ) ,
2152+ colors . yellow ( `warnings when minifying css:\n${ messages . join ( '\n' ) } ` ) ,
21582153 )
21592154 }
2160- // esbuild output does return a linebreak at the end
2161- return inlined ? code . trimEnd ( ) : code
2155+
2156+ // NodeJS res.code = Buffer
2157+ // Deno res.code = Uint8Array
2158+ // For correct decode compiled css need to use TextDecoder
2159+ // LightningCSS output does not return a linebreak at the end
2160+ return decoder . decode ( code ) + ( inlined ? '' : '\n' )
21622161 } catch ( e ) {
2163- if ( e . errors ) {
2164- e . message = '[esbuild css minify] ' + e . message
2165- const msgs = await formatMessages ( e . errors , { kind : 'error' } )
2166- e . frame = '\n' + msgs . join ( '\n' )
2167- e . loc = e . errors [ 0 ] . location
2162+ e . message = `[lightningcss minify] ${ e . message } `
2163+ if ( e . loc ) {
2164+ e . loc = {
2165+ line : e . loc . line ,
2166+ column : e . loc . column - 1 , // 1-based
2167+ }
2168+ e . frame = generateCodeFrame ( css , e . loc )
21682169 }
21692170 throw e
21702171 }
0 commit comments