Skip to content

Commit 57f14cb

Browse files
authored
feat(css): css.devSourcemap option (#7471)
1 parent e29ea8e commit 57f14cb

File tree

5 files changed

+82
-29
lines changed

5 files changed

+82
-29
lines changed

packages/playground/css-sourcemap/vite.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
}
1111
},
1212
css: {
13+
devSourcemap: true,
1314
preprocessorOptions: {
1415
less: {
1516
additionalData: '@color: red;'

packages/playground/vue-sourcemap/vite.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const vuePlugin = require('@vitejs/plugin-vue')
55
*/
66
module.exports = {
77
css: {
8+
devSourcemap: true,
89
preprocessorOptions: {
910
less: {
1011
additionalData: '@color: red;'

packages/plugin-vue/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export interface ResolvedOptions extends Options {
6363
compiler: typeof _compiler
6464
root: string
6565
sourceMap: boolean
66+
cssDevSourcemap: boolean
6667
devServer?: ViteDevServer
6768
devToolsEnabled?: boolean
6869
}
@@ -99,6 +100,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
99100
reactivityTransform,
100101
root: process.cwd(),
101102
sourceMap: true,
103+
cssDevSourcemap: false,
102104
devToolsEnabled: process.env.NODE_ENV !== 'production'
103105
}
104106

@@ -137,6 +139,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
137139
...options,
138140
root: config.root,
139141
sourceMap: config.command === 'build' ? !!config.build.sourcemap : true,
142+
cssDevSourcemap: config.css?.devSourcemap ?? false,
140143
isProduction: config.isProduction,
141144
devToolsEnabled:
142145
!!config.define!.__VUE_PROD_DEVTOOLS__ || !config.isProduction

packages/plugin-vue/src/style.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ export async function transformStyle(
2323
isProd: options.isProduction,
2424
source: code,
2525
scoped: block.scoped,
26-
postcssOptions: {
27-
map: {
28-
from: filename,
29-
inline: false,
30-
annotation: false
31-
}
32-
}
26+
...(options.cssDevSourcemap
27+
? {
28+
postcssOptions: {
29+
map: {
30+
from: filename,
31+
inline: false,
32+
annotation: false
33+
}
34+
}
35+
}
36+
: {})
3337
})
3438

3539
if (result.errors.length) {

packages/vite/src/node/plugins/css.ts

+66-22
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ export interface CSSOptions {
6262
| (Postcss.ProcessOptions & {
6363
plugins?: Postcss.Plugin[]
6464
})
65+
/**
66+
* Enables css sourcemaps during dev
67+
* @default false
68+
* @experimental
69+
*/
70+
devSourcemap?: boolean
6571
}
6672

6773
export interface CSSModulesOptions {
@@ -309,9 +315,12 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
309315
return `export default ${JSON.stringify(css)}`
310316
}
311317

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+
}
315324

316325
return [
317326
`import { updateStyle as __vite__updateStyle, removeStyle as __vite__removeStyle } from ${JSON.stringify(
@@ -612,7 +621,11 @@ async function compileCSS(
612621
modules?: Record<string, string>
613622
deps?: Set<string>
614623
}> {
615-
const { modules: modulesOptions, preprocessorOptions } = config.css || {}
624+
const {
625+
modules: modulesOptions,
626+
preprocessorOptions,
627+
devSourcemap
628+
} = config.css || {}
616629
const isModule = modulesOptions !== false && cssModuleRE.test(id)
617630
// although at serve time it can work without processing, we do need to
618631
// crawl them in order to register watch dependencies.
@@ -661,6 +674,7 @@ async function compileCSS(
661674
}
662675
// important: set this for relative import resolving
663676
opts.filename = cleanUrl(id)
677+
opts.enableSourcemap = devSourcemap ?? false
664678

665679
const preprocessResult = await preProcessor(
666680
code,
@@ -815,6 +829,16 @@ async function compileCSS(
815829
}
816830
}
817831

832+
if (!devSourcemap) {
833+
return {
834+
ast: postcssResult,
835+
code: postcssResult.css,
836+
map: { mappings: '' },
837+
modules,
838+
deps
839+
}
840+
}
841+
818842
const rawPostcssMap = postcssResult.map.toJSON()
819843

820844
const postcssMap = formatPostcssSourceMap(
@@ -1086,6 +1110,7 @@ type StylePreprocessorOptions = {
10861110
additionalData?: PreprocessorAdditionalData
10871111
filename: string
10881112
alias: Alias[]
1113+
enableSourcemap: boolean
10891114
}
10901115

10911116
type SassStylePreprocessorOptions = StylePreprocessorOptions & Sass.Options
@@ -1175,17 +1200,22 @@ const scss: SassStylePreprocessor = async (
11751200
const { content: data, map: additionalMap } = await getSource(
11761201
source,
11771202
options.filename,
1178-
options.additionalData
1203+
options.additionalData,
1204+
options.enableSourcemap
11791205
)
11801206
const finalOptions: Sass.Options = {
11811207
...options,
11821208
data,
11831209
file: options.filename,
11841210
outFile: options.filename,
11851211
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+
: {})
11891219
}
11901220

11911221
try {
@@ -1299,18 +1329,23 @@ const less: StylePreprocessor = async (source, root, options, resolvers) => {
12991329
const { content, map: additionalMap } = await getSource(
13001330
source,
13011331
options.filename,
1302-
options.additionalData
1332+
options.additionalData,
1333+
options.enableSourcemap
13031334
)
13041335

13051336
let result: Less.RenderOutput | undefined
13061337
try {
13071338
result = await nodeLess.render(content, {
13081339
...options,
13091340
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+
: {})
13141349
})
13151350
} catch (e) {
13161351
const error = e as Less.RenderError
@@ -1418,6 +1453,7 @@ const styl: StylePreprocessor = async (source, root, options) => {
14181453
source,
14191454
options.filename,
14201455
options.additionalData,
1456+
options.enableSourcemap,
14211457
'\n'
14221458
)
14231459
// Get preprocessor options.imports dependencies as stylus
@@ -1427,19 +1463,21 @@ const styl: StylePreprocessor = async (source, root, options) => {
14271463
)
14281464
try {
14291465
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+
}
14351473

14361474
const result = ref.render()
14371475

14381476
// Concat imports deps with computed deps
14391477
const deps = [...ref.deps(), ...importsDeps]
14401478

14411479
// @ts-expect-error sourcemap exists
1442-
const map: ExistingRawSourceMap = ref.sourcemap
1480+
const map: ExistingRawSourceMap | undefined = ref.sourcemap
14431481

14441482
return {
14451483
code: result,
@@ -1454,9 +1492,10 @@ const styl: StylePreprocessor = async (source, root, options) => {
14541492
}
14551493

14561494
function formatStylusSourceMap(
1457-
mapBefore: ExistingRawSourceMap,
1495+
mapBefore: ExistingRawSourceMap | undefined,
14581496
root: string
1459-
): ExistingRawSourceMap {
1497+
): ExistingRawSourceMap | undefined {
1498+
if (!mapBefore) return undefined
14601499
const map = { ...mapBefore }
14611500

14621501
const resolveFromRoot = (p: string) => normalizePath(path.resolve(root, p))
@@ -1472,7 +1511,8 @@ function formatStylusSourceMap(
14721511
async function getSource(
14731512
source: string,
14741513
filename: string,
1475-
additionalData?: PreprocessorAdditionalData,
1514+
additionalData: PreprocessorAdditionalData | undefined,
1515+
enableSourcemap: boolean,
14761516
sep: string = ''
14771517
): Promise<{ content: string; map?: ExistingRawSourceMap }> {
14781518
if (!additionalData) return { content: source }
@@ -1485,6 +1525,10 @@ async function getSource(
14851525
return newContent
14861526
}
14871527

1528+
if (!enableSourcemap) {
1529+
return { content: additionalData + sep + source }
1530+
}
1531+
14881532
const ms = new MagicString(source)
14891533
ms.appendLeft(0, sep)
14901534
ms.appendLeft(0, additionalData)

0 commit comments

Comments
 (0)