Skip to content

Conversation

@kazupon
Copy link
Member

@kazupon kazupon commented Apr 20, 2025

Description

In vite 6.3, the handling of plugin hooks has been changed from transform to transform.handler internally.
Currently, vite is minimizing the impact on end users through a patch, but in vite 7, the patch for transform will be removed.

Linked Issues

Additional context

Rollup used internally in Vite will be replaced by Rolldown in the future. Rolldown plans to provide JSON transform plugin natively, but the current workaround that hijacks the unplugin-vue-i18n Vite JSON plugin cannot be used.
Regarding this issue, the Rolldown team and Vite team are currently discussing a workaround mechanism at the following URL.

vitejs/rolldown-vite#120

@pkg-pr-new
Copy link

pkg-pr-new bot commented Apr 20, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@intlify/bundle-utils@484
npm i https://pkg.pr.new/@intlify/unplugin-vue-i18n@484

commit: a21d865

@kazupon kazupon requested a review from Copilot April 20, 2025 03:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates the handling of the Vite JSON plugin to support the new transform.handler hook introduced in Vite 6.3, in preparation for Vite 7.

  • Introduces helper functions getVitePluginTransform and overrideVitePluginTransform to manage different transform hook types.
  • Updates the resource plugin to use the new transform override mechanism for the vite:json plugin.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/unplugin-vue-i18n/src/utils/plugin.ts Adds utility functions to extract and override the Vite plugin transform hook based on the new handler pattern.
packages/unplugin-vue-i18n/src/core/resource.ts Adapts the JSON transform handling in the resource plugin to leverage the new transform override utilities.

Comment on lines +30 to +34
// TODO: `override` type, we need more strict type
export function overrideVitePluginTransform(
plugin: RollupPlugin,
override: Function,
to: 'handler' | 'transform'
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.
}

// 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.
@kazupon kazupon added the improvement Includes backwards-compatible fixes label Apr 20, 2025
@kazupon kazupon merged commit 03e03a7 into main Apr 20, 2025
19 checks passed
@kazupon kazupon deleted the fix/vite-json-plugin-hijacking branch April 20, 2025 03:27
kazupon added a commit that referenced this pull request Apr 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Includes backwards-compatible fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants