Skip to content

Commit 72d3a7d

Browse files
authored
fix(unplugin-vue-i18n): resource resolving and alias resolving (#201)
1 parent d2bcb64 commit 72d3a7d

File tree

3 files changed

+163
-31
lines changed

3 files changed

+163
-31
lines changed

packages/unplugin-vue-i18n/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
}
2727
},
2828
"dependencies": {
29-
"@intlify/bundle-utils": "^3.2.1",
29+
"@intlify/bundle-utils": "^3.4.0",
3030
"@intlify/shared": "next",
3131
"@rollup/pluginutils": "^4.2.0",
32-
"@vue/compiler-sfc": "^3.2.23",
32+
"@vue/compiler-sfc": "^3.2.45",
3333
"debug": "^4.3.1",
3434
"fast-glob": "^3.2.5",
3535
"js-yaml": "^4.1.0",
3636
"json5": "^2.2.0",
37-
"pathe": "^0.2.0",
37+
"pathe": "^0.3.9",
3838
"picocolors": "^1.0.0",
3939
"source-map": "0.6.1",
4040
"unplugin": "^0.8.0"

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

+79-24
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
generateJSON,
1818
generateYAML,
1919
checkInstallPackage,
20-
checkVueI18nBridgeInstallPackage
20+
checkVueI18nBridgeInstallPackage,
21+
getVueI18nVersion
2122
} from '@intlify/bundle-utils'
2223
import { RawSourceMap } from 'source-map'
2324
import { parse } from '@vue/compiler-sfc'
@@ -38,6 +39,7 @@ const debug = createDebug('unplugin-vue-i18n')
3839

3940
const installedPkg = checkInstallPackage('@intlify/unplugin-vue-i18n', debug)
4041
const installedVueI18nBridge = checkVueI18nBridgeInstallPackage(debug)
42+
const vueI18nVersion = getVueI18nVersion(debug)
4143

4244
export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
4345
debug('plugin options:', options, meta.framework)
@@ -99,20 +101,29 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
99101
debug('useVueI18nImportName', useVueI18nImportName)
100102

101103
// prettier-ignore
102-
const getAliasName = () =>
103-
installedVueI18nBridge && installedPkg === 'vue-i18n'
104-
? 'vue-i18n-bridge'
105-
: installedPkg === 'petite-vue-i18n' && isBoolean(useVueI18nImportName) &&
106-
useVueI18nImportName
104+
const getVueI18nAliasName = () =>
105+
vueI18nVersion === '9' || vueI18nVersion === '8'
106+
? 'vue-i18n'
107+
: vueI18nVersion === 'unknown' && installedPkg === 'petite-vue-i18n' && isBoolean(useVueI18nImportName) && useVueI18nImportName
107108
? 'vue-i18n'
108-
: `${installedPkg}`
109+
: installedPkg
110+
111+
const getVueI18nBridgeAliasPath = () =>
112+
`vue-i18n-bridge/dist/vue-i18n-bridge.runtime.esm-bundler.js`
113+
114+
const getVueI18nAliasPath = (aliasName: string) =>
115+
vueI18nVersion === '8'
116+
? `${aliasName}/dist/${aliasName}.esm.js` // for vue-i18n@8
117+
: `${aliasName}/dist/${installedPkg}.runtime.esm-bundler.js`
109118

110119
const esm = isBoolean(options.esm) ? options.esm : true
111120
debug('esm', esm)
112121

113122
let isProduction = false
114123
let sourceMap = false
115124

