Skip to content

Commit bc0c207

Browse files
authored
fix: lazy hook filter should work (#21443)
1 parent f8bc91a commit bc0c207

5 files changed

Lines changed: 35 additions & 17 deletions

File tree

packages/vite/src/node/build.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,10 +1292,8 @@ function wrapEnvironmentResolveId(
12921292
}
12931293

12941294
if ('handler' in hook) {
1295-
return {
1296-
...hook,
1297-
handler,
1298-
} as Plugin['resolveId']
1295+
hook.handler = handler
1296+
return hook
12991297
} else {
13001298
return handler
13011299
}
@@ -1318,10 +1316,8 @@ function wrapEnvironmentLoad(
13181316
}
13191317

13201318
if ('handler' in hook) {
1321-
return {
1322-
...hook,
1323-
handler,
1324-
} as Plugin['load']
1319+
hook.handler = handler
1320+
return hook
13251321
} else {
13261322
return handler
13271323
}
@@ -1345,10 +1341,8 @@ function wrapEnvironmentTransform(
13451341
}
13461342

13471343
if ('handler' in hook) {
1348-
return {
1349-
...hook,
1350-
handler,
1351-
} as Plugin['transform']
1344+
hook.handler = handler
1345+
return hook
13521346
} else {
13531347
return handler
13541348
}
@@ -1388,10 +1382,8 @@ function wrapEnvironmentHook<HookName extends keyof Plugin>(
13881382
}
13891383

13901384
if ('handler' in hook) {
1391-
return {
1392-
...hook,
1393-
handler,
1394-
} as Plugin[HookName]
1385+
hook.handler = handler
1386+
return hook
13951387
} else {
13961388
return handler
13971389
}

playground/transform-plugin/__tests__/transform-plugin.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ test('module type should be supported', async () => {
99
expect(await page.textContent('#module-type-json-virtual-post')).toBe('js')
1010
})
1111

12+
test('lazy hook filter should be applied', async () => {
13+
expect(await page.textContent('#lazy-hook-filter')).toBe('success')
14+
})
15+
1216
tests()

playground/transform-plugin/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ <h2>Module Type</h2>
99
<div id="module-type-json-virtual-pre"></div>
1010
<div id="module-type-json-virtual-post"></div>
1111

12+
<h2>Lazy Hook Filter</h2>
13+
<div id="lazy-hook-filter"></div>
14+
1215
<script type="module" src="./index.js"></script>

playground/transform-plugin/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ document.getElementById('module-type-json-virtual-pre').innerHTML =
1313
barJson.moduleTypePre
1414
document.getElementById('module-type-json-virtual-post').innerHTML =
1515
barJson.moduleTypePost
16+
17+
// 'LAZY_HOOK_FILTER_CONTENT' is replaced by lazy-hook-filter plugin
18+
document.getElementById('lazy-hook-filter').innerHTML =
19+
'LAZY_HOOK_FILTER_CONTENT'

playground/transform-plugin/vite.config.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ const moduleTypePlugins = [
5959
},
6060
]
6161

62+
const lazyHookFilterPlugin = {
63+
name: 'lazy-hook-filter',
64+
options() {
65+
lazyHookFilterPlugin.transform.filter = { id: '**/index.js' }
66+
},
67+
transform: {
68+
filter: /** @type {import('vite').Rolldown.HookFilter} */ ({
69+
id: { exclude: ['**/*.js'] },
70+
}),
71+
handler(code) {
72+
return code.replaceAll('LAZY_HOOK_FILTER_CONTENT', 'success')
73+
},
74+
},
75+
}
76+
6277
export default defineConfig({
63-
plugins: [transformPlugin, moduleTypePlugins],
78+
plugins: [transformPlugin, moduleTypePlugins, lazyHookFilterPlugin],
6479
})

0 commit comments

Comments
 (0)