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

refactor(compiler-sfc): bump postcss to version 8 #2649

Closed
wants to merge 10 commits into from
15 changes: 11 additions & 4 deletions packages/compiler-sfc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
},
"homepage": "https://github.com/vuejs/vue-next/tree/master/packages/compiler-sfc#readme",
"peerDependencies": {
"vue": "3.0.2"
"vue": "3.0.2",
"postcss": "^8.1.8"
},
"dependencies": {
"@babel/parser": "^7.12.0",
Expand All @@ -41,20 +42,26 @@
"@vue/compiler-ssr": "3.0.2",
"@vue/shared": "3.0.2",
"consolidate": "^0.16.0",
"cssesc": "^3.0.0",
"estree-walker": "^2.0.1",
"generic-names": "^3.0.0",
"hash-sum": "^2.0.0",
"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-modules": "^3.0.0",
"postcss-modules-extract-imports": "^3.0.0",
"postcss-modules-local-by-default": "^4.0.0",
"postcss-modules-scope": "^3.0.0",
"postcss-modules-values": "^4.0.0",
"postcss-selector-parser": "^6.0.4",
"source-map": "^0.6.1"
},
"devDependencies": {
"@types/consolidate": "^0.14.0",
"@types/lru-cache": "^5.1.0",
"pug": "^2.0.4",
"sass": "^1.26.9"
"sass": "^1.26.9",
"postcss": "^8.1.8"
}
}
44 changes: 22 additions & 22 deletions packages/compiler-sfc/src/compileStyle.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import postcss, {
ProcessOptions,
LazyResult,
Result,
ResultMap,
ResultMessage
} from 'postcss'
import postcss, { ProcessOptions, Result, SourceMap, Message } from 'postcss'
import LazyResult from 'postcss/lib/lazy-result'
import trimPlugin from './stylePluginTrim'
import scopedPlugin from './stylePluginScoped'
import {
Expand Down Expand Up @@ -35,28 +30,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 +149,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 +162,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 +226,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 @@ -10,7 +10,7 @@ import {
import { SFCDescriptor } from './parse'
import { rewriteDefault } from './rewriteDefault'
import { ParserPlugin } from '@babel/parser'
import postcss, { Root } from 'postcss'
import { PluginCreator } from 'postcss'
import hash from 'hash-sum'

export const CSS_VARS_HELPER = `useCssVars`
Expand Down Expand Up @@ -51,20 +51,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