@@ -17,7 +17,8 @@ import {
17
17
generateJSON ,
18
18
generateYAML ,
19
19
checkInstallPackage ,
20
- checkVueI18nBridgeInstallPackage
20
+ checkVueI18nBridgeInstallPackage ,
21
+ getVueI18nVersion
21
22
} from '@intlify/bundle-utils'
22
23
import { RawSourceMap } from 'source-map'
23
24
import { parse } from '@vue/compiler-sfc'
@@ -38,6 +39,7 @@ const debug = createDebug('unplugin-vue-i18n')
38
39
39
40
const installedPkg = checkInstallPackage ( '@intlify/unplugin-vue-i18n' , debug )
40
41
const installedVueI18nBridge = checkVueI18nBridgeInstallPackage ( debug )
42
+ const vueI18nVersion = getVueI18nVersion ( debug )
41
43
42
44
export const unplugin = createUnplugin < PluginOptions > ( ( options = { } , meta ) => {
43
45
debug ( 'plugin options:' , options , meta . framework )
@@ -99,20 +101,29 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
99
101
debug ( 'useVueI18nImportName' , useVueI18nImportName )
100
102
101
103
// 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
107
108
? '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`
109
118
110
119
const esm = isBoolean ( options . esm ) ? options . esm : true
111
120
debug ( 'esm' , esm )
112
121
113
122
let isProduction = false
114
123
let sourceMap = false
115
124
125
+ const vueI18nAliasName = getVueI18nAliasName ( )
126
+
116
127
return {
117
128
name : 'unplugin-vue-i18n' ,
118
129
@@ -132,23 +143,40 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
132
143
config . resolve ,
133
144
meta . framework
134
145
)
146
+
135
147
if ( command === 'build' && runtimeOnly ) {
136
- const aliasName = getAliasName ( )
137
- debug ( `alias name: ${ aliasName } ` )
148
+ debug ( `vue-i18n alias name: ${ vueI18nAliasName } ` )
138
149
if ( isArray ( config . resolve ! . alias ) ) {
139
150
config . resolve ! . alias . push ( {
140
- find : aliasName ,
141
- replacement : ` ${ aliasName } /dist/ ${ aliasName } .runtime.esm-bundler.js`
151
+ find : vueI18nAliasName ,
152
+ replacement : getVueI18nAliasPath ( vueI18nAliasName )
142
153
} )
154
+ if ( installedVueI18nBridge ) {
155
+ config . resolve ! . alias . push ( {
156
+ find : 'vue-i18n-bridge' ,
157
+ replacement : getVueI18nBridgeAliasPath ( )
158
+ } )
159
+ }
143
160
} else if ( isObject ( config . resolve ! . alias ) ) {
144
161
// 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
+ }
148
169
}
149
170
debug (
150
- `set ${ aliasName } runtime only: ${ aliasName } /dist/${ aliasName } .runtime.esm-bundler.js`
171
+ `set ${ vueI18nAliasName } runtime only: ${ getVueI18nAliasPath (
172
+ vueI18nAliasName
173
+ ) } `
151
174
)
175
+ if ( installedVueI18nBridge ) {
176
+ debug (
177
+ `set vue-i18n-bridge runtime only: ${ getVueI18nBridgeAliasPath ( ) } `
178
+ )
179
+ }
152
180
} else if (
153
181
command === 'serve' &&
154
182
installedPkg === 'petite-vue-i18n' &&
@@ -160,16 +188,16 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
160
188
)
161
189
if ( isArray ( config . resolve ! . alias ) ) {
162
190
config . resolve ! . alias . push ( {
163
- find : 'vue-i18n' ,
191
+ find : vueI18nAliasName ,
164
192
replacement : `petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`
165
193
} )
166
194
} else {
167
195
// eslint-disable-next-line @typescript-eslint/no-explicit-any
168
196
; ( config . resolve ! . alias as any ) [
169
- 'vue-i18n'
197
+ vueI18nAliasName
170
198
] = `petite-vue-i18n/dist/petite-vue-i18n.esm-bundler.js`
171
199
}
172
- debug ( `alias name: ${ getAliasName ( ) } ` )
200
+ debug ( `petite-vue-i18n alias name: ${ vueI18nAliasName } ` )
173
201
}
174
202
175
203
config . define = config . define || { }
@@ -239,15 +267,40 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
239
267
sourceMap = ! ! compiler . options . devtool
240
268
debug ( `webpack: isProduction = ${ isProduction } , sourceMap = ${ sourceMap } ` )
241
269
270
+ compiler . options . resolve = normalizeConfigResolveAlias (
271
+ compiler . options . resolve ,
272
+ meta . framework
273
+ )
274
+
242
275
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
+ ) } `
246
288
)
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
+ ) {
247
299
// eslint-disable-next-line @typescript-eslint/no-explicit-any
248
300
; ( 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 } ` )
251
304
}
252
305
253
306
loadWebpack ( ) . then ( webpack => {
@@ -274,7 +327,9 @@ export const unplugin = createUnplugin<PluginOptions>((options = {}, meta) => {
274
327
compiler . options . module . rules . push ( {
275
328
test : / \. ( j s o n 5 ? | y a ? m l ) $ / ,
276
329
type : 'javascript/auto' ,
277
- exclude : include // exclude target i18n resources
330
+ include ( resource : string ) {
331
+ return filter ( resource )
332
+ }
278
333
} )
279
334
}
280
335
0 commit comments