@@ -62,6 +62,12 @@ export interface CSSOptions {
62
62
| ( Postcss . ProcessOptions & {
63
63
plugins ?: Postcss . Plugin [ ]
64
64
} )
65
+ /**
66
+ * Enables css sourcemaps during dev
67
+ * @default false
68
+ * @experimental
69
+ */
70
+ devSourcemap ?: boolean
65
71
}
66
72
67
73
export interface CSSModulesOptions {
@@ -309,9 +315,12 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
309
315
return `export default ${ JSON . stringify ( css ) } `
310
316
}
311
317
312
- const sourcemap = this . getCombinedSourcemap ( )
313
- await injectSourcesContent ( sourcemap , cleanUrl ( id ) , config . logger )
314
- const cssContent = getCodeWithSourcemap ( 'css' , css , sourcemap )
318
+ let cssContent = css
319
+ if ( config . css ?. devSourcemap ) {
320
+ const sourcemap = this . getCombinedSourcemap ( )
321
+ await injectSourcesContent ( sourcemap , cleanUrl ( id ) , config . logger )
322
+ cssContent = getCodeWithSourcemap ( 'css' , css , sourcemap )
323
+ }
315
324
316
325
return [
317
326
`import { updateStyle as __vite__updateStyle, removeStyle as __vite__removeStyle } from ${ JSON . stringify (
@@ -612,7 +621,11 @@ async function compileCSS(
612
621
modules ?: Record < string , string >
613
622
deps ?: Set < string >
614
623
} > {
615
- const { modules : modulesOptions , preprocessorOptions } = config . css || { }
624
+ const {
625
+ modules : modulesOptions ,
626
+ preprocessorOptions,
627
+ devSourcemap
628
+ } = config . css || { }
616
629
const isModule = modulesOptions !== false && cssModuleRE . test ( id )
617
630
// although at serve time it can work without processing, we do need to
618
631
// crawl them in order to register watch dependencies.
@@ -661,6 +674,7 @@ async function compileCSS(
661
674
}
662
675
// important: set this for relative import resolving
663
676
opts . filename = cleanUrl ( id )
677
+ opts . enableSourcemap = devSourcemap ?? false
664
678
665
679
const preprocessResult = await preProcessor (
666
680
code ,
@@ -815,6 +829,16 @@ async function compileCSS(
815
829
}
816
830
}
817
831
832
+ if ( ! devSourcemap ) {
833
+ return {
834
+ ast : postcssResult ,
835
+ code : postcssResult . css ,
836
+ map : { mappings : '' } ,
837
+ modules,
838
+ deps
839
+ }
840
+ }
841
+
818
842
const rawPostcssMap = postcssResult . map . toJSON ( )
819
843
820
844
const postcssMap = formatPostcssSourceMap (
@@ -1086,6 +1110,7 @@ type StylePreprocessorOptions = {
1086
1110
additionalData ?: PreprocessorAdditionalData
1087
1111
filename : string
1088
1112
alias : Alias [ ]
1113
+ enableSourcemap : boolean
1089
1114
}
1090
1115
1091
1116
type SassStylePreprocessorOptions = StylePreprocessorOptions & Sass . Options
@@ -1175,17 +1200,22 @@ const scss: SassStylePreprocessor = async (
1175
1200
const { content : data , map : additionalMap } = await getSource (
1176
1201
source ,
1177
1202
options . filename ,
1178
- options . additionalData
1203
+ options . additionalData ,
1204
+ options . enableSourcemap
1179
1205
)
1180
1206
const finalOptions : Sass . Options = {
1181
1207
...options ,
1182
1208
data,
1183
1209
file : options . filename ,
1184
1210
outFile : options . filename ,
1185
1211
importer,
1186
- sourceMap : true ,
1187
- omitSourceMapUrl : true ,
1188
- sourceMapRoot : path . dirname ( options . filename )
1212
+ ...( options . enableSourcemap
1213
+ ? {
1214
+ sourceMap : true ,
1215
+ omitSourceMapUrl : true ,
1216
+ sourceMapRoot : path . dirname ( options . filename )
1217
+ }
1218
+ : { } )
1189
1219
}
1190
1220
1191
1221
try {
@@ -1299,18 +1329,23 @@ const less: StylePreprocessor = async (source, root, options, resolvers) => {
1299
1329
const { content, map : additionalMap } = await getSource (
1300
1330
source ,
1301
1331
options . filename ,
1302
- options . additionalData
1332
+ options . additionalData ,
1333
+ options . enableSourcemap
1303
1334
)
1304
1335
1305
1336
let result : Less . RenderOutput | undefined
1306
1337
try {
1307
1338
result = await nodeLess . render ( content , {
1308
1339
...options ,
1309
1340
plugins : [ viteResolverPlugin , ...( options . plugins || [ ] ) ] ,
1310
- sourceMap : {
1311
- outputSourceFiles : true ,
1312
- sourceMapFileInline : false
1313
- }
1341
+ ...( options . enableSourcemap
1342
+ ? {
1343
+ sourceMap : {
1344
+ outputSourceFiles : true ,
1345
+ sourceMapFileInline : false
1346
+ }
1347
+ }
1348
+ : { } )
1314
1349
} )
1315
1350
} catch ( e ) {
1316
1351
const error = e as Less . RenderError
@@ -1418,6 +1453,7 @@ const styl: StylePreprocessor = async (source, root, options) => {
1418
1453
source ,
1419
1454
options . filename ,
1420
1455
options . additionalData ,
1456
+ options . enableSourcemap ,
1421
1457
'\n'
1422
1458
)
1423
1459
// Get preprocessor options.imports dependencies as stylus
@@ -1427,19 +1463,21 @@ const styl: StylePreprocessor = async (source, root, options) => {
1427
1463
)
1428
1464
try {
1429
1465
const ref = nodeStylus ( content , options )
1430
- ref . set ( 'sourcemap' , {
1431
- comment : false ,
1432
- inline : false ,
1433
- basePath : root
1434
- } )
1466
+ if ( options . enableSourcemap ) {
1467
+ ref . set ( 'sourcemap' , {
1468
+ comment : false ,
1469
+ inline : false ,
1470
+ basePath : root
1471
+ } )
1472
+ }
1435
1473
1436
1474
const result = ref . render ( )
1437
1475
1438
1476
// Concat imports deps with computed deps
1439
1477
const deps = [ ...ref . deps ( ) , ...importsDeps ]
1440
1478
1441
1479
// @ts -expect-error sourcemap exists
1442
- const map : ExistingRawSourceMap = ref . sourcemap
1480
+ const map : ExistingRawSourceMap | undefined = ref . sourcemap
1443
1481
1444
1482
return {
1445
1483
code : result ,
@@ -1454,9 +1492,10 @@ const styl: StylePreprocessor = async (source, root, options) => {
1454
1492
}
1455
1493
1456
1494
function formatStylusSourceMap (
1457
- mapBefore : ExistingRawSourceMap ,
1495
+ mapBefore : ExistingRawSourceMap | undefined ,
1458
1496
root : string
1459
- ) : ExistingRawSourceMap {
1497
+ ) : ExistingRawSourceMap | undefined {
1498
+ if ( ! mapBefore ) return undefined
1460
1499
const map = { ...mapBefore }
1461
1500
1462
1501
const resolveFromRoot = ( p : string ) => normalizePath ( path . resolve ( root , p ) )
@@ -1472,7 +1511,8 @@ function formatStylusSourceMap(
1472
1511
async function getSource (
1473
1512
source : string ,
1474
1513
filename : string ,
1475
- additionalData ?: PreprocessorAdditionalData ,
1514
+ additionalData : PreprocessorAdditionalData | undefined ,
1515
+ enableSourcemap : boolean ,
1476
1516
sep : string = ''
1477
1517
) : Promise < { content : string ; map ?: ExistingRawSourceMap } > {
1478
1518
if ( ! additionalData ) return { content : source }
@@ -1485,6 +1525,10 @@ async function getSource(
1485
1525
return newContent
1486
1526
}
1487
1527
1528
+ if ( ! enableSourcemap ) {
1529
+ return { content : additionalData + sep + source }
1530
+ }
1531
+
1488
1532
const ms = new MagicString ( source )
1489
1533
ms . appendLeft ( 0 , sep )
1490
1534
ms . appendLeft ( 0 , additionalData )
0 commit comments