Skip to content

Commit

Permalink
feat: add globalConfig (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolking authored Oct 23, 2024
1 parent 0c1cb2d commit 40fa4bf
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
## Installation

> [!WARNING]
> Since the [dayjs](https://github.com/iamkun/dayjs) used internally by element-plus is not a [JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), in order to ensure that it can be converted into a JavaScript module before startup, you need to add a `.npmrc` file to the root directory of the project and add the following configuration:
> Since the [dayjs](https://github.com/iamkun/dayjs) used internally by element-plus is not a [JavaScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules), in order to ensure that it can be converted into a JavaScript modules before startup, you need to add a `.npmrc` file to the root directory of the project and add the following configuration:
> ```
> shamefully-hoist=true
> ```
Expand Down Expand Up @@ -96,6 +96,16 @@ When you change the global namespace, you must change it here as well.

Replace default locale, you can find locale list [here](https://github.com/element-plus/element-plus/tree/dev/packages/locale/lang)

e.g. `'zh-cn'`

### globalConfig

- Type: `object`

Set global configuration, such as modifying the default `size` and `z-index` of the component.

e.g. `{ size: 'small', zIndex: 3000 }`

### injectionID

- Type: `object`
Expand Down
3 changes: 2 additions & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineNuxtConfig({
defaultLocale: 'zh-cn',
imports: ['useLocale'],
themes: ['dark'],
injectionID: { prefix: 100, current: 1 }
injectionID: { prefix: 100, current: 1 },
globalConfig: { size: 'small', zIndex: 1000 }
}
})
21 changes: 21 additions & 0 deletions src/core/globalConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { libraryName } from '../config'
import { resolvePath } from '../utils'
import type { Options } from '../types'

export async function resolveGlobalConfig (config: Options) {
const { globalConfig } = config
const libraryPath = await resolvePath(libraryName)

return {
filename: `${libraryName}-globalConfig.plugin.mjs`,
getContents: () => {
return `import { defineNuxtPlugin } from '#imports';
import { provideGlobalConfig } from '${libraryPath}';
export default defineNuxtPlugin(nuxtApp => {
provideGlobalConfig(${JSON.stringify(globalConfig)}, nuxtApp.vueApp, true);
})
`
}
}
}
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './components'
export * from './directives'
export * from './globalConfig'
export * from './imports'
export * from './injection'
export * from './localePlugn'
Expand Down
5 changes: 5 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defaults, libraryName } from './config'
import {
resolveComponents,
resolveDirectives,
resolveGlobalConfig,
resolveImports,
resolveInjection,
resolveOptions,
Expand Down Expand Up @@ -31,6 +32,10 @@ export default defineNuxtModule<Partial<Options>>({
nuxt.options.imports.autoImport !== false && resolveImports(options)
nuxt.options.components !== false && resolveComponents(options)

if (options.globalConfig) {
addPluginTemplate(await resolveGlobalConfig(options))
}

if (nuxt.options.ssr !== false) {
addPluginTemplate(await resolveInjection(options))
addPluginTemplate(resolveTeleports(options))
Expand Down
11 changes: 10 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ElIdInjectionContext, ElZIndexInjectionContext } from 'element-plus'
import type { ElIdInjectionContext, ElZIndexInjectionContext, ConfigProviderContext } from 'element-plus'

/** name: export name from the library, as: the name you want to use in your project, from: the name of library */
export type PresetImport = string | [name: string, as?: string, from?: string]
Expand Down Expand Up @@ -149,6 +149,15 @@ export interface Options extends TransformOptions {
* @example 'zh-cn'
*/
defaultLocale?: string
/**
* Set global configuration, such as modifying the default size and z-index of the component.
*
* @example
* ```ts
* { size: 'small', zIndex: 3000 }
* ```
*/
globalConfig?: ConfigProviderContext
}

declare module '@nuxt/schema' {
Expand Down

0 comments on commit 40fa4bf

Please sign in to comment.