Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler-sfc): upgrade to postcss 8 #2710

Merged
merged 3 commits into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/compiler-sfc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
"lru-cache": "^5.1.1",
"magic-string": "^0.25.7",
"merge-source-map": "^1.1.0",
"postcss": "^7.0.32",
"postcss-modules": "^3.2.2",
"postcss": "^8.1.10",
"postcss-modules": "^4.0.0",
"postcss-selector-parser": "^6.0.4",
"source-map": "^0.6.1"
},
Expand Down
41 changes: 23 additions & 18 deletions packages/compiler-sfc/src/compileStyle.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import postcss, {
ProcessOptions,
LazyResult,
Result,
ResultMap,
ResultMessage
SourceMap,
Message,
LazyResult
} from 'postcss'
import trimPlugin from './stylePluginTrim'
import scopedPlugin from './stylePluginScoped'
Expand Down Expand Up @@ -35,28 +35,33 @@ export interface SFCStyleCompileOptions {
map?: RawSourceMap
}

/**
* Aligns with postcss-modules
* https://github.com/css-modules/postcss-modules
*/
export interface CSSModulesOptions {
scopeBehaviour?: 'global' | 'local'
generateScopedName?:
| string
| ((name: string, filename: string, css: string) => string)
hashPrefix?: string
localsConvention?: 'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly'
exportGlobals?: boolean
globalModulePaths?: string[]
}

export interface SFCAsyncStyleCompileOptions extends SFCStyleCompileOptions {
isAsync?: boolean
// css modules support, note this requires async so that we can get the
// resulting json
modules?: boolean
// maps to postcss-modules options
// https://github.com/css-modules/postcss-modules
modulesOptions?: {
scopeBehaviour?: 'global' | 'local'
globalModulePaths?: string[]
generateScopedName?:
| string
| ((name: string, filename: string, css: string) => string)
hashPrefix?: string
localsConvention?: 'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly'
}
modulesOptions?: CSSModulesOptions
}

export interface SFCStyleCompileResults {
code: string
map: RawSourceMap | undefined
rawResult: LazyResult | Result | undefined
rawResult: Result | LazyResult | undefined
errors: Error[]
modules?: Record<string, string>
dependencies: Set<string>
Expand Down Expand Up @@ -149,7 +154,7 @@ export function doCompileStyle(

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

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

return preprocessor(
options.source,
options.map,
options.inMap || options.map,
{
filename: options.filename,
...options.preprocessOptions
Expand Down
17 changes: 9 additions & 8 deletions packages/compiler-sfc/src/cssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
BindingMetadata
} from '@vue/compiler-dom'
import { SFCDescriptor } from './parse'
import postcss, { Root } from 'postcss'
import { PluginCreator } from 'postcss'
import hash from 'hash-sum'

export const CSS_VARS_HELPER = `useCssVars`
Expand Down Expand Up @@ -49,20 +49,21 @@ export interface CssVarsPluginOptions {
isProd: boolean
}

export const cssVarsPlugin = postcss.plugin<CssVarsPluginOptions>(
'vue-scoped',
opts => (root: Root) => {
const { id, isProd } = opts!
root.walkDecls(decl => {
export const cssVarsPlugin: PluginCreator<CssVarsPluginOptions> = opts => {
const { id, isProd } = opts!
return {
postcssPlugin: 'vue-sfc-vars',
Declaration(decl) {
// rewrite CSS variables
if (cssVarRE.test(decl.value)) {
decl.value = decl.value.replace(cssVarRE, (_, $1, $2, $3) => {
return `var(--${genVarName(id, $1 || $2 || $3, isProd)})`
})
}
})
}
}
)
}
cssVarsPlugin.postcss = true

export function genCssVarsCode(
vars: string[],
Expand Down
Loading