Skip to content

Commit 9c5103a

Browse files
authored
feat: css sourcemap support during dev (#7173)
1 parent de8beb9 commit 9c5103a

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

packages/plugin-vue/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
235235
descriptor,
236236
Number(query.index),
237237
options,
238-
this
238+
this,
239+
filename
239240
)
240241
}
241242
}

packages/plugin-vue/src/style.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import type { SFCDescriptor } from 'vue/compiler-sfc'
2-
import type { TransformPluginContext } from 'rollup'
2+
import type { ExistingRawSourceMap, TransformPluginContext } from 'rollup'
33
import type { ResolvedOptions } from '.'
4+
import type { RawSourceMap } from 'source-map'
5+
import { formatPostcssSourceMap } from 'vite'
46

57
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
68
export async function transformStyle(
79
code: string,
810
descriptor: SFCDescriptor,
911
index: number,
1012
options: ResolvedOptions,
11-
pluginContext: TransformPluginContext
13+
pluginContext: TransformPluginContext,
14+
filename: string
1215
) {
1316
const block = descriptor.styles[index]
1417
// vite already handles pre-processors and CSS module so this is only
@@ -19,7 +22,14 @@ export async function transformStyle(
1922
id: `data-v-${descriptor.id}`,
2023
isProd: options.isProduction,
2124
source: code,
22-
scoped: block.scoped
25+
scoped: block.scoped,
26+
postcssOptions: {
27+
map: {
28+
from: filename,
29+
inline: false,
30+
annotation: false
31+
}
32+
}
2333
})
2434

2535
if (result.errors.length) {
@@ -36,8 +46,17 @@ export async function transformStyle(
3646
return null
3747
}
3848

49+
const map = result.map
50+
? formatPostcssSourceMap(
51+
// version property of result.map is declared as string
52+
// but actually it is a number
53+
result.map as Omit<RawSourceMap, 'version'> as ExistingRawSourceMap,
54+
filename
55+
)
56+
: ({ mappings: '' } as any)
57+
3958
return {
4059
code: result.code,
41-
map: result.map || ({ mappings: '' } as any)
60+
map: map
4261
}
4362
}

scripts/jestPerTestSetup.ts

+10
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ beforeAll(async () => {
114114
customLogger: createInMemoryLogger(serverLogs)
115115
}
116116

117+
setupConsoleWarnCollector(serverLogs)
118+
117119
global.serverLogs = serverLogs
118120

119121
if (!isBuildTest) {
@@ -263,3 +265,11 @@ function createInMemoryLogger(logs: string[]): Logger {
263265

264266
return logger
265267
}
268+
269+
function setupConsoleWarnCollector(logs: string[]) {
270+
const warn = console.warn
271+
console.warn = (...args) => {
272+
serverLogs.push(args.join(' '))
273+
return warn.call(console, ...args)
274+
}
275+
}

0 commit comments

Comments
 (0)