From 40fa4bf436fc0f00c20413c33e74e9b63c06153b Mon Sep 17 00:00:00 2001 From: qiang Date: Wed, 23 Oct 2024 18:16:12 +0800 Subject: [PATCH] feat: add globalConfig (#132) --- README.md | 12 +++++++++++- playground/nuxt.config.ts | 3 ++- src/core/globalConfig.ts | 21 +++++++++++++++++++++ src/core/index.ts | 1 + src/module.ts | 5 +++++ src/types.ts | 11 ++++++++++- 6 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/core/globalConfig.ts diff --git a/README.md b/README.md index a85116b..fc5771b 100644 --- a/README.md +++ b/README.md @@ -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 > ``` @@ -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` diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index e0fdcf8..74a5fa3 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -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 } } }) diff --git a/src/core/globalConfig.ts b/src/core/globalConfig.ts new file mode 100644 index 0000000..79ac0c9 --- /dev/null +++ b/src/core/globalConfig.ts @@ -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); +}) +` + } + } +} diff --git a/src/core/index.ts b/src/core/index.ts index 2f8141b..a719e88 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -1,5 +1,6 @@ export * from './components' export * from './directives' +export * from './globalConfig' export * from './imports' export * from './injection' export * from './localePlugn' diff --git a/src/module.ts b/src/module.ts index 5b96ea8..84fbe86 100644 --- a/src/module.ts +++ b/src/module.ts @@ -3,6 +3,7 @@ import { defaults, libraryName } from './config' import { resolveComponents, resolveDirectives, + resolveGlobalConfig, resolveImports, resolveInjection, resolveOptions, @@ -31,6 +32,10 @@ export default defineNuxtModule>({ 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)) diff --git a/src/types.ts b/src/types.ts index ce0a4c6..3ea121e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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] @@ -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' {