Skip to content

Commit 4c3c2b3

Browse files
committed
fix(unplugin-vue-i18n): detect vite:json from config.plugins instead of import(vite)
1 parent 06d3a29 commit 4c3c2b3

1 file changed

Lines changed: 38 additions & 50 deletions

File tree

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

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -179,65 +179,53 @@ export function resourcePlugin(
179179
sourceMap = config.command === 'build' ? !!config.build.sourcemap : false
180180
debug(`configResolved: isProduction = ${isProduction}, sourceMap = ${sourceMap}`)
181181

182-
// Check if we're using rolldown-vite
183-
const isRolldownVite = !!(await getViteModule()).rolldownVersion
184-
debug(`Using ${isRolldownVite ? 'rolldown-vite' : 'vite'} for the build`)
185-
186182
/**
187183
* NOTE(kazupon):
188-
* For the native rolldown plugin, we need to change to another solution from the current workaround.
189-
* Currently, the rolldown team and vite team are discussing this issue.
190-
* https://github.com/vitejs/rolldown-vite/issues/120
184+
* Override `vite:json` plugin transform to prevent it from processing
185+
* JSON files that unplugin-vue-i18n has already compiled.
186+
*
187+
* We detect the builder by checking whether `vite:json` exists in
188+
* `config.plugins` — rolldown-based Vite (v8+) uses
189+
* `builtin:vite-json` instead, so `getVitePlugin` returns null and
190+
* this block is naturally skipped. This is more reliable than
191+
* `import('vite').rolldownVersion` which can resolve to the wrong
192+
* copy in multi-vite setups (e.g. Nuxt 4 + UnoCSS).
193+
* ref: https://github.com/intlify/bundle-tools/issues/553
191194
*/
195+
const jsonPlugin = getVitePlugin(config, 'vite:json')
196+
if (jsonPlugin && jsonPlugin.transform) {
197+
const transform = jsonPlugin.transform
198+
const isObjectHook = typeof transform !== 'function' && 'handler' in transform
199+
const orgTransform = isObjectHook ? transform.handler : transform
200+
201+
async function overrideJson(this: unknown, code: string, id: string) {
202+
const filter = await getFilter()
203+
if (!/\.json$/.test(id) || filter(id)) {
204+
return
205+
}
192206

193-
// json transform handling for normal Vite (not rolldown-vite)
194-
if (!isRolldownVite) {
195-
const jsonPlugin = getVitePlugin(config, 'vite:json')
196-
if (jsonPlugin && jsonPlugin.transform) {
197207
/**
198208
* 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
209+
* `vite:json` plugin will be handled if the query generated from the result of parse SFC
210+
* with `vite:vue` plugin contains json as follows.
211+
* e.g src/components/HelloI18n.vue?vue&type=i18n&index=1&lang.json
212+
*
213+
* To avoid this, return the result that has already been processed (`enforce: 'pre'`) in the wrapped json plugin.
206214
*/
207-
const transform = jsonPlugin.transform
208-
const isObjectHook = typeof transform !== 'function' && 'handler' in transform
209-
const orgTransform = isObjectHook ? transform.handler : transform
210-
211-
// override json transform
212-
async function overrideJson(this: unknown, code: string, id: string) {
213-
const filter = await getFilter()
214-
if (!/\.json$/.test(id) || filter(id)) {
215-
return
216-
}
217-
218-
/**
219-
* NOTE(kazupon):
220-
* `vite:json` plugin will be handled if the query generated from the result of parse SFC
221-
* with `vite:vue` plugin contains json as follows.
222-
* e.g src/components/HelloI18n.vue?vue&type=i18n&index=1&lang.json
223-
*
224-
* To avoid this, return the result that has already been processed (`enforce: 'pre'`) in the wrapped json plugin.
225-
*/
226-
const { query } = parseVueRequest(id)
227-
if (query.vue) {
228-
return
229-
}
230-
231-
debug('org json plugin')
232-
// @ts-expect-error
233-
return orgTransform.apply(this, [code, id])
215+
const { query } = parseVueRequest(id)
216+
if (query.vue) {
217+
return
234218
}
235219

236-
if (isObjectHook) {
237-
transform.handler = overrideJson as typeof transform.handler
238-
} else {
239-
jsonPlugin.transform = overrideJson as typeof jsonPlugin.transform
240-
}
220+
debug('org json plugin')
221+
// @ts-expect-error
222+
return orgTransform.apply(this, [code, id])
223+
}
224+
225+
if (isObjectHook) {
226+
transform.handler = overrideJson as typeof transform.handler
227+
} else {
228+
jsonPlugin.transform = overrideJson as typeof jsonPlugin.transform
241229
}
242230
}
243231
},

0 commit comments

Comments
 (0)