Skip to content

Commit b64583a

Browse files
authored
breaking: drop useVueI18nImportName option (#372)
1 parent c09cd3b commit b64583a

File tree

4 files changed

+101
-109
lines changed

4 files changed

+101
-109
lines changed

packages/unplugin-vue-i18n/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ If do you will use this option, you need to enable `jitCompilation` option.
548548
This option that to use i18n custom blocks in `vue-class-component`.
549549

550550
> [!IMPORTANT]
551-
'useClassComponent' option is deprecated in v5.
551+
`useClassComponent` option is deprecated in v5.
552552
This option will be supported with vue-i18n until v9 latest version.
553553

554554
### `onlyLocales`
@@ -567,6 +567,9 @@ If do you will use this option, you need to enable `jitCompilation` option.
567567

568568
This option allows a smooth migration from `petite-vue-i18n` to `vue-i18n` and allows progressive enhacement.
569569

570+
> [!IMPORTANT]
571+
`useVueI18nImportName` option is deprecated in v5.
572+
This option will be supported with vue-i18n until v9 latest version.
570573

571574
## 📜 Changelog
572575

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

Lines changed: 37 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import createDebug from 'debug'
44
import fg from 'fast-glob'
55
import {
66
isArray,
7-
isObject,
87
isEmptyObject,
98
isString,
109
isNumber,
@@ -16,12 +15,11 @@ import { createFilter } from '@rollup/pluginutils'
1615
import {
1716
generateJSON,
1817
generateYAML,
19-
generateJavaScript,
20-
checkInstallPackage
18+
generateJavaScript
2119
} from '@intlify/bundle-utils'
2220
import { parse } from '@vue/compiler-sfc'
2321
import { parseVueRequest, VueQuery } from './query'
24-
import { getRaw, warn, error, raiseError } from './utils'
22+
import { getRaw, warn, error, raiseError, checkInstallPackage } from './utils'
2523

2624
import type { RawSourceMap } from 'source-map-js'
2725
import type {
@@ -37,7 +35,7 @@ const VIRTUAL_PREFIX = '\0'
3735

3836
const debug = createDebug('unplugin-vue-i18n')
3937

40-
const installedPkg = checkInstallPackage('@intlify/unplugin-vue-i18n', debug)
38+
const installedPkgInfo = checkInstallPackage(debug)
4139

4240
export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
4341
debug('plugin options:', options, meta.framework)
@@ -84,15 +82,15 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
8482
debug('dropMessageCompiler', dropMessageCompiler)
8583

8684
// prettier-ignore
87-
const compositionOnly = installedPkg === 'vue-i18n'
85+
const compositionOnly = installedPkgInfo.pkg === 'vue-i18n'
8886
? isBoolean(options.compositionOnly)
8987
? options.compositionOnly
9088
: true
9189
: true
9290
debug('compositionOnly', compositionOnly)
9391

9492
// prettier-ignore
95-
const fullInstall = installedPkg === 'vue-i18n'
93+
const fullInstall = installedPkgInfo.pkg === 'vue-i18n'
9694
? isBoolean(options.fullInstall)
9795
? options.fullInstall
9896
: true
@@ -102,20 +100,8 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
102100
const ssrBuild = !!options.ssr
103101
debug('ssr', ssrBuild)
104102

105-
const useVueI18nImportName = options.useVueI18nImportName
106-
if (useVueI18nImportName != null) {
107-
warn(`'useVueI18nImportName' option is experimental`)
108-
}
109-
debug('useVueI18nImportName', useVueI18nImportName)
110-
111-
// prettier-ignore
112-
const getVueI18nAliasName = () =>
113-
installedPkg === 'petite-vue-i18n' && isBoolean(useVueI18nImportName) && useVueI18nImportName
114-
? 'vue-i18n'
115-
: installedPkg
116-
117103
const getVueI18nAliasPath = ({ ssr = false, runtimeOnly = false }) => {
118-
return `${installedPkg}/dist/${installedPkg}${runtimeOnly ? '.runtime' : ''}.${
104+
return `${installedPkgInfo.alias}/dist/${installedPkgInfo.pkg}${runtimeOnly ? '.runtime' : ''}.${
119105
!ssr ? 'esm-bundler.js' /* '.mjs' */ : 'node.mjs'
120106
}`
121107
}
@@ -134,7 +120,8 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
134120
let isProduction = false
135121
let sourceMap = false
136122

137-
const vueI18nAliasName = getVueI18nAliasName()
123+
const vueI18nAliasName = installedPkgInfo.alias
124+
debug(`vue-i18n alias name: ${vueI18nAliasName}`)
138125

139126
return {
140127
name: 'unplugin-vue-i18n',
@@ -150,73 +137,30 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
150137
enforce: meta.framework === 'vite' ? 'pre' : 'post',
151138

152139
vite: {
153-
config(config, { command }) {
154-
config.resolve = normalizeConfigResolveAlias(
155-
config.resolve,
156-
meta.framework
157-
)
140+
config() {
141+
const defineConfig = {
142+
define: {
143+
__VUE_I18N_LEGACY_API__: !compositionOnly,
144+
__VUE_I18N_FULL_INSTALL__: fullInstall,
145+
__INTLIFY_DROP_MESSAGE_COMPILER__: dropMessageCompiler,
146+
__VUE_I18N_PROD_DEVTOOLS__: false
147+
}
148+
}
149+
debug('define Config:', defineConfig)
158150

159-
if (command === 'build') {
160-
debug(`vue-i18n alias name: ${vueI18nAliasName}`)
161-
if (isArray(config.resolve!.alias)) {
162-
config.resolve!.alias.push({
163-
find: vueI18nAliasName,
164-
replacement: getVueI18nAliasPath({
165-
ssr: ssrBuild,
166-
runtimeOnly
167-
})
168-
})
169-
} else if (isObject(config.resolve!.alias)) {
170-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
171-
;(config.resolve!.alias as any)[vueI18nAliasName] =
172-
getVueI18nAliasPath({
151+
const aliasConfig = {
152+
resolve: {
153+
alias: {
154+
[vueI18nAliasName]: getVueI18nAliasPath({
173155
ssr: ssrBuild,
174156
runtimeOnly
175157
})
158+
}
176159
}
177-
debug(
178-
`set ${vueI18nAliasName} runtime only: ${getVueI18nAliasPath({
179-
ssr: ssrBuild,
180-
runtimeOnly
181-
})}`
182-
)
183-
} else if (
184-
command === 'serve' &&
185-
installedPkg === 'petite-vue-i18n' &&
186-
useVueI18nImportName
187-
) {
188-
config.resolve = normalizeConfigResolveAlias(
189-
config.resolve,
190-
meta.framework
191-
)
192-
if (isArray(config.resolve!.alias)) {
193-
config.resolve!.alias.push({
194-
find: vueI18nAliasName,
195-
replacement: `petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`
196-
})
197-
} else {
198-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
199-
;(config.resolve!.alias as any)[vueI18nAliasName] =
200-
`petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`
201-
}
202-
debug(`petite-vue-i18n alias name: ${vueI18nAliasName}`)
203160
}
161+
debug('alias Config:', aliasConfig)
204162

205-
config.define = config.define || {}
206-
config.define['__VUE_I18N_LEGACY_API__'] = !compositionOnly
207-
debug(
208-
`set __VUE_I18N_LEGACY_API__ is '${config.define['__VUE_I18N_LEGACY_API__']}'`
209-
)
210-
config.define['__VUE_I18N_FULL_INSTALL__'] = fullInstall
211-
debug(
212-
`set __VUE_I18N_FULL_INSTALL__ is '${config.define['__VUE_I18N_FULL_INSTALL__']}'`
213-
)
214-
config.define['__INTLIFY_DROP_MESSAGE_COMPILER__'] = dropMessageCompiler
215-
debug(
216-
`set __INTLIFY_DROP_MESSAGE_COMPILER__ is '${config.define['__INTLIFY_DROP_MESSAGE_COMPILER__']}'`
217-
)
218-
219-
config.define['__VUE_I18N_PROD_DEVTOOLS__'] = false
163+
return assign(defineConfig, aliasConfig)
220164
},
221165

222166
configResolved(config) {
@@ -346,30 +290,18 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
346290
compiler.options.resolve,
347291
meta.framework
348292
)
349-
350-
if (isProduction) {
351-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
352-
;(compiler.options.resolve!.alias as any)[vueI18nAliasName] =
353-
getVueI18nAliasPath({
354-
ssr: ssrBuild,
355-
runtimeOnly
356-
})
357-
debug(
358-
`set ${vueI18nAliasName}: ${getVueI18nAliasPath({
359-
ssr: ssrBuild,
360-
runtimeOnly
361-
})}`
362-
)
363-
} else if (
364-
!isProduction &&
365-
installedPkg === 'petite-vue-i18n' &&
366-
useVueI18nImportName
367-
) {
368-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
369-
;(compiler.options.resolve!.alias as any)[vueI18nAliasName] =
370-
`petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`
371-
debug(`petite-vue-i18n alias name: ${vueI18nAliasName}`)
372-
}
293+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
294+
;(compiler.options.resolve!.alias as any)[vueI18nAliasName] =
295+
getVueI18nAliasPath({
296+
ssr: ssrBuild,
297+
runtimeOnly
298+
})
299+
debug(
300+
`set ${vueI18nAliasName}: ${getVueI18nAliasPath({
301+
ssr: ssrBuild,
302+
runtimeOnly
303+
})}`
304+
)
373305

374306
loadWebpack().then(webpack => {
375307
if (webpack) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export interface PluginOptions {
1111
forceStringify?: boolean
1212
defaultSFCLang?: SFCLangFormat
1313
globalSFCScope?: boolean
14-
useVueI18nImportName?: boolean
1514
strictMessage?: boolean
1615
escapeHtml?: boolean
1716
}

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
1-
import { promises as fs } from 'fs'
1+
import fs from 'node:fs'
2+
import { promises as fsp } from 'node:fs'
3+
import createDebug from 'debug'
24
import pc from 'picocolors'
5+
import module from 'node:module'
6+
import path from 'node:path'
7+
8+
const SUPPORT_PACKAGES = ['vue-i18n', 'petite-vue-i18n'] as const
9+
10+
type SupportPackage = (typeof SUPPORT_PACKAGES)[number]
11+
12+
type InstalledPackageInfo = {
13+
alias: string
14+
pkg: SupportPackage
15+
}
16+
17+
const _require = module.createRequire(import.meta.url)
18+
19+
export function checkInstallPackage(
20+
debug: createDebug.Debugger
21+
): InstalledPackageInfo {
22+
const pkgInfo =
23+
resolvePkgPath('vue-i18n', debug) ||
24+
resolvePkgPath('petite-vue-i18n', debug)
25+
if (!pkgInfo) {
26+
throw new Error(
27+
`requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
28+
)
29+
}
30+
31+
debug('installed package info:', pkgInfo)
32+
return pkgInfo
33+
}
34+
35+
function resolvePkgPath(
36+
id: string,
37+
debug: createDebug.Debugger
38+
): InstalledPackageInfo | null {
39+
try {
40+
/**
41+
* NOTE:
42+
* Assuming the case of using npm alias `npm:`,
43+
* get the installed package name from `package.json`
44+
*/
45+
const resolvedPath = _require.resolve(id)
46+
const pkgPath = path.dirname(resolvedPath)
47+
const pkgJson = JSON.parse(
48+
fs.readFileSync(path.join(pkgPath, 'package.json'), 'utf-8')
49+
) as { name: string }
50+
const pkgName: string = pkgJson.name.startsWith('vue-i18n')
51+
? 'vue-i18n'
52+
: pkgJson.name.startsWith('petite-vue-i18n')
53+
? 'petite-vue-i18n'
54+
: ''
55+
return pkgJson ? { alias: id, pkg: pkgName as SupportPackage } : null
56+
} catch (e) {
57+
debug(`cannot find '${id}'`, e)
58+
return null
59+
}
60+
}
361

462
export function warn(...args: unknown[]) {
563
console.warn(pc.yellow(pc.bold(`[unplugin-vue-i18n] `)), ...args)
@@ -10,7 +68,7 @@ export function error(...args: unknown[]) {
1068
}
1169

1270
export async function getRaw(path: string): Promise<string> {
13-
return fs.readFile(path, { encoding: 'utf-8' })
71+
return fsp.readFile(path, { encoding: 'utf-8' })
1472
}
1573

1674
export function raiseError(message: string) {

0 commit comments

Comments
 (0)