Skip to content

Commit 8774a87

Browse files
authored
feat(unplugin-vue-i18n): support petite-vue-i18n and useVueI18nImportName option (#156)
1 parent 5d4b14c commit 8774a87

File tree

3 files changed

+72
-11
lines changed

3 files changed

+72
-11
lines changed

packages/unplugin-vue-i18n/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ This plugin will automatically select and bundle Vue I18n build according to the
237237

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

240+
### For `petite-vue-i18n`
241+
242+
This plugin will automatically select and bundle `petite-vue-i18n` build according to the following vite behavior:
243+
244+
- vite dev: `petite-vue-i18n.esm-bundler.js`
245+
- vite build: `petite-vue-i18n.runtime.esm-bundler.js`
246+
240247

241248
## 🔧 Options
242249

@@ -428,9 +435,15 @@ About details, See the [here](https://vue-i18n.intlify.dev/guide/advanced/optimi
428435

429436
This option that to use i18n custom blocks in `vue-class-component`.
430437

438+
### `useVueI18nImportName` (Experimental)
439+
440+
- **Type:** `boolean`
441+
- **Default:** `false`
442+
443+
Whether to use the import name of `petite-vue-i18n` with the same import name as vue-i18n (`import { xxx } from 'vue-i18n'`).
444+
445+
This option allows a smooth migration from `petite-vue-i18n` to `vue-i18n` and allows progressive enhacement.
431446

432-
## ✅ TODO
433-
- [ ] Bundling optimizations
434447

435448
## 📜 Changelog
436449

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

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import {
1212
assign
1313
} from '@intlify/shared'
1414
import { createFilter } from '@rollup/pluginutils'
15-
import { generateJSON, generateYAML } from '@intlify/bundle-utils'
15+
import {
16+
generateJSON,
17+
generateYAML,
18+
checkInstallPackage
19+
} from '@intlify/bundle-utils'
1620
import { RawSourceMap } from 'source-map'
1721
import { parse } from '@vue/compiler-sfc'
1822
import { parseVueRequest, VueQuery } from './query'
@@ -30,6 +34,8 @@ const VIRTUAL_PREFIX = '\0'
3034

3135
const debug = createDebug('unplugin-vue-i18n')
3236

37+
const installedPkg = checkInstallPackage('@intlify/unplugin-vue-i18n', debug)
38+
3339
export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
3440
debug('plugin options:', options, meta.framework)
3541

@@ -64,16 +70,35 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
6470
: true
6571
debug('runtimeOnly', runtimeOnly)
6672

67-
const compositionOnly = isBoolean(options.compositionOnly)
68-
? options.compositionOnly
73+
// prettier-ignore
74+
const compositionOnly = installedPkg === 'vue-i18n'
75+
? isBoolean(options.compositionOnly)
76+
? options.compositionOnly
77+
: true
6978
: true
7079
debug('compositionOnly', compositionOnly)
7180

72-
const fullInstall = isBoolean(options.fullInstall)
73-
? options.fullInstall
74-
: true
81+
// prettier-ignore
82+
const fullInstall = installedPkg === 'vue-i18n'
83+
? isBoolean(options.fullInstall)
84+
? options.fullInstall
85+
: true
86+
: false
7587
debug('fullInstall', fullInstall)
7688

89+
const useVueI18nImportName = options.useVueI18nImportName
90+
if (useVueI18nImportName != null) {
91+
warn(`'useVueI18nImportName' option is experimental`)
92+
}
93+
debug('useVueI18nImportName', useVueI18nImportName)
94+
95+
const getAliasName = () =>
96+
installedPkg === 'petite-vue-i18n' &&
97+
isBoolean(useVueI18nImportName) &&
98+
useVueI18nImportName
99+
? 'vue-i18n'
100+
: `${installedPkg}`
101+
77102
const esm = isBoolean(options.esm) ? options.esm : true
78103
debug('esm', esm)
79104

@@ -99,15 +124,37 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
99124
if (command === 'build' && runtimeOnly) {
100125
if (isArray(config.resolve!.alias)) {
101126
config.resolve!.alias.push({
102-
find: 'vue-i18n',
103-
replacement: `vue-i18n/dist/vue-i18n.runtime.esm-bundler.js`
127+
find: getAliasName(),
128+
replacement: `${installedPkg}/dist/${installedPkg}.runtime.esm-bundler.js`
104129
})
105130
} else if (isObject(config.resolve!.alias)) {
131+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
132+
;(config.resolve!.alias as any)[
133+
getAliasName()
134+
] = `${installedPkg}/dist/${installedPkg}.runtime.esm-bundler.js`
135+
}
136+
debug(`alias name: ${getAliasName()}`)
137+
debug(
138+
`set ${installedPkg} runtime only: ${installedPkg}/dist/${installedPkg}.runtime.esm-bundler.js`
139+
)
140+
} else if (
141+
command === 'serve' &&
142+
installedPkg === 'petite-vue-i18n' &&
143+
useVueI18nImportName
144+
) {
145+
normalizeConfigResolveAlias(config.resolve, meta.framework)
146+
if (isArray(config.resolve!.alias)) {
147+
config.resolve!.alias.push({
148+
find: 'vue-i18n',
149+
replacement: `petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`
150+
})
151+
} else {
106152
// eslint-disable-next-line @typescript-eslint/no-explicit-any
107153
;(config.resolve!.alias as any)[
108154
'vue-i18n'
109-
] = `vue-i18n/dist/vue-i18n.runtime.esm-bundler.js`
155+
] = `petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`
110156
}
157+
debug(`alias name: ${getAliasName()}`)
111158
}
112159

113160
config.define = config.define || {}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export interface PluginOptions {
1010
globalSFCScope?: boolean
1111
bridge?: boolean
1212
useClassComponent?: boolean
13+
useVueI18nImportName?: boolean
1314
}

0 commit comments

Comments
 (0)