Skip to content

Commit b8900e3

Browse files
authored
fix(unplugin-vue-i18n): Support for transform.handler in vite:json plugin (#485)
1 parent 44f2550 commit b8900e3

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
checkVuePlugin,
2222
error,
2323
getVitePlugin,
24+
getVitePluginTransform,
25+
overrideVitePluginTransform,
2426
raiseError,
2527
resolveNamespace,
2628
warn
@@ -136,11 +138,24 @@ export function resourcePlugin(
136138
`configResolved: isProduction = ${isProduction}, sourceMap = ${sourceMap}`
137139
)
138140

141+
/**
142+
* NOTE:
143+
* For the native rolldown plugin, we need to change to another solution from the current workaround.
144+
* Currently, the rolldown team and vite team are discussing this issue.
145+
* https://github.com/vitejs/rolldown-vite/issues/120
146+
*/
147+
139148
// json transform handling
140149
const jsonPlugin = getVitePlugin(config, 'vite:json')
141150
if (jsonPlugin) {
142-
const orgTransform = jsonPlugin.transform // backup @rollup/plugin-json
143-
jsonPlugin.transform = async function (code: string, id: string) {
151+
// saving `vite:json` plugin instance
152+
const [orgTransform, transformWay] =
153+
getVitePluginTransform(jsonPlugin)
154+
if (!orgTransform) {
155+
throw new Error('vite:json plugin not found!')
156+
}
157+
158+
async function overrideViteJsonPlugin(code: string, id: string) {
144159
if (!/\.json$/.test(id) || filter(id)) {
145160
return
146161
}
@@ -162,6 +177,13 @@ export function resourcePlugin(
162177
// @ts-expect-error
163178
return orgTransform!.apply(this, [code, id])
164179
}
180+
181+
// override `vite:json` plugin transform function
182+
overrideVitePluginTransform(
183+
jsonPlugin,
184+
overrideViteJsonPlugin,
185+
transformWay!
186+
)
165187
}
166188

167189
/**

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,34 @@ export function getVitePlugin(
1919
return config.plugins.find(p => p.name === name) as RollupPlugin
2020
}
2121

22+
export function getVitePluginTransform(
23+
plugin: RollupPlugin
24+
): [RollupPlugin['transform'], 'handler' | 'transform' | undefined] {
25+
if (plugin.transform) {
26+
return 'handler' in plugin.transform
27+
? [plugin.transform.handler, 'handler']
28+
: [plugin.transform, 'transform']
29+
} else {
30+
return [undefined, undefined]
31+
}
32+
}
33+
34+
// TODO: `override` type, we need more strict type
35+
export function overrideVitePluginTransform(
36+
plugin: RollupPlugin,
37+
override: Function,
38+
to: 'handler' | 'transform'
39+
): void {
40+
if (plugin.transform == undefined) {
41+
throw new Error('plugin.transform is undefined')
42+
}
43+
if (to === 'handler' && 'handler' in plugin.transform) {
44+
plugin.transform.handler = override as typeof plugin.transform.handler
45+
} else {
46+
plugin.transform = override as typeof plugin.transform
47+
}
48+
}
49+
2250
export function checkVuePlugin(vuePlugin: RollupPlugin | null): boolean {
2351
if (vuePlugin == null || !vuePlugin.api) {
2452
error(

0 commit comments

Comments
 (0)