@@ -33,6 +33,10 @@ import type {
33
33
SassModernPreprocessBaseOptions ,
34
34
StylusPreprocessorBaseOptions ,
35
35
} from 'types/internal/cssPreprocessorOptions'
36
+ import type {
37
+ TransformAttributeResult as LightningCssTransformAttributeResult ,
38
+ TransformResult as LightningCssTransformResult ,
39
+ } from 'lightningcss'
36
40
import { getCodeWithSourcemap , injectSourcesContent } from '../server/sourcemap'
37
41
import type { EnvironmentModuleNode } from '../server/moduleGraph'
38
42
import {
@@ -3114,60 +3118,71 @@ async function compileLightningCSS(
3114
3118
const toAbsolute = ( filePath : string ) =>
3115
3119
path . isAbsolute ( filePath ) ? filePath : path . join ( config . root , filePath )
3116
3120
3117
- const res = styleAttrRE . test ( id )
3118
- ? ( await importLightningCSS ( ) ) . transformStyleAttribute ( {
3119
- filename,
3120
- code : Buffer . from ( src ) ,
3121
- targets : config . css ?. lightningcss ?. targets ,
3122
- minify : config . isProduction && ! ! config . build . cssMinify ,
3123
- analyzeDependencies : true ,
3124
- } )
3125
- : await (
3126
- await importLightningCSS ( )
3127
- ) . bundleAsync ( {
3128
- ...config . css ?. lightningcss ,
3129
- filename,
3130
- resolver : {
3131
- read ( filePath ) {
3132
- if ( filePath === filename ) {
3133
- return src
3134
- }
3135
- // This happens with html-proxy (#13776)
3136
- if ( ! filePath . endsWith ( '.css' ) ) {
3137
- return src
3138
- }
3139
- return fs . readFileSync ( toAbsolute ( filePath ) , 'utf-8' )
3140
- } ,
3141
- async resolve ( id , from ) {
3142
- const publicFile = checkPublicFile (
3143
- id ,
3144
- environment . getTopLevelConfig ( ) ,
3145
- )
3146
- if ( publicFile ) {
3147
- return publicFile
3148
- }
3121
+ let res : LightningCssTransformAttributeResult | LightningCssTransformResult
3122
+ try {
3123
+ res = styleAttrRE . test ( id )
3124
+ ? ( await importLightningCSS ( ) ) . transformStyleAttribute ( {
3125
+ filename,
3126
+ code : Buffer . from ( src ) ,
3127
+ targets : config . css ?. lightningcss ?. targets ,
3128
+ minify : config . isProduction && ! ! config . build . cssMinify ,
3129
+ analyzeDependencies : true ,
3130
+ } )
3131
+ : await (
3132
+ await importLightningCSS ( )
3133
+ ) . bundleAsync ( {
3134
+ ...config . css ?. lightningcss ,
3135
+ filename,
3136
+ resolver : {
3137
+ read ( filePath ) {
3138
+ if ( filePath === filename ) {
3139
+ return src
3140
+ }
3141
+ // This happens with html-proxy (#13776)
3142
+ if ( ! filePath . endsWith ( '.css' ) ) {
3143
+ return src
3144
+ }
3145
+ return fs . readFileSync ( toAbsolute ( filePath ) , 'utf-8' )
3146
+ } ,
3147
+ async resolve ( id , from ) {
3148
+ const publicFile = checkPublicFile (
3149
+ id ,
3150
+ environment . getTopLevelConfig ( ) ,
3151
+ )
3152
+ if ( publicFile ) {
3153
+ return publicFile
3154
+ }
3149
3155
3150
- const resolved = await getAtImportResolvers (
3151
- environment . getTopLevelConfig ( ) ,
3152
- ) . css ( environment , id , toAbsolute ( from ) )
3156
+ const resolved = await getAtImportResolvers (
3157
+ environment . getTopLevelConfig ( ) ,
3158
+ ) . css ( environment , id , toAbsolute ( from ) )
3153
3159
3154
- if ( resolved ) {
3155
- deps . add ( resolved )
3156
- return resolved
3157
- }
3158
- return id
3160
+ if ( resolved ) {
3161
+ deps . add ( resolved )
3162
+ return resolved
3163
+ }
3164
+ return id
3165
+ } ,
3159
3166
} ,
3160
- } ,
3161
- minify : config . isProduction && ! ! config . build . cssMinify ,
3162
- sourceMap :
3163
- config . command === 'build'
3164
- ? ! ! config . build . sourcemap
3165
- : config . css ?. devSourcemap ,
3166
- analyzeDependencies : true ,
3167
- cssModules : cssModuleRE . test ( id )
3168
- ? ( config . css ?. lightningcss ?. cssModules ?? true )
3169
- : undefined ,
3170
- } )
3167
+ minify : config . isProduction && ! ! config . build . cssMinify ,
3168
+ sourceMap :
3169
+ config . command === 'build'
3170
+ ? ! ! config . build . sourcemap
3171
+ : config . css ?. devSourcemap ,
3172
+ analyzeDependencies : true ,
3173
+ cssModules : cssModuleRE . test ( id )
3174
+ ? ( config . css ?. lightningcss ?. cssModules ?? true )
3175
+ : undefined ,
3176
+ } )
3177
+ } catch ( e ) {
3178
+ e . message = `[lightningcss] ${ e . message } `
3179
+ e . loc = {
3180
+ file : toAbsolute ( e . fileName ) ,
3181
+ line : e . loc . line ,
3182
+ column : e . loc . column - 1 , // 1-based
3183
+ }
3184
+ throw e
3185
+ }
3171
3186
3172
3187
// NodeJS res.code = Buffer
3173
3188
// Deno res.code = Uint8Array
0 commit comments