Skip to content

Commit

Permalink
feat: css sourcemap support during dev (#7173)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Mar 21, 2022
1 parent de8beb9 commit 9c5103a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/plugin-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
descriptor,
Number(query.index),
options,
this
this,
filename
)
}
}
Expand Down
27 changes: 23 additions & 4 deletions packages/plugin-vue/src/style.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import type { SFCDescriptor } from 'vue/compiler-sfc'
import type { TransformPluginContext } from 'rollup'
import type { ExistingRawSourceMap, TransformPluginContext } from 'rollup'
import type { ResolvedOptions } from '.'
import type { RawSourceMap } from 'source-map'
import { formatPostcssSourceMap } from 'vite'

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export async function transformStyle(
code: string,
descriptor: SFCDescriptor,
index: number,
options: ResolvedOptions,
pluginContext: TransformPluginContext
pluginContext: TransformPluginContext,
filename: string
) {
const block = descriptor.styles[index]
// vite already handles pre-processors and CSS module so this is only
Expand All @@ -19,7 +22,14 @@ export async function transformStyle(
id: `data-v-${descriptor.id}`,
isProd: options.isProduction,
source: code,
scoped: block.scoped
scoped: block.scoped,
postcssOptions: {
map: {
from: filename,
inline: false,
annotation: false
}
}
})

if (result.errors.length) {
Expand All @@ -36,8 +46,17 @@ export async function transformStyle(
return null
}

const map = result.map
? formatPostcssSourceMap(
// version property of result.map is declared as string
// but actually it is a number
result.map as Omit<RawSourceMap, 'version'> as ExistingRawSourceMap,
filename
)
: ({ mappings: '' } as any)

return {
code: result.code,
map: result.map || ({ mappings: '' } as any)
map: map
}
}
10 changes: 10 additions & 0 deletions scripts/jestPerTestSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ beforeAll(async () => {
customLogger: createInMemoryLogger(serverLogs)
}

setupConsoleWarnCollector(serverLogs)

global.serverLogs = serverLogs

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

return logger
}

function setupConsoleWarnCollector(logs: string[]) {
const warn = console.warn
console.warn = (...args) => {
serverLogs.push(args.join(' '))
return warn.call(console, ...args)
}
}

0 comments on commit 9c5103a

Please sign in to comment.