Skip to content

Commit 49bc2e4

Browse files
authored
feat(compiler-sfc): upgrade to postcss 8 (#2710)
1 parent a89d985 commit 49bc2e4

File tree

6 files changed

+283
-247
lines changed

6 files changed

+283
-247
lines changed

packages/compiler-sfc/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
"lru-cache": "^5.1.1",
4747
"magic-string": "^0.25.7",
4848
"merge-source-map": "^1.1.0",
49-
"postcss": "^7.0.32",
50-
"postcss-modules": "^3.2.2",
49+
"postcss": "^8.1.10",
50+
"postcss-modules": "^4.0.0",
5151
"postcss-selector-parser": "^6.0.4",
5252
"source-map": "^0.6.1"
5353
},

packages/compiler-sfc/src/compileStyle.ts

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import postcss, {
22
ProcessOptions,
3-
LazyResult,
43
Result,
5-
ResultMap,
6-
ResultMessage
4+
SourceMap,
5+
Message,
6+
LazyResult
77
} from 'postcss'
88
import trimPlugin from './stylePluginTrim'
99
import scopedPlugin from './stylePluginScoped'
@@ -35,28 +35,33 @@ export interface SFCStyleCompileOptions {
3535
map?: RawSourceMap
3636
}
3737

38+
/**
39+
* Aligns with postcss-modules
40+
* https://github.com/css-modules/postcss-modules
41+
*/
42+
export interface CSSModulesOptions {
43+
scopeBehaviour?: 'global' | 'local'
44+
generateScopedName?:
45+
| string
46+
| ((name: string, filename: string, css: string) => string)
47+
hashPrefix?: string
48+
localsConvention?: 'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly'
49+
exportGlobals?: boolean
50+
globalModulePaths?: string[]
51+
}
52+
3853
export interface SFCAsyncStyleCompileOptions extends SFCStyleCompileOptions {
3954
isAsync?: boolean
4055
// css modules support, note this requires async so that we can get the
4156
// resulting json
4257
modules?: boolean
43-
// maps to postcss-modules options
44-
// https://github.com/css-modules/postcss-modules
45-
modulesOptions?: {
46-
scopeBehaviour?: 'global' | 'local'
47-
globalModulePaths?: string[]
48-
generateScopedName?:
49-
| string
50-
| ((name: string, filename: string, css: string) => string)
51-
hashPrefix?: string
52-
localsConvention?: 'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly'
53-
}
58+
modulesOptions?: CSSModulesOptions
5459
}
5560

5661
export interface SFCStyleCompileResults {
5762
code: string
5863
map: RawSourceMap | undefined
59-
rawResult: LazyResult | Result | undefined
64+
rawResult: Result | LazyResult | undefined
6065
errors: Error[]
6166
modules?: Record<string, string>
6267
dependencies: Set<string>
@@ -149,7 +154,7 @@ export function doCompileStyle(
149154

150155
let result: LazyResult | undefined
151156
let code: string | undefined
152-
let outMap: ResultMap | undefined
157+
let outMap: SourceMap | undefined
153158
// stylus output include plain css. so need remove the repeat item
154159
const dependencies = new Set(
155160
preProcessedSource ? preProcessedSource.dependencies : []
@@ -162,7 +167,7 @@ export function doCompileStyle(
162167
errors.push(...preProcessedSource.errors)
163168
}
164169

165-
const recordPlainCssDependencies = (messages: ResultMessage[]) => {
170+
const recordPlainCssDependencies = (messages: Message[]) => {
166171
messages.forEach(msg => {
167172
if (msg.type === 'dependency') {
168173
// postcss output path is absolute position path
@@ -226,7 +231,7 @@ function preprocess(
226231

227232
return preprocessor(
228233
options.source,
229-
options.map,
234+
options.inMap || options.map,
230235
{
231236
filename: options.filename,
232237
...options.preprocessOptions

packages/compiler-sfc/src/cssVars.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
BindingMetadata
99
} from '@vue/compiler-dom'
1010
import { SFCDescriptor } from './parse'
11-
import postcss, { Root } from 'postcss'
11+
import { PluginCreator } from 'postcss'
1212
import hash from 'hash-sum'
1313

1414
export const CSS_VARS_HELPER = `useCssVars`
@@ -49,20 +49,21 @@ export interface CssVarsPluginOptions {
4949
isProd: boolean
5050
}
5151

52-
export const cssVarsPlugin = postcss.plugin<CssVarsPluginOptions>(
53-
'vue-scoped',
54-
opts => (root: Root) => {
55-
const { id, isProd } = opts!
56-
root.walkDecls(decl => {
52+
export const cssVarsPlugin: PluginCreator<CssVarsPluginOptions> = opts => {
53+
const { id, isProd } = opts!
54+
return {
55+
postcssPlugin: 'vue-sfc-vars',
56+
Declaration(decl) {
5757
// rewrite CSS variables
5858
if (cssVarRE.test(decl.value)) {
5959
decl.value = decl.value.replace(cssVarRE, (_, $1, $2, $3) => {
6060
return `var(--${genVarName(id, $1 || $2 || $3, isProd)})`
6161
})
6262
}
63-
})
63+
}
6464
}
65-
)
65+
}
66+
cssVarsPlugin.postcss = true
6667

6768
export function genCssVarsCode(
6869
vars: string[],

0 commit comments

Comments
 (0)