125+
const vueI18nAliasName = getVueI18nAliasName()
126+
116127
return {
117128
name: 'unplugin-vue-i18n',
118129

@@ -132,23 +143,40 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
132143
config.resolve,
133144
meta.framework
134145
)
146+
135147
if (command === 'build' && runtimeOnly) {
136-
const aliasName = getAliasName()
137-
debug(`alias name: ${aliasName}`)
148+
debug(`vue-i18n alias name: ${vueI18nAliasName}`)
138149
if (isArray(config.resolve!.alias)) {
139150
config.resolve!.alias.push({
140-
find: aliasName,
141-
replacement: `${aliasName}/dist/${aliasName}.runtime.esm-bundler.js`
151+
find: vueI18nAliasName,
152+
replacement: getVueI18nAliasPath(vueI18nAliasName)
142153
})
154+
if (installedVueI18nBridge) {
155+
config.resolve!.alias.push({
156+
find: 'vue-i18n-bridge',
157+
replacement: getVueI18nBridgeAliasPath()
158+
})
159+
}
143160
} else if (isObject(config.resolve!.alias)) {
144161
// eslint-disable-next-line @typescript-eslint/no-explicit-any
145-
;(config.resolve!.alias as any)[
146-
aliasName
147-
] = `${aliasName}/dist/${aliasName}.runtime.esm-bundler.js`
162+
;(config.resolve!.alias as any)[vueI18nAliasName] =
163+
getVueI18nAliasPath(vueI18nAliasName)
164+
if (installedVueI18nBridge) {
165+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
166+
;(config.resolve!.alias as any)['vue-i18n-bridge'] =
167+
getVueI18nBridgeAliasPath()
168+
}
148169
}
149170
debug(
150-
`set ${aliasName} runtime only: ${aliasName}/dist/${aliasName}.runtime.esm-bundler.js`
171+
`set ${vueI18nAliasName} runtime only: ${getVueI18nAliasPath(
172+
vueI18nAliasName
173+
)}`
151174
)
175+
if (installedVueI18nBridge) {
176+
debug(
177+
`set vue-i18n-bridge runtime only: ${getVueI18nBridgeAliasPath()}`
178+
)
179+
}
152180
} else if (
153181
command === 'serve' &&
154182
installedPkg === 'petite-vue-i18n' &&
@@ -160,16 +188,16 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
160188
)
161189
if (isArray(config.resolve!.alias)) {
162190
config.resolve!.alias.push({
163-
find: 'vue-i18n',
191+
find: vueI18nAliasName,
164192
replacement: `petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`
165193
})
166194
} else {
167195
// eslint-disable-next-line @typescript-eslint/no-explicit-any
168196
;(config.resolve!.alias as any)[
169-
'vue-i18n'
197+
vueI18nAliasName
170198
] = `petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`
171199
}
172-
debug(`alias name: ${getAliasName()}`)
200+
debug(`petite-vue-i18n alias name: ${vueI18nAliasName}`)
173201
}
174202

175203
config.define = config.define || {}
@@ -239,15 +267,40 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
239267
sourceMap = !!compiler.options.devtool
240268
debug(`webpack: isProduction = ${isProduction}, sourceMap = ${sourceMap}`)
241269

270+
compiler.options.resolve = normalizeConfigResolveAlias(
271+
compiler.options.resolve,
272+
meta.framework
273+
)
274+
242275
if (isProduction && runtimeOnly) {
243-
compiler.options.resolve = normalizeConfigResolveAlias(
244-
compiler.options.resolve,
245-
meta.framework
276+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
277+
;(compiler.options.resolve!.alias as any)[vueI18nAliasName] =
278+
getVueI18nAliasPath(vueI18nAliasName)
279+
if (installedVueI18nBridge) {
280+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
281+
;(compiler.options.resolve!.alias as any)['vue-i18n-bridge'] =
282+
getVueI18nBridgeAliasPath()
283+
}
284+
debug(
285+
`set ${vueI18nAliasName} runtime only: ${getVueI18nAliasPath(
286+
vueI18nAliasName
287+
)}`
246288
)
289+
if (installedVueI18nBridge) {
290+
debug(
291+
`set vue-i18n-bridge runtime only: ${getVueI18nBridgeAliasPath()}`
292+
)
293+
}
294+
} else if (
295+
!isProduction &&
296+
installedPkg === 'petite-vue-i18n' &&
297+
useVueI18nImportName
298+
) {
247299
// eslint-disable-next-line @typescript-eslint/no-explicit-any
248300
;(compiler.options.resolve!.alias as any)[
249-
'vue-i18n'
250-
] = `vue-i18n/dist/vue-i18n.runtime.esm-bundler.js`
301+
vueI18nAliasName
302+
] = `petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`
303+
debug(`petite-vue-i18n alias name: ${vueI18nAliasName}`)
251304
}
252305

