@@ -1275,7 +1275,6 @@ async function compileCSS(
1275
1275
) : Promise < {
1276
1276
code : string
1277
1277
map ?: SourceMapInput
1278
- ast ?: PostCSS . Result
1279
1278
modules ?: Record < string , string >
1280
1279
deps ?: Set < string >
1281
1280
} > {
@@ -1284,51 +1283,49 @@ async function compileCSS(
1284
1283
return compileLightningCSS ( id , code , environment , urlResolver )
1285
1284
}
1286
1285
1286
+ const lang = CSS_LANGS_RE . exec ( id ) ?. [ 1 ] as CssLang | undefined
1287
+ const deps = new Set < string > ( )
1288
+
1289
+ // pre-processors: sass etc.
1290
+ let preprocessorMap : ExistingRawSourceMap | undefined
1291
+ if ( isPreProcessor ( lang ) ) {
1292
+ const preprocessorResult = await compileCSSPreprocessors (
1293
+ environment ,
1294
+ id ,
1295
+ lang ,
1296
+ code ,
1297
+ workerController ,
1298
+ )
1299
+ code = preprocessorResult . code
1300
+ preprocessorMap = preprocessorResult . map
1301
+ preprocessorResult . deps ?. forEach ( ( dep ) => deps . add ( dep ) )
1302
+ }
1303
+
1287
1304
const { modules : modulesOptions , devSourcemap } = config . css
1288
1305
const isModule = modulesOptions !== false && cssModuleRE . test ( id )
1289
1306
// although at serve time it can work without processing, we do need to
1290
1307
// crawl them in order to register watch dependencies.
1291
1308
const needInlineImport = code . includes ( '@import' )
1292
1309
const hasUrl = cssUrlRE . test ( code ) || cssImageSetRE . test ( code )
1293
- const lang = CSS_LANGS_RE . exec ( id ) ?. [ 1 ] as CssLang | undefined
1294
1310
const postcssConfig = await resolvePostcssConfig (
1295
1311
environment . getTopLevelConfig ( ) ,
1296
1312
)
1297
1313
1298
- // 1. plain css that needs no processing
1314
+ // postcss processing is not needed
1299
1315
if (
1300
- lang === 'css ' &&
1316
+ lang !== 'sss ' &&
1301
1317
! postcssConfig &&
1302
1318
! isModule &&
1303
1319
! needInlineImport &&
1304
1320
! hasUrl
1305
1321
) {
1306
- return { code, map : null }
1307
- }
1308
-
1309
- let modules : Record < string , string > | undefined
1310
- const deps = new Set < string > ( )
1311
-
1312
- // 2. pre-processors: sass etc.
1313
- let preprocessorMap : ExistingRawSourceMap | undefined
1314
- if ( isPreProcessor ( lang ) ) {
1315
- const preprocessorResult = await compileCSSPreprocessors (
1316
- environment ,
1317
- id ,
1318
- lang ,
1319
- code ,
1320
- workerController ,
1321
- )
1322
- code = preprocessorResult . code
1323
- preprocessorMap = preprocessorResult . map
1324
- preprocessorResult . deps ?. forEach ( ( dep ) => deps . add ( dep ) )
1322
+ return { code, map : preprocessorMap ?? null , deps }
1325
1323
}
1326
1324
1327
- // 3. postcss
1325
+ // postcss
1328
1326
const atImportResolvers = getAtImportResolvers (
1329
1327
environment . getTopLevelConfig ( ) ,
1330
1328
)
1331
- const postcssOptions = postcssConfig ?. options ?? { }
1332
1329
const postcssPlugins = postcssConfig ?. plugins . slice ( ) ?? [ ]
1333
1330
1334
1331
if ( needInlineImport ) {
@@ -1390,7 +1387,13 @@ async function compileCSS(
1390
1387
)
1391
1388
}
1392
1389
1393
- if ( urlResolver ) {
1390
+ if (
1391
+ urlResolver &&
1392
+ // if there's an @import, we need to add this plugin
1393
+ // regradless of whether it contains url() or image-set(),
1394
+ // because we don't know the content referenced by @import
1395
+ ( needInlineImport || hasUrl )
1396
+ ) {
1394
1397
postcssPlugins . push (
1395
1398
UrlRewritePostcssPlugin ( {
1396
1399
resolver : urlResolver ,
@@ -1400,6 +1403,8 @@ async function compileCSS(
1400
1403
)
1401
1404
}
1402
1405
1406
+ let modules : Record < string , string > | undefined
1407
+
1403
1408
if ( isModule ) {
1404
1409
postcssPlugins . unshift (
1405
1410
( await importPostcssModules ( ) ) . default ( {
@@ -1433,7 +1438,11 @@ async function compileCSS(
1433
1438
)
1434
1439
}
1435
1440
1436
- if ( ! postcssPlugins . length ) {
1441
+ const postcssOptions = postcssConfig ?. options ?? { }
1442
+ const postcssParser =
1443
+ lang === 'sss' ? loadSss ( config . root ) : postcssOptions . parser
1444
+
1445
+ if ( ! postcssPlugins . length && ! postcssParser ) {
1437
1446
return {
1438
1447
code,
1439
1448
map : preprocessorMap ,
@@ -1445,10 +1454,11 @@ async function compileCSS(
1445
1454
try {
1446
1455
const source = removeDirectQuery ( id )
1447
1456
const postcss = await importPostcss ( )
1457
+
1448
1458
// postcss is an unbundled dep and should be lazy imported
1449
1459
postcssResult = await postcss . default ( postcssPlugins ) . process ( code , {
1450
1460
...postcssOptions ,
1451
- parser : lang === 'sss' ? loadSss ( config . root ) : postcssOptions . parser ,
1461
+ parser : postcssParser ,
1452
1462
to : source ,
1453
1463
from : source ,
1454
1464
...( devSourcemap
@@ -1514,7 +1524,6 @@ async function compileCSS(
1514
1524
1515
1525
if ( ! devSourcemap ) {
1516
1526
return {
1517
- ast : postcssResult ,
1518
1527
code : postcssResult . css ,
1519
1528
map : { mappings : '' } ,
1520
1529
modules,
@@ -1532,7 +1541,6 @@ async function compileCSS(
1532
1541
)
1533
1542
1534
1543
return {
1535
- ast : postcssResult ,
1536
1544
code : postcssResult . css ,
1537
1545
map : combineSourcemapsIfExists ( cleanUrl ( id ) , postcssMap , preprocessorMap ) ,
1538
1546
modules,
@@ -2182,8 +2190,8 @@ function loadSassPackage(root: string): {
2182
2190
}
2183
2191
}
2184
2192
2185
- let cachedSss : any
2186
- function loadSss ( root : string ) {
2193
+ let cachedSss : PostCSS . Syntax
2194
+ function loadSss ( root : string ) : PostCSS . Syntax {
2187
2195
if ( cachedSss ) return cachedSss
2188
2196
2189
2197
const sssPath = loadPreprocessorPath ( PostCssDialectLang . sss , root )
0 commit comments