Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions packages/unplugin-vue-i18n/src/core/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ import { createHash } from 'node:crypto'
import fs from 'node:fs'
import { dirname, parse as parsePath, resolve } from 'node:path'
import { parse } from 'vue/compiler-sfc'
import { checkVuePlugin, error, getVitePlugin, raiseError, resolveNamespace, warn } from '../utils'
import {
checkVuePlugin,
error,
getVitePlugin,
getVitePluginTransform,
overrideVitePluginTransform,
raiseError,
resolveNamespace,
warn
} from '../utils'
import { getVueCompiler, parseVueRequest } from '../vue'

import type { CodeGenOptions } from '@intlify/bundle-utils'
Expand Down Expand Up @@ -168,12 +177,23 @@ export function resourcePlugin(opts: ResolvedOptions, meta: UnpluginContextMeta)
ctx.sourceMap = config.command === 'build' ? !!config.build.sourcemap : false
debug(`configResolved: isProduction = ${ctx.prod}, sourceMap = ${ctx.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) {
// backup @rollup/plugin-json
const orgTransform = jsonPlugin.transform
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
}
Expand All @@ -195,6 +215,9 @@ export function resourcePlugin(opts: ResolvedOptions, meta: UnpluginContextMeta)
// @ts-expect-error
return orgTransform!.apply(this, [code, id])
}

// override `vite:json` plugin transform function
overrideVitePluginTransform(jsonPlugin, overrideViteJsonPlugin, transformWay!)
Copy link

Copilot AI Apr 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a non-null assertion on transformWay, consider validating its presence before overriding to ensure more robust error handling.

Suggested change
overrideVitePluginTransform(jsonPlugin, overrideViteJsonPlugin, transformWay!)
overrideVitePluginTransform(jsonPlugin, overrideViteJsonPlugin, transformWay)

Copilot uses AI. Check for mistakes.
}
}
},
Expand Down
28 changes: 28 additions & 0 deletions packages/unplugin-vue-i18n/src/utils/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@ export function getVitePlugin(config: UserConfig, name: string): RollupPlugin |
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'
Comment on lines +30 to +34
Copy link

Copilot AI Apr 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Refine the type for the override parameter to improve type safety and prevent potential runtime issues.

Suggested change
// TODO: `override` type, we need more strict type
export function overrideVitePluginTransform(
plugin: RollupPlugin,
override: Function,
to: 'handler' | 'transform'
// TODO: Refined `override` type for stricter type safety
export function overrideVitePluginTransform<
T extends 'handler' | 'transform'
>(
plugin: RollupPlugin,
override: T extends 'handler'
? typeof plugin.transform extends { handler: infer H }
? H
: never
: typeof plugin.transform,
to: T

Copilot uses AI. Check for mistakes.
): 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(
Expand Down