From 584e7e85e1f47ba02b8ac6ca99135f148f9486ac Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Mon, 19 Jan 2026 15:25:20 +0900 Subject: [PATCH] fix: lazy hook filter should work --- packages/vite/src/node/build.ts | 24 +++++++------------ .../__tests__/transform-plugin.spec.ts | 4 ++++ playground/transform-plugin/index.html | 3 +++ playground/transform-plugin/index.js | 4 ++++ playground/transform-plugin/vite.config.js | 17 ++++++++++++- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 9be1e7bfd715ef..d3131a054f77b4 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -1292,10 +1292,8 @@ function wrapEnvironmentResolveId( } if ('handler' in hook) { - return { - ...hook, - handler, - } as Plugin['resolveId'] + hook.handler = handler + return hook } else { return handler } @@ -1318,10 +1316,8 @@ function wrapEnvironmentLoad( } if ('handler' in hook) { - return { - ...hook, - handler, - } as Plugin['load'] + hook.handler = handler + return hook } else { return handler } @@ -1345,10 +1341,8 @@ function wrapEnvironmentTransform( } if ('handler' in hook) { - return { - ...hook, - handler, - } as Plugin['transform'] + hook.handler = handler + return hook } else { return handler } @@ -1388,10 +1382,8 @@ function wrapEnvironmentHook( } if ('handler' in hook) { - return { - ...hook, - handler, - } as Plugin[HookName] + hook.handler = handler + return hook } else { return handler } diff --git a/playground/transform-plugin/__tests__/transform-plugin.spec.ts b/playground/transform-plugin/__tests__/transform-plugin.spec.ts index c706df3c6bcd1d..519f3112d8aea6 100644 --- a/playground/transform-plugin/__tests__/transform-plugin.spec.ts +++ b/playground/transform-plugin/__tests__/transform-plugin.spec.ts @@ -9,4 +9,8 @@ test('module type should be supported', async () => { expect(await page.textContent('#module-type-json-virtual-post')).toBe('js') }) +test('lazy hook filter should be applied', async () => { + expect(await page.textContent('#lazy-hook-filter')).toBe('success') +}) + tests() diff --git a/playground/transform-plugin/index.html b/playground/transform-plugin/index.html index b6d8bce8952ae1..8d108bd2a8ec4a 100644 --- a/playground/transform-plugin/index.html +++ b/playground/transform-plugin/index.html @@ -9,4 +9,7 @@

Module Type

+

Lazy Hook Filter

+
+ diff --git a/playground/transform-plugin/index.js b/playground/transform-plugin/index.js index bb7d910bd88c87..18e9010a94ec2a 100644 --- a/playground/transform-plugin/index.js +++ b/playground/transform-plugin/index.js @@ -13,3 +13,7 @@ document.getElementById('module-type-json-virtual-pre').innerHTML = barJson.moduleTypePre document.getElementById('module-type-json-virtual-post').innerHTML = barJson.moduleTypePost + +// 'LAZY_HOOK_FILTER_CONTENT' is replaced by lazy-hook-filter plugin +document.getElementById('lazy-hook-filter').innerHTML = + 'LAZY_HOOK_FILTER_CONTENT' diff --git a/playground/transform-plugin/vite.config.js b/playground/transform-plugin/vite.config.js index 719f052b5595a2..20c336b0b3cfef 100644 --- a/playground/transform-plugin/vite.config.js +++ b/playground/transform-plugin/vite.config.js @@ -59,6 +59,21 @@ const moduleTypePlugins = [ }, ] +const lazyHookFilterPlugin = { + name: 'lazy-hook-filter', + options() { + lazyHookFilterPlugin.transform.filter = { id: '**/index.js' } + }, + transform: { + filter: /** @type {import('vite').Rolldown.HookFilter} */ ({ + id: { exclude: ['**/*.js'] }, + }), + handler(code) { + return code.replaceAll('LAZY_HOOK_FILTER_CONTENT', 'success') + }, + }, +} + export default defineConfig({ - plugins: [transformPlugin, moduleTypePlugins], + plugins: [transformPlugin, moduleTypePlugins, lazyHookFilterPlugin], })