This repository was archived by the owner on Jan 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathstyle.ts
85 lines (79 loc) · 2.17 KB
/
style.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import {
compileStyleAsync,
SFCAsyncStyleCompileOptions,
} from '@vue/compiler-sfc'
import { TransformPluginContext } from 'rollup'
import { Options } from '.'
import { getDescriptor } from './utils/descriptorCache'
import { StyleBlockQuery } from './utils/query'
import { normalizeSourceMap } from './utils/sourceMap'
export async function transformStyle(
code: string,
request: string,
options: Options,
query: StyleBlockQuery,
isProduction: boolean,
pluginContext: TransformPluginContext
) {
const descriptor = getDescriptor(query.filename)
const block = descriptor.styles[query.index]!
let preprocessOptions = options.preprocessOptions || {}
const preprocessLang = (options.preprocessStyles
? block.lang
: undefined) as SFCAsyncStyleCompileOptions['preprocessLang']
if (preprocessLang) {
preprocessOptions = preprocessOptions[preprocessLang] || preprocessOptions
// include node_modules for imports by default
switch (preprocessLang) {
case 'scss':
case 'sass':
preprocessOptions = {
includePaths: ['node_modules'],
...preprocessOptions,
}
break
case 'less':
case 'stylus':
preprocessOptions = {
paths: ['node_modules'],
...preprocessOptions,
}
}
} else {
preprocessOptions = {}
}
const result = await compileStyleAsync({
filename: query.filename,
id: `data-v-${query.id}`,
isProd: isProduction,
source: code,
scoped: block.scoped,
modules: !!block.module,
postcssOptions: options.postcssOptions,
postcssPlugins: options.postcssPlugins,
modulesOptions: options.cssModulesOptions,
preprocessLang,
preprocessCustomRequire: options.preprocessCustomRequire,
preprocessOptions,
})
if (result.errors.length) {
result.errors.forEach((error) =>
pluginContext.error({
id: query.filename,
message: error.message,
})
)
return null
}
if (query.module) {
return {
code: `export default ${JSON.stringify(result.modules)}`,
map: null,
}
} else {
return {
code: result.code,
map: normalizeSourceMap(result.map!, request),
}
}
}