Skip to content

Commit 93eab2b

Browse files
authored
feat(unplugin-vue-i18n): support module option (#394)
1 parent 0f3ae89 commit 93eab2b

File tree

11 files changed

+37
-265
lines changed

11 files changed

+37
-265
lines changed

packages/unplugin-vue-i18n/README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,6 @@ This plugin will automatically select and bundle Vue I18n build according to the
253253

254254
About details, See the [here](https://vue-i18n.intlify.dev/guide/advanced/optimization.html#improve-performance-and-reduce-bundle-size-with-runtime-build-only)
255255

256-
### For `petite-vue-i18n`
257-
258-
This plugin will automatically select and bundle `petite-vue-i18n` build according to the following vite behavior:
259-
260-
- vite dev: `petite-vue-i18n.esm-bundler.js`
261-
- vite build: `petite-vue-i18n.runtime.esm-bundler.js`
262-
263256

264257
## 🔧 Options
265258

@@ -302,6 +295,27 @@ This plugin will automatically select and bundle `petite-vue-i18n` build accordi
302295
> [!WARNING]
303296
If you use the `js` and `ts` resources formats, set the paths, so your application code is not targeted. We recommend that resources be isolated from the application code.
304297

298+
### `module`
299+
300+
- **Type:** `string`
301+
- **Default:** `'vue-i18n'`
302+
303+
> [!NOTE]
304+
This options is supported from v5.1.0, and works with vue-i18n v10 and later.
305+
306+
Bundle target vue-i18n module. You can specify either `‘vue-i18n’` or `‘petite-vue-i18n’`.
307+
308+
The default is `'vue-i18n'`, and the following installed in node_modules will be bundled.
309+
310+
- development: `vue-i18n.esm-bundler.js`
311+
- production: `vue-i18n.runtime.esm-bundler.js`
312+
313+
In the case of `‘petite-vue-i18n’`, the following installed in node_modules will be bundled.
314+
315+
- development: `petite-vue-i18n.esm-bundler.js`
316+
- production: `petite-vue-i18n.runtime.esm-bundler.js`
317+
318+
If you are using petite-vue-i18n, you will need to set this value.
305319

306320
### `strictMessage`
307321

packages/unplugin-vue-i18n/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
"fast-glob": "^3.2.12",
3535
"js-yaml": "^4.1.0",
3636
"json5": "^2.2.3",
37-
"mlly": "^1.7.1",
3837
"pathe": "^1.0.0",
3938
"picocolors": "^1.0.0",
4039
"source-map-js": "^1.0.2",

packages/unplugin-vue-i18n/src/core/options.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import { isString, isBoolean, isArray } from '@intlify/shared'
33

44
import type { PluginOptions } from '../types'
55
import type { TranslationDirectiveResolveIndetifier } from '../vue'
6-
import type { InstalledPackageInfo } from '../utils'
76

8-
export function resolveOptions(
9-
options: PluginOptions,
10-
installedPkgInfo: InstalledPackageInfo
11-
) {
7+
export function resolveOptions(options: PluginOptions) {
8+
const moduleType = (options.module || 'vue-i18n') as string
9+
1210
// normalize for `options.onlyLocales`
1311
let onlyLocales: string[] = []
1412
if (options.onlyLocales) {
@@ -43,14 +41,14 @@ export function resolveOptions(
4341
const dropMessageCompiler = !!options.dropMessageCompiler
4442

4543
// prettier-ignore
46-
const compositionOnly = installedPkgInfo.pkg === 'vue-i18n'
44+
const compositionOnly = moduleType === 'vue-i18n'
4745
? isBoolean(options.compositionOnly)
4846
? options.compositionOnly
4947
: true
5048
: true
5149

5250
// prettier-ignore
53-
const fullInstall = installedPkgInfo.pkg === 'vue-i18n'
51+
const fullInstall = moduleType === 'vue-i18n'
5452
? isBoolean(options.fullInstall)
5553
? options.fullInstall
5654
: true
@@ -85,6 +83,7 @@ export function resolveOptions(
8583
return {
8684
include,
8785
exclude,
86+
module: moduleType,
8887
onlyLocales,
8988
forceStringify,
9089
defaultSFCLang,

packages/unplugin-vue-i18n/src/core/resource.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import type {
3636
TransformResult
3737
} from 'unplugin'
3838
import type { ResolvedOptions } from '../core/options'
39-
import type { InstalledPackageInfo } from '../utils'
4039
import type { VueQuery } from '../vue'
4140
import type { PluginOptions } from '../types'
4241

@@ -50,6 +49,7 @@ export function resourcePlugin(
5049
onlyLocales,
5150
include,
5251
exclude,
52+
module,
5353
forceStringify,
5454
defaultSFCLang,
5555
globalSFCScope,
@@ -63,18 +63,17 @@ export function resourcePlugin(
6363
escapeHtml,
6464
transformI18nBlock
6565
}: ResolvedOptions,
66-
meta: UnpluginContextMeta,
67-
installedPkgInfo: InstalledPackageInfo
66+
meta: UnpluginContextMeta
6867
): UnpluginOptions {
6968
const filter = createFilter(include, exclude)
7069
const getVueI18nAliasPath = ({ ssr = false, runtimeOnly = false }) => {
71-
return `${installedPkgInfo.alias}/dist/${installedPkgInfo.pkg}${runtimeOnly ? '.runtime' : ''}.${
70+
return `${module}/dist/${module}${runtimeOnly ? '.runtime' : ''}.${
7271
!ssr ? 'esm-bundler.js' /* '.mjs' */ : 'node.mjs'
7372
}`
7473
}
7574
let isProduction = false
7675
let sourceMap = false
77-
const vueI18nAliasName = installedPkgInfo.alias
76+
const vueI18nAliasName = module
7877
debug(`vue-i18n alias name: ${vueI18nAliasName}`)
7978

8079
let vuePlugin: RollupPlugin | null = null

packages/unplugin-vue-i18n/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { createUnplugin } from 'unplugin'
22
import createDebug from 'debug'
3-
import { raiseError, checkInstallPackage, resolveNamespace } from './utils'
3+
import { raiseError, resolveNamespace } from './utils'
44
import { resolveOptions, resourcePlugin, directivePlugin } from './core'
55

66
import type { UnpluginFactory, UnpluginInstance } from 'unplugin'
77
import type { PluginOptions } from './types'
88

99
const debug = createDebug(resolveNamespace('root'))
10-
const installedPkgInfo = checkInstallPackage(debug)
1110

1211
export * from './types'
1312

@@ -22,10 +21,10 @@ export const unpluginFactory: UnpluginFactory<PluginOptions | undefined> = (
2221
}
2322

2423
debug('plugin options (resolving):', options)
25-
const resolvedOptions = resolveOptions(options, installedPkgInfo)
24+
const resolvedOptions = resolveOptions(options)
2625
debug('plugin options (resolved):', resolvedOptions)
2726

28-
const plugins = [resourcePlugin(resolvedOptions, meta, installedPkgInfo)]
27+
const plugins = [resourcePlugin(resolvedOptions, meta)]
2928
if (resolvedOptions.optimizeTranslationDirective) {
3029
if (meta.framework === 'webpack') {
3130
raiseError(

packages/unplugin-vue-i18n/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
export type SFCLangFormat = 'json' | 'json5' | 'yml' | 'yaml'
2+
export type VueI18nModule = 'vue-i18n' | 'petite-vue-i18n'
3+
24
export interface PluginOptions {
35
include?: string | string[]
46
onlyLocales?: string | string[]
57
allowDynamic?: boolean
8+
module?: VueI18nModule
69
dropMessageCompiler?: boolean
710
runtimeOnly?: boolean
811
compositionOnly?: boolean
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
export * from './log'
22
export * from './plugin'
3-
export * from './resolver'
4-
export * from './pkg'

packages/unplugin-vue-i18n/src/utils/pkg.ts

Lines changed: 0 additions & 139 deletions
This file was deleted.

packages/unplugin-vue-i18n/src/utils/resolver.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)