diff --git a/README.md b/README.md index dbe319a..484e57e 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,12 @@ eg. `{ lazyComponent: true }` Whether to automatically import styles. +### defaultLocale + +- Type: `string` + +Replace default locale, you can find locale list [here](https://github.com/youzan/vant/tree/main/packages/vant/src/locale/lang). + ### excludeExports - Type: `array` diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 7d2484f..afafb99 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -5,6 +5,7 @@ export default defineNuxtConfig({ modules: [Vant], vant: { lazyload: true, + defaultLocale: 'en-US', imports: ['Locale', 'useCurrentLang'] } }) diff --git a/src/core/index.ts b/src/core/index.ts index 304302a..9eb0c5c 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -1,6 +1,7 @@ export * from './components' export * from './imports' export * from './lazyload' +export * from './localePlugn' export * from './options' export * from './styles' export * from './transformPlugin' diff --git a/src/core/localePlugn.ts b/src/core/localePlugn.ts new file mode 100644 index 0000000..c600f0f --- /dev/null +++ b/src/core/localePlugn.ts @@ -0,0 +1,34 @@ +import { createUnplugin } from 'unplugin' +import MagicString from 'magic-string' +import type { NuxtOptions } from '@nuxt/schema' +import { libraryName } from '../config' + +interface LocalePluginOptions { + sourcemap?: NuxtOptions['sourcemap']['client'] + locale: string +} + +export const localePlugin = createUnplugin((options: LocalePluginOptions) => { + return { + name: `${libraryName}:locale`, + enforce: 'pre', + transformInclude (id) { + const regExp = new RegExp(`${libraryName}/es/locale/index`) + return !!id.match(regExp) + }, + transform (code, id) { + const s = new MagicString(code) + + s.replaceAll('zh-CN', options.locale) + + if (s.hasChanged()) { + return { + code: s.toString(), + map: options.sourcemap + ? s.generateMap({ source: id, includeContent: true }) + : undefined + } + } + } + } +}) diff --git a/src/module.ts b/src/module.ts index c2e2dff..cd32dbf 100644 --- a/src/module.ts +++ b/src/module.ts @@ -6,6 +6,7 @@ import { resolveLazyload, resolveOptions, resolveStyles, + localePlugin, transformPlugin } from './core/index' import type { Options } from './types' @@ -36,6 +37,13 @@ export default defineNuxtModule>({ transformStyles: name => resolveStyles(options, name) }) ) + + if (options.defaultLocale && options.defaultLocale !== 'zh-CN') { + config.plugins.push(localePlugin.vite({ + sourcemap: nuxt.options.sourcemap[mode], + locale: options.defaultLocale + })) + } }) nuxt.hook('webpack:config', (configs) => { @@ -51,6 +59,13 @@ export default defineNuxtModule>({ transformStyles: name => resolveStyles(options, name) }) ) + + if (options.defaultLocale && options.defaultLocale !== 'zh-CN') { + config.plugins.push(localePlugin.webpack({ + sourcemap: nuxt.options.sourcemap[mode], + locale: options.defaultLocale + })) + } }) }) } diff --git a/src/types.ts b/src/types.ts index aa43dfd..33db723 100644 --- a/src/types.ts +++ b/src/types.ts @@ -27,6 +27,13 @@ export interface Options extends TransformOptions { * @default true */ importStyle: boolean + /** + * Replace default locale, you can find locale list [here](https://github.com/youzan/vant/tree/main/packages/vant/src/locale/lang). + * + * @default 'zh-CN' + * @example 'en-US' + */ + defaultLocale?: string /** * Exclude exports from Vant that are not component content. *