Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 11 additions & 2 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
@@ -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} };`
: ''}`
}
}
}
21 changes: 14 additions & 7 deletions src/core/globalConfig.ts
Original file line number Diff line number Diff line change
@@ -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);
})`
}
}
}
7 changes: 6 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ export default defineNuxtModule<ModuleOptions>({
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) {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
*/
Expand Down