feat(sfc): css modules support

This commit is contained in:
Evan You 2019-12-17 21:28:24 -05:00
parent abfea8eb45
commit d84cf3a538
5 changed files with 39 additions and 3 deletions

View File

@ -34,6 +34,7 @@ export type Data = { [key: string]: unknown }
export interface SFCInternalOptions { export interface SFCInternalOptions {
__scopeId?: string __scopeId?: string
__cssModules?: Data
__hmrId?: string __hmrId?: string
__hmrUpdated?: boolean __hmrUpdated?: boolean
} }

View File

@ -121,11 +121,18 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
// public $xxx properties & user-attached properties (sink) // public $xxx properties & user-attached properties (sink)
const publicGetter = publicPropertiesMap[key] const publicGetter = publicPropertiesMap[key]
if (publicGetter !== undefined) { let cssModule
if (publicGetter != null) {
if (__DEV__ && key === '$attrs') { if (__DEV__ && key === '$attrs') {
markAttrsAccessed() markAttrsAccessed()
} }
return publicGetter(target) return publicGetter(target)
} else if (
__BUNDLER__ &&
(cssModule = type.__cssModules) != null &&
(cssModule = cssModule[key])
) {
return cssModule
} else if (hasOwn(sink, key)) { } else if (hasOwn(sink, key)) {
return sink[key] return sink[key]
} else if (__DEV__ && currentRenderingInstance != null) { } else if (__DEV__ && currentRenderingInstance != null) {

View File

@ -0,0 +1,23 @@
import { getCurrentInstance } from '../component'
import { EMPTY_OBJ } from '@vue/shared'
import { warn } from '../warning'
export function useCSSModule(name = '$style'): Record<string, string> {
const instance = getCurrentInstance()!
if (!instance) {
__DEV__ && warn(`useCSSModule must be called inside setup()`)
return EMPTY_OBJ
}
const modules = instance.type.__cssModules
if (!modules) {
__DEV__ && warn(`Current instance does not have CSS modules injected.`)
return EMPTY_OBJ
}
const mod = modules[name]
if (!mod) {
__DEV__ &&
warn(`Current instance does not have CSS module named "${name}".`)
return EMPTY_OBJ
}
return mod as Record<string, string>
}

View File

@ -59,9 +59,11 @@ function createRecord(id: string, comp: ComponentOptions): boolean {
return true return true
} }
function rerender(id: string, newRender: RenderFunction) { function rerender(id: string, newRender?: RenderFunction) {
map.get(id)!.instances.forEach(instance => { map.get(id)!.instances.forEach(instance => {
if (newRender) {
instance.render = newRender instance.render = newRender
}
instance.renderCache = [] instance.renderCache = []
// this flag forces child components with slot content to update // this flag forces child components with slot content to update
instance.renderUpdated = true instance.renderUpdated = true

View File

@ -51,6 +51,9 @@ export const PatchFlags = PublicPatchFlags as {
BAIL: number BAIL: number
} }
// SFC CSS Modules
export { useCSSModule } from './helpers/useCssModule'
// For custom renderers // For custom renderers
export { createRenderer, RootRenderFunction } from './renderer' export { createRenderer, RootRenderFunction } from './renderer'
export { warn } from './warning' export { warn } from './warning'