@@ -205,7 +205,7 @@ export const cssConfigDefaults = Object.freeze({
205
205
} satisfies CSSOptions )
206
206
207
207
export type ResolvedCSSOptions = Omit < CSSOptions , 'lightningcss' > &
208
- Required < Pick < CSSOptions , 'transformer' > > & {
208
+ Required < Pick < CSSOptions , 'transformer' | 'devSourcemap' > > & {
209
209
lightningcss ?: LightningCSSOptions
210
210
}
211
211
@@ -1294,7 +1294,11 @@ async function compileCSSPreprocessors(
1294
1294
lang : PreprocessLang ,
1295
1295
code : string ,
1296
1296
workerController : PreprocessorWorkerController ,
1297
- ) : Promise < { code : string ; map ?: ExistingRawSourceMap ; deps ?: Set < string > } > {
1297
+ ) : Promise < {
1298
+ code : string
1299
+ map ?: ExistingRawSourceMap | { mappings : '' }
1300
+ deps ?: Set < string >
1301
+ } > {
1298
1302
const { config } = environment
1299
1303
const { preprocessorOptions, devSourcemap } = config . css
1300
1304
const atImportResolvers = getAtImportResolvers (
@@ -1372,7 +1376,7 @@ async function compileCSS(
1372
1376
const deps = new Set < string > ( )
1373
1377
1374
1378
// pre-processors: sass etc.
1375
- let preprocessorMap : ExistingRawSourceMap | undefined
1379
+ let preprocessorMap : ExistingRawSourceMap | { mappings : '' } | undefined
1376
1380
if ( isPreProcessor ( lang ) ) {
1377
1381
const preprocessorResult = await compileCSSPreprocessors (
1378
1382
environment ,
@@ -1595,18 +1599,58 @@ async function compilePostCSS(
1595
1599
return
1596
1600
}
1597
1601
1602
+ const result = await runPostCSS (
1603
+ id ,
1604
+ code ,
1605
+ postcssPlugins ,
1606
+ { ...postcssOptions , parser : postcssParser } ,
1607
+ deps ,
1608
+ environment . logger ,
1609
+ devSourcemap ,
1610
+ )
1611
+ return { ...result , modules }
1612
+ }
1613
+
1614
+ async function transformSugarSS (
1615
+ environment : PartialEnvironment ,
1616
+ id : string ,
1617
+ code : string ,
1618
+ ) {
1619
+ const { config } = environment
1620
+ const { devSourcemap } = config . css
1621
+
1622
+ const result = await runPostCSS (
1623
+ id ,
1624
+ code ,
1625
+ [ ] ,
1626
+ { parser : loadSss ( config . root ) } ,
1627
+ undefined ,
1628
+ environment . logger ,
1629
+ devSourcemap ,
1630
+ )
1631
+ return result
1632
+ }
1633
+
1634
+ async function runPostCSS (
1635
+ id : string ,
1636
+ code : string ,
1637
+ plugins : PostCSS . AcceptedPlugin [ ] ,
1638
+ options : PostCSS . ProcessOptions ,
1639
+ deps : Set < string > | undefined ,
1640
+ logger : Logger ,
1641
+ enableSourcemap : boolean ,
1642
+ ) {
1598
1643
let postcssResult : PostCSS . Result
1599
1644
try {
1600
1645
const source = removeDirectQuery ( id )
1601
1646
const postcss = await importPostcss ( )
1602
1647
1603
1648
// postcss is an unbundled dep and should be lazy imported
1604
- postcssResult = await postcss . default ( postcssPlugins ) . process ( code , {
1605
- ...postcssOptions ,
1606
- parser : postcssParser ,
1649
+ postcssResult = await postcss . default ( plugins ) . process ( code , {
1650
+ ...options ,
1607
1651
to : source ,
1608
1652
from : source ,
1609
- ...( devSourcemap
1653
+ ...( enableSourcemap
1610
1654
? {
1611
1655
map : {
1612
1656
inline : false ,
@@ -1624,7 +1668,7 @@ async function compilePostCSS(
1624
1668
// record CSS dependencies from @imports
1625
1669
for ( const message of postcssResult . messages ) {
1626
1670
if ( message . type === 'dependency' ) {
1627
- deps . add ( normalizePath ( message . file as string ) )
1671
+ deps ? .add ( normalizePath ( message . file as string ) )
1628
1672
} else if ( message . type === 'dir-dependency' ) {
1629
1673
// https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#3-dependencies
1630
1674
const { dir, glob : globPattern = '**' } = message
@@ -1635,7 +1679,7 @@ async function compilePostCSS(
1635
1679
ignore : [ '**/node_modules/**' ] ,
1636
1680
} )
1637
1681
for ( let i = 0 ; i < files . length ; i ++ ) {
1638
- deps . add ( files [ i ] )
1682
+ deps ? .add ( files [ i ] )
1639
1683
}
1640
1684
} else if ( message . type === 'warning' ) {
1641
1685
const warning = message as PostCSS . Warning
@@ -1653,7 +1697,7 @@ async function compilePostCSS(
1653
1697
}
1654
1698
: undefined ,
1655
1699
) } `
1656
- environment . logger . warn ( colors . yellow ( msg ) )
1700
+ logger . warn ( colors . yellow ( msg ) )
1657
1701
}
1658
1702
}
1659
1703
} catch ( e ) {
@@ -1667,96 +1711,10 @@ async function compilePostCSS(
1667
1711
throw e
1668
1712
}
1669
1713
1670
- if ( ! devSourcemap ) {
1671
- return {
1672
- code : postcssResult . css ,
1673
- map : { mappings : '' } ,
1674
- modules,
1675
- }
1676
- }
1677
-
1678
- const rawPostcssMap = postcssResult . map . toJSON ( )
1679
- const postcssMap = await formatPostcssSourceMap (
1680
- // version property of rawPostcssMap is declared as string
1681
- // but actually it is a number
1682
- rawPostcssMap as Omit < RawSourceMap , 'version' > as ExistingRawSourceMap ,
1683
- cleanUrl ( id ) ,
1684
- )
1685
-
1686
- return {
1687
- code : postcssResult . css ,
1688
- map : postcssMap ,
1689
- modules,
1690
- }
1691
- }
1692
-
1693
- // TODO: dedupe
1694
- async function transformSugarSS (
1695
- environment : PartialEnvironment ,
1696
- id : string ,
1697
- code : string ,
1698
- ) {
1699
- const { config } = environment
1700
- const { devSourcemap } = config . css
1701
-
1702
- let postcssResult : PostCSS . Result
1703
- try {
1704
- const source = removeDirectQuery ( id )
1705
- const postcss = await importPostcss ( )
1706
- // postcss is an unbundled dep and should be lazy imported
1707
- postcssResult = await postcss . default ( ) . process ( code , {
1708
- parser : loadSss ( config . root ) ,
1709
- to : source ,
1710
- from : source ,
1711
- ...( devSourcemap
1712
- ? {
1713
- map : {
1714
- inline : false ,
1715
- annotation : false ,
1716
- // postcss may return virtual files
1717
- // we cannot obtain content of them, so this needs to be enabled
1718
- sourcesContent : true ,
1719
- // when "prev: preprocessorMap", the result map may include duplicate filename in `postcssResult.map.sources`
1720
- // prev: preprocessorMap,
1721
- } ,
1722
- }
1723
- : { } ) ,
1724
- } )
1725
-
1726
- for ( const message of postcssResult . messages ) {
1727
- if ( message . type === 'warning' ) {
1728
- const warning = message as PostCSS . Warning
1729
- let msg = `[vite:css] ${ warning . text } `
1730
- msg += `\n${ generateCodeFrame (
1731
- code ,
1732
- {
1733
- line : warning . line ,
1734
- column : warning . column - 1 , // 1-based
1735
- } ,
1736
- warning . endLine !== undefined && warning . endColumn !== undefined
1737
- ? {
1738
- line : warning . endLine ,
1739
- column : warning . endColumn - 1 , // 1-based
1740
- }
1741
- : undefined ,
1742
- ) } `
1743
- environment . logger . warn ( colors . yellow ( msg ) )
1744
- }
1745
- }
1746
- } catch ( e ) {
1747
- e . message = `[postcss] ${ e . message } `
1748
- e . code = code
1749
- e . loc = {
1750
- file : e . file ,
1751
- line : e . line ,
1752
- column : e . column - 1 , // 1-based
1753
- }
1754
- throw e
1755
- }
1756
-
1757
- if ( ! devSourcemap ) {
1714
+ if ( ! enableSourcemap ) {
1758
1715
return {
1759
1716
code : postcssResult . css ,
1717
+ map : { mappings : '' as const } ,
1760
1718
}
1761
1719
}
1762
1720
@@ -1865,17 +1823,21 @@ export async function formatPostcssSourceMap(
1865
1823
1866
1824
function combineSourcemapsIfExists (
1867
1825
filename : string ,
1868
- map1 : ExistingRawSourceMap | undefined ,
1869
- map2 : ExistingRawSourceMap | undefined ,
1870
- ) : ExistingRawSourceMap | undefined {
1871
- return map1 && map2
1872
- ? ( combineSourcemaps ( filename , [
1873
- // type of version property of ExistingRawSourceMap is number
1874
- // but it is always 3
1875
- map1 as RawSourceMap ,
1876
- map2 as RawSourceMap ,
1877
- ] ) as ExistingRawSourceMap )
1878
- : map1
1826
+ map1 : ExistingRawSourceMap | { mappings : '' } | undefined ,
1827
+ map2 : ExistingRawSourceMap | { mappings : '' } | undefined ,
1828
+ ) : ExistingRawSourceMap | { mappings : '' } | undefined {
1829
+ if ( ! map1 || ! map2 ) {
1830
+ return map1
1831
+ }
1832
+ if ( map1 . mappings === '' || map2 . mappings === '' ) {
1833
+ return { mappings : '' }
1834
+ }
1835
+ return combineSourcemaps ( filename , [
1836
+ // type of version property of ExistingRawSourceMap is number
1837
+ // but it is always 3
1838
+ map1 as RawSourceMap ,
1839
+ map2 as RawSourceMap ,
1840
+ ] ) as ExistingRawSourceMap
1879
1841
}
1880
1842
1881
1843
const viteHashUpdateMarker = '/*$vite$:1*/'
0 commit comments