Skip to content

Commit

Permalink
feat: useCssModules
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 18, 2022
1 parent 9b4e179 commit 0fabda7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ function genConfig(name) {
const vars = {
__VERSION__: version,
__DEV__: `process.env.NODE_ENV !== 'production'`,
__TEST__: false
__TEST__: false,
__GLOBAL__: opts.format === 'umd' || name.includes('browser')
}
// feature flags
Object.keys(featureFlags).forEach(key => {
Expand Down
1 change: 1 addition & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
declare const __DEV__: boolean
declare const __TEST__: boolean
declare const __GLOBAL__: boolean

interface Window {
__VUE_DEVTOOLS_GLOBAL_HOOK__: DevtoolsHook
Expand Down
2 changes: 2 additions & 0 deletions src/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export { useSlots, useAttrs, mergeDefaults } from './apiSetup'
export { nextTick } from 'core/util/next-tick'
export { set, del } from 'core/observer'

export { useCssModule } from './sfc-helpers/useCssModule'

/**
* @internal type is manually declared in <root>/types/v3-define-component.d.ts
*/
Expand Down
24 changes: 24 additions & 0 deletions src/v3/sfc-helpers/useCssModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { emptyObject, warn } from '../../core/util'
import { currentInstance } from '../currentInstance'

export function useCssModule(name = '$style'): Record<string, string> {
/* istanbul ignore else */
if (!__GLOBAL__) {
if (!currentInstance) {
__DEV__ && warn(`useCssModule must be called inside setup()`)
return emptyObject
}
const mod = currentInstance[name]
if (!mod) {
__DEV__ &&
warn(`Current instance does not have CSS module named "${name}".`)
return emptyObject
}
return mod as Record<string, string>
} else {
if (__DEV__) {
warn(`useCssModule() is not supported in the global build.`)
}
return emptyObject
}
}

0 comments on commit 0fabda7

Please sign in to comment.