diff --git a/packages/unplugin-vue-i18n/src/core/resource.ts b/packages/unplugin-vue-i18n/src/core/resource.ts index 94426d62..0bed0f07 100644 --- a/packages/unplugin-vue-i18n/src/core/resource.ts +++ b/packages/unplugin-vue-i18n/src/core/resource.ts @@ -21,6 +21,8 @@ import { checkVuePlugin, error, getVitePlugin, + getVitePluginTransform, + overrideVitePluginTransform, raiseError, resolveNamespace, warn @@ -136,11 +138,24 @@ export function resourcePlugin( `configResolved: isProduction = ${isProduction}, sourceMap = ${sourceMap}` ) + /** + * NOTE: + * For the native rolldown plugin, we need to change to another solution from the current workaround. + * Currently, the rolldown team and vite team are discussing this issue. + * https://github.com/vitejs/rolldown-vite/issues/120 + */ + // json transform handling const jsonPlugin = getVitePlugin(config, 'vite:json') if (jsonPlugin) { - const orgTransform = jsonPlugin.transform // backup @rollup/plugin-json - jsonPlugin.transform = async function (code: string, id: string) { + // saving `vite:json` plugin instance + const [orgTransform, transformWay] = + getVitePluginTransform(jsonPlugin) + if (!orgTransform) { + throw new Error('vite:json plugin not found!') + } + + async function overrideViteJsonPlugin(code: string, id: string) { if (!/\.json$/.test(id) || filter(id)) { return } @@ -162,6 +177,13 @@ export function resourcePlugin( // @ts-expect-error return orgTransform!.apply(this, [code, id]) } + + // override `vite:json` plugin transform function + overrideVitePluginTransform( + jsonPlugin, + overrideViteJsonPlugin, + transformWay! + ) } /** diff --git a/packages/unplugin-vue-i18n/src/utils/plugin.ts b/packages/unplugin-vue-i18n/src/utils/plugin.ts index 254f8d3f..b5a83253 100644 --- a/packages/unplugin-vue-i18n/src/utils/plugin.ts +++ b/packages/unplugin-vue-i18n/src/utils/plugin.ts @@ -19,6 +19,34 @@ export function getVitePlugin( return config.plugins.find(p => p.name === name) as RollupPlugin } +export function getVitePluginTransform( + plugin: RollupPlugin +): [RollupPlugin['transform'], 'handler' | 'transform' | undefined] { + if (plugin.transform) { + return 'handler' in plugin.transform + ? [plugin.transform.handler, 'handler'] + : [plugin.transform, 'transform'] + } else { + return [undefined, undefined] + } +} + +// TODO: `override` type, we need more strict type +export function overrideVitePluginTransform( + plugin: RollupPlugin, + override: Function, + to: 'handler' | 'transform' +): void { + if (plugin.transform == undefined) { + throw new Error('plugin.transform is undefined') + } + if (to === 'handler' && 'handler' in plugin.transform) { + plugin.transform.handler = override as typeof plugin.transform.handler + } else { + plugin.transform = override as typeof plugin.transform + } +} + export function checkVuePlugin(vuePlugin: RollupPlugin | null): boolean { if (vuePlugin == null || !vuePlugin.api) { error(