vue3-yuanma/packages/runtime-dom/src/helpers/useCssModule.ts

30 lines
868 B
TypeScript
Raw Normal View History

2020-07-09 20:25:29 +00:00
import { warn, getCurrentInstance } from '@vue/runtime-core'
2019-12-18 02:28:24 +00:00
import { EMPTY_OBJ } from '@vue/shared'
2020-07-12 22:04:09 +00:00
export function useCssModule(name = '$style'): Record<string, string> {
if (!__GLOBAL__) {
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>
} else {
if (__DEV__) {
warn(`useCSSModule() is not supported in the global build.`)
}
2019-12-18 02:28:24 +00:00
return EMPTY_OBJ
}
}