Skip to content

Commit 4d535e9

Browse files
authored
feat: supports simultaneous configuration of cache and defaultLocale (#163)
1 parent 354e7a7 commit 4d535e9

5 files changed

Lines changed: 35 additions & 16 deletions

File tree

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,9 @@ e.g. `'zh-cn'`
117117
- Type: `boolean`
118118
- Default: `false`
119119

120-
Whether to cache the element-plus components and directives.
120+
Whether to cache the element-plus components and directives. **Only effective in development mode**.
121121

122-
> [!WARNING]
123-
> If you enable this feature, you will get faster loading speed in development mode.
124-
> However, please note that this will make the `defaultLocale` ineffective in development mode.
122+
If you enable this feature, you will get faster loading speed in development mode.
125123

126124
### globalConfig
127125

src/core/cache.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { libraryName } from '../config'
2+
import type { ModuleOptions } from '../types'
3+
import { camelize } from '../utils'
4+
5+
export function resolveCache (config: ModuleOptions) {
6+
const { defaultLocale } = config
7+
const locale = camelize(defaultLocale ?? '')
28

3-
export function resolveCache () {
49
return {
510
filename: `${libraryName}-cache.mjs`,
611
getContents: () => {
7-
return `export * from '${libraryName}';`
12+
return `export * from '${libraryName}';
13+
${defaultLocale
14+
? `import ${locale} from '${libraryName}/es/locale/lang/${defaultLocale}.mjs';
15+
export { ${locale} };`
16+
: ''}`
817
}
918
}
1019
}

src/core/globalConfig.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import { libraryName } from '../config'
22
import type { ModuleOptions } from '../types'
3+
import { camelize, resolveComponentPath } from '../utils'
34

45
export function resolveGlobalConfig (config: ModuleOptions) {
5-
const { globalConfig } = config
6+
const { globalConfig, cache, defaultLocale } = config
7+
const needLocale = !!(cache && defaultLocale)
8+
const locale = camelize(defaultLocale ?? '')
9+
let provideConfig = JSON.stringify(globalConfig)
10+
11+
if (needLocale) {
12+
provideConfig = JSON.stringify(Object.assign({}, globalConfig, { locale })).replace(`"${locale}"`, locale)
13+
}
614

715
return {
816
filename: `${libraryName}-globalConfig.plugin.mjs`,
9-
getContents: () => {
17+
getContents: async () => {
1018
return `import { defineNuxtPlugin, provideGlobalConfig } from '#imports';
11-
12-
export default defineNuxtPlugin(nuxtApp => {
13-
provideGlobalConfig(${JSON.stringify(globalConfig)}, nuxtApp.vueApp, true);
14-
})
15-
`
19+
${needLocale ? `import { ${locale} } from '${await resolveComponentPath('', cache)}';\n` : ''}
20+
export default defineNuxtPlugin(nuxtApp => {
21+
provideGlobalConfig(${provideConfig}, nuxtApp.vueApp, true);
22+
})`
1623
}
1724
}
1825
}

src/module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ export default defineNuxtModule<ModuleOptions>({
3232
setup (options, nuxt) {
3333
const layers = getLayersDir(nuxt.options._layers)
3434

35+
// disable the `cache` option when building applications
36+
if (!nuxt.options.dev) {
37+
options.cache = false
38+
}
39+
3540
resolveOptions(options)
3641
resolveThemes(options)
3742
resolveBaseImports(options)
3843
nuxt.options.imports.autoImport !== false && resolveImports(options)
3944
nuxt.options.components !== false && resolveComponents(options)
40-
options.cache && addTemplate(resolveCache())
45+
options.cache && addTemplate(resolveCache(options))
4146
options.globalConfig && addPluginTemplate(resolveGlobalConfig(options))
4247

4348
if (nuxt.options.ssr !== false) {

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ export interface ModuleOptions extends TransformOptions {
183183
*/
184184
installMethods: Array<'ElLoading' | 'ElMessage' | 'ElMessageBox' | 'ElNotification'>
185185
/**
186-
* Whether to cache the element-plus components and directives.
186+
* Whether to cache the element-plus components and directives. **Only effective in development mode**.
187187
*
188-
* 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.
188+
* If you enable this feature, you will get faster loading speed in development mode.
189189
*
190190
* @default 'false'
191191
*/

0 commit comments

Comments
 (0)