253306
loadWebpack().then(webpack => {
@@ -274,7 +327,9 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
274327
compiler.options.module.rules.push({
275328
test: /\.(json5?|ya?ml)$/,
276329
type: 'javascript/auto',
277-
exclude: include // exclude target i18n resources
330+
include(resource: string) {
331+
return filter(resource)
332+
}
278333
})
279334
}
280335

yarn.lock

+81-4
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ __metadata:
798798
languageName: unknown
799799
linkType: soft
800800

801-
"@intlify/bundle-utils@^3.1.2, @intlify/bundle-utils@^3.2.1, @intlify/bundle-utils@workspace:packages/bundle-utils":
801+
"@intlify/bundle-utils@^3.1.2, @intlify/bundle-utils@^3.4.0, @intlify/bundle-utils@workspace:packages/bundle-utils":
802802
version: 0.0.0-use.local
803803
resolution: "@intlify/bundle-utils@workspace:packages/bundle-utils"
804804
dependencies:
@@ -895,16 +895,16 @@ __metadata:
895895
version: 0.0.0-use.local
896896
resolution: "@intlify/unplugin-vue-i18n@workspace:packages/unplugin-vue-i18n"
897897
dependencies:
898-
"@intlify/bundle-utils": ^3.2.1
898+
"@intlify/bundle-utils": ^3.4.0
899899
"@intlify/shared": next
900900
"@rollup/pluginutils": ^4.2.0
901-
"@vue/compiler-sfc": ^3.2.23
901+
"@vue/compiler-sfc": ^3.2.45
902902
debug: ^4.3.1
903903
fast-glob: ^3.2.5
904904
js-yaml: ^4.1.0
905905
json5: ^2.2.0
906906
mlly: ^0.5.2
907-
pathe: ^0.2.0
907+
pathe: ^0.3.9
908908
picocolors: ^1.0.0
909909
source-map: 0.6.1
910910
unbuild: ^0.8.11
@@ -2427,6 +2427,18 @@ __metadata:
24272427
languageName: node
24282428
linkType: hard
24292429

2430+
"@vue/compiler-core@npm:3.2.45":
2431+
version: 3.2.45
2432+
resolution: "@vue/compiler-core@npm:3.2.45"
2433+
dependencies:
2434+
"@babel/parser": ^7.16.4
2435+
"@vue/shared": 3.2.45
2436+
estree-walker: ^2.0.2
2437+
source-map: ^0.6.1
2438+
checksum: e3c687b24c16c2b320c02ed38960f8bee7dcb88bddb09e60a80d2d4dc004070cbbd4eccbc99cc168d48d753ff60d0b9eefba835e1dec3b7f233a98c89af31c07
2439+
languageName: node
2440+
linkType: hard
2441+
24302442
"@vue/compiler-dom@npm:3.2.23":
24312443
version: 3.2.23
24322444
resolution: "@vue/compiler-dom@npm:3.2.23"
@@ -2447,6 +2459,16 @@ __metadata:
24472459
languageName: node
24482460
linkType: hard
24492461

2462+
"@vue/compiler-dom@npm:3.2.45":
2463+
version: 3.2.45
2464+
resolution: "@vue/compiler-dom@npm:3.2.45"
2465+
dependencies:
2466+
"@vue/compiler-core": 3.2.45
2467+
"@vue/shared": 3.2.45
2468+
checksum: 89115538635f0da9cce615de5488d2759256fa573976a09a049536dbb94e9b5086b46f2f11e743cf0a7b14837161b3191c67611e0493054a5d4c4b96a322c901
2469+
languageName: node
2470+
linkType: hard
2471+
24502472
"@vue/compiler-sfc@npm:3.2.23":
24512473
version: 3.2.23
24522474
resolution: "@vue/compiler-sfc@npm:3.2.23"
@@ -2483,6 +2505,24 @@ __metadata:
24832505
languageName: node
24842506
linkType: hard
24852507

2508+
"@vue/compiler-sfc@npm:^3.2.45":
2509+
version: 3.2.45
2510+
resolution: "@vue/compiler-sfc@npm:3.2.45"
2511+
dependencies:
2512+
"@babel/parser": ^7.16.4
2513+
"@vue/compiler-core": 3.2.45
2514+
"@vue/compiler-dom": 3.2.45
2515+
"@vue/compiler-ssr": 3.2.45
2516+
"@vue/reactivity-transform": 3.2.45
2517+
"@vue/shared": 3.2.45
2518+
estree-walker: ^2.0.2
2519+
magic-string: ^0.25.7
2520+
postcss: ^8.1.10
2521+
source-map: ^0.6.1
2522+
checksum: bec375faa0012e953dc0887482cc01d52003ad424b6a8a9c8a2506fd4f0197ad62be22f77ce5691c2306068ae7bc0028399f25399e7d4beee668285d431f4d8f
2523+
languageName: node
2524+
linkType: hard
2525+
24862526
"@vue/compiler-ssr@npm:3.2.23":
24872527
version: 3.2.23
24882528
resolution: "@vue/compiler-ssr@npm:3.2.23"
@@ -2503,6 +2543,16 @@ __metadata:
25032543
languageName: node
25042544
linkType: hard
25052545

2546+
"@vue/compiler-ssr@npm:3.2.45":
2547+
version: 3.2.45
2548+
resolution: "@vue/compiler-ssr@npm:3.2.45"
2549+
dependencies:
2550+
"@vue/compiler-dom": 3.2.45
2551+
"@vue/shared": 3.2.45
2552+
checksum: 830c475506d2b6d1a6872b3fde1024ef5132f725121fd9c34832c5cefcc8cfddf0dcaa3acc9b2da4754162fccdff48b3275b9ff31415a7793b224c04355dc632
2553+
languageName: node
2554+
linkType: hard
2555+
25062556
"@vue/component-compiler-utils@npm:^3.1.0":
25072557
version: 3.3.0
25082558
resolution: "@vue/component-compiler-utils@npm:3.3.0"
@@ -2543,6 +2593,19 @@ __metadata:
25432593
languageName: node
25442594
linkType: hard
25452595

2596+
"@vue/reactivity-transform@npm:3.2.45":
2597+
version: 3.2.45
2598+
resolution: "@vue/reactivity-transform@npm:3.2.45"
2599+
dependencies:
2600+
"@babel/parser": ^7.16.4
2601+
"@vue/compiler-core": 3.2.45
2602+
"@vue/shared": 3.2.45
2603+
estree-walker: ^2.0.2
2604+
magic-string: ^0.25.7
2605+
checksum: 401040818947eb04c782487a7861d3ba20f95c9f3ca14282b3d7624002bfe6000547bb48c561afe87ae6d302143fec71a7e0bc3ed3ae2bfad8a228adf7fd90d6
2606+
languageName: node
2607+
linkType: hard
2608+
25462609
"@vue/reactivity@npm:3.2.23":
25472610
version: 3.2.23
25482611
resolution: "@vue/reactivity@npm:3.2.23"
@@ -2612,6 +2675,13 @@ __metadata:
26122675
languageName: node
26132676
linkType: hard
26142677

2678+
"@vue/shared@npm:3.2.45":
2679+
version: 3.2.45
2680+
resolution: "@vue/shared@npm:3.2.45"
2681+
checksum: ff3205056caed2a965aa0980e21319515ce13c859a9b269fdab0ee8b3c9f3d8eec7eefdb7fd6c6b47c12acdc7bf23c6c187b6191054221b4a29108139b20c221
2682+
languageName: node
2683+
linkType: hard
2684+
26152685
"@webassemblyjs/ast@npm:1.11.1":
26162686
version: 1.11.1
26172687
resolution: "@webassemblyjs/ast@npm:1.11.1"
@@ -11324,6 +11394,13 @@ __metadata:
1132411394
languageName: node
1132511395
linkType: hard
1132611396

11397+
"pathe@npm:^0.3.9":
11398+
version: 0.3.9
11399+
resolution: "pathe@npm:0.3.9"
11400+
checksum: 9afcbaa79c5f8ec603b6b0a20b9accfcec8de57e26738f4a844de4625cfb07cc733b7234387ef42c7ab23a49b91846b6b51cb247584793842a3179539af463df
11401+
languageName: node
11402+
linkType: hard
11403+
1132711404
"pbkdf2@npm:^3.0.3":
1132811405
version: 3.1.2
1132911406
resolution: "pbkdf2@npm:3.1.2"

0 commit comments

Comments
 (0)