diff --git a/README.md b/README.md index 22b3778..1bf1537 100644 --- a/README.md +++ b/README.md @@ -117,11 +117,9 @@ e.g. `'zh-cn'` - Type: `boolean` - Default: `false` -Whether to cache the element-plus components and directives. +Whether to cache the element-plus components and directives. **Only effective in development mode**. -> [!WARNING] -> If you enable this feature, you will get faster loading speed in development mode. -> However, please note that this will make the `defaultLocale` ineffective in development mode. +If you enable this feature, you will get faster loading speed in development mode. ### globalConfig diff --git a/src/core/cache.ts b/src/core/cache.ts index 0dde8f5..a5a7588 100644 --- a/src/core/cache.ts +++ b/src/core/cache.ts @@ -1,10 +1,19 @@ import { libraryName } from '../config' +import type { ModuleOptions } from '../types' +import { camelize } from '../utils' + +export function resolveCache (config: ModuleOptions) { + const { defaultLocale } = config + const locale = camelize(defaultLocale ?? '') -export function resolveCache () { return { filename: `${libraryName}-cache.mjs`, getContents: () => { - return `export * from '${libraryName}';` + return `export * from '${libraryName}'; +${defaultLocale + ? `import ${locale} from '${libraryName}/es/locale/lang/${defaultLocale}.mjs'; +export { ${locale} };` + : ''}` } } } diff --git a/src/core/globalConfig.ts b/src/core/globalConfig.ts index efc0217..b9d3e55 100644 --- a/src/core/globalConfig.ts +++ b/src/core/globalConfig.ts @@ -1,18 +1,25 @@ import { libraryName } from '../config' import type { ModuleOptions } from '../types' +import { camelize, resolveComponentPath } from '../utils' export function resolveGlobalConfig (config: ModuleOptions) { - const { globalConfig } = config + const { globalConfig, cache, defaultLocale } = config + const needLocale = !!(cache && defaultLocale) + const locale = camelize(defaultLocale ?? '') + let provideConfig = JSON.stringify(globalConfig) + + if (needLocale) { + provideConfig = JSON.stringify(Object.assign({}, globalConfig, { locale })).replace(`"${locale}"`, locale) + } return { filename: `${libraryName}-globalConfig.plugin.mjs`, - getContents: () => { + getContents: async () => { return `import { defineNuxtPlugin, provideGlobalConfig } from '#imports'; - -export default defineNuxtPlugin(nuxtApp => { - provideGlobalConfig(${JSON.stringify(globalConfig)}, nuxtApp.vueApp, true); -}) -` +${needLocale ? `import { ${locale} } from '${await resolveComponentPath('', cache)}';\n` : ''} +export default defineNuxtPlugin(nuxtApp => { + provideGlobalConfig(${provideConfig}, nuxtApp.vueApp, true); +})` } } } diff --git a/src/module.ts b/src/module.ts index 94e26a1..1dab850 100644 --- a/src/module.ts +++ b/src/module.ts @@ -32,12 +32,17 @@ export default defineNuxtModule({ setup (options, nuxt) { const layers = getLayersDir(nuxt.options._layers) + // disable the `cache` option when building applications + if (!nuxt.options.dev) { + options.cache = false + } + resolveOptions(options) resolveThemes(options) resolveBaseImports(options) nuxt.options.imports.autoImport !== false && resolveImports(options) nuxt.options.components !== false && resolveComponents(options) - options.cache && addTemplate(resolveCache()) + options.cache && addTemplate(resolveCache(options)) options.globalConfig && addPluginTemplate(resolveGlobalConfig(options)) if (nuxt.options.ssr !== false) { diff --git a/src/types.ts b/src/types.ts index 598c7cf..1de0e4a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -183,9 +183,9 @@ export interface ModuleOptions extends TransformOptions { */ installMethods: Array<'ElLoading' | 'ElMessage' | 'ElMessageBox' | 'ElNotification'> /** - * Whether to cache the element-plus components and directives. + * Whether to cache the element-plus components and directives. **Only effective in development mode**. * - * If you enable this feature, you will get faster loading speed in development mode. However, please note that this will make the `defaultLocale` ineffective in development mode. + * If you enable this feature, you will get faster loading speed in development mode. * * @default 'false' */