Skip to content

Commit 4db5d44

Browse files
committed
fix(unplugin-vue-i18n): preserve vite:json ObjectHook shape for Vite 8 compatibility
1 parent a7b5d54 commit 4db5d44

3 files changed

Lines changed: 25 additions & 16 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
},
4646
"onlyBuiltDependencies": [
4747
"esbuild",
48-
"oxc-resolver"
48+
"oxc-resolver",
49+
"unrs-resolver"
4950
]
5051
},
5152
"license": "MIT",

packages/unplugin-vue-i18n/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
},
1212
"peerDependencies": {
1313
"petite-vue-i18n": "*",
14+
"vite": "^6.0.0 || ^7.0.0 || ^8.0.0",
1415
"vue": "^3.2.25",
1516
"vue-i18n": "*"
1617
},
1718
"peerDependenciesMeta": {
1819
"petite-vue-i18n": {
1920
"optional": true
2021
},
22+
"vite": {
23+
"optional": true
24+
},
2125
"vue-i18n": {
2226
"optional": true
2327
}

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,23 @@ export function resourcePlugin(
193193
// json transform handling for normal Vite (not rolldown-vite)
194194
if (!isRolldownVite) {
195195
const jsonPlugin = getVitePlugin(config, 'vite:json')
196-
if (jsonPlugin) {
197-
// saving `vite:json` plugin instance
198-
const orgTransform =
199-
'handler' in jsonPlugin.transform!
200-
? jsonPlugin.transform!.handler
201-
: jsonPlugin.transform!
196+
if (jsonPlugin && jsonPlugin.transform) {
197+
/**
198+
* NOTE(kazupon):
199+
* Detect the hook shape BEFORE mutation. Vite 8 changed `transform`
200+
* to an `ObjectHook` (`{ handler, filter, order, ... }`), while
201+
* earlier versions use a plain function. We must preserve the
202+
* ObjectHook shape so that `filter`/`order` are not lost; otherwise
203+
* Vite 8's `vite:json` will run on already-compiled JS output.
204+
* ref: https://github.com/intlify/bundle-tools/issues/553
205+
* ref: https://github.com/vitejs/vite/pull/19878/files#diff-2cfbd4f4d8c32727cd8e1a561cffbde0b384a3ce0789340440e144f9d64c10f6R1086-R1088
206+
*/
207+
const transform = jsonPlugin.transform
208+
const isObjectHook = typeof transform !== 'function' && 'handler' in transform
209+
const orgTransform = isObjectHook ? transform.handler : transform
202210

203211
// override json transform
204-
async function overrideJson(code: string, id: string) {
212+
async function overrideJson(this: unknown, code: string, id: string) {
205213
const filter = await getFilter()
206214
if (!/\.json$/.test(id) || filter(id)) {
207215
return
@@ -225,14 +233,10 @@ export function resourcePlugin(
225233
return orgTransform.apply(this, [code, id])
226234
}
227235

228-
/**
229-
* NOTE(kazupon):
230-
* We need to override the transform function of the `vite:json` plugin for `transform` and `transform.handler`.
231-
* ref: https://github.com/vitejs/vite/pull/19878/files#diff-2cfbd4f4d8c32727cd8e1a561cffbde0b384a3ce0789340440e144f9d64c10f6R1086-R1088
232-
*/
233-
jsonPlugin.transform = overrideJson
234-
if ('handler' in jsonPlugin.transform!) {
235-
jsonPlugin.transform.handler = overrideJson
236+
if (isObjectHook) {
237+
transform.handler = overrideJson as typeof transform.handler
238+
} else {
239+
jsonPlugin.transform = overrideJson as typeof jsonPlugin.transform
236240
}
237241
}
238242
}

0 commit comments

Comments
 (0)