Skip to content

Commit 1db52bf

Browse files
authoredNov 30, 2022
feat: align object interface for transformIndexHtml hook (#9669)
1 parent f4c1264 commit 1db52bf

File tree

5 files changed

+60
-20
lines changed

5 files changed

+60
-20
lines changed
 

‎docs/guide/api-plugin.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo
329329

330330
### `transformIndexHtml`
331331

332-
- **Type:** `IndexHtmlTransformHook | { enforce?: 'pre' | 'post', transform: IndexHtmlTransformHook }`
332+
- **Type:** `IndexHtmlTransformHook | { order?: 'pre' | 'post', handler: IndexHtmlTransformHook }`
333333
- **Kind:** `async`, `sequential`
334334

335335
Dedicated hook for transforming HTML entry point files such as `index.html`. The hook receives the current HTML string and a transform context. The context exposes the [`ViteDevServer`](./api-javascript#vitedevserver) instance during dev, and exposes the Rollup output bundle during build.

‎packages/vite/src/node/plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export interface Plugin extends RollupPlugin {
112112
*
113113
* By default the transform is applied **after** vite's internal html
114114
* transform. If you need to apply the transform before vite, use an object:
115-
* `{ enforce: 'pre', transform: hook }`
115+
* `{ order: 'pre', handler: hook }`
116116
*/
117117
transformIndexHtml?: IndexHtmlTransform
118118
/**

‎packages/vite/src/node/plugins/html.ts

+52-15
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ function handleParseError(
282282
* Compiles index.html into an entry js module
283283
*/
284284
export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
285-
const [preHooks, postHooks] = resolveHtmlTransforms(config.plugins)
285+
const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(
286+
config.plugins
287+
)
286288
preHooks.unshift(preImportMapHook(config))
287289
postHooks.push(postImportMapHook())
288290
const processedHtml = new Map<string, string>()
@@ -786,12 +788,16 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
786788
if (s) {
787789
result = s.toString()
788790
}
789-
result = await applyHtmlTransforms(result, postHooks, {
790-
path: '/' + relativeUrlPath,
791-
filename: id,
792-
bundle,
793-
chunk
794-
})
791+
result = await applyHtmlTransforms(
792+
result,
793+
[...normalHooks, ...postHooks],
794+
{
795+
path: '/' + relativeUrlPath,
796+
filename: id,
797+
bundle,
798+
chunk
799+
}
800+
)
795801
// resolve asset url references
796802
result = result.replace(assetUrlRE, (_, fileHash, postfix = '') => {
797803
return toOutputAssetFilePath(this.getFileName(fileHash)) + postfix
@@ -863,9 +869,24 @@ export type IndexHtmlTransformHook = (
863869
export type IndexHtmlTransform =
864870
| IndexHtmlTransformHook
865871
| {
872+
order?: 'pre' | 'post' | null
873+
/**
874+
* @deprecated renamed to `order`
875+
*/
866876
enforce?: 'pre' | 'post'
877+
/**
878+
* @deprecated renamed to `handler`
879+
*/
867880
transform: IndexHtmlTransformHook
868881
}
882+
| {
883+
order?: 'pre' | 'post' | null
884+
/**
885+
* @deprecated renamed to `order`
886+
*/
887+
enforce?: 'pre' | 'post'
888+
handler: IndexHtmlTransformHook
889+
}
869890

870891
export function preImportMapHook(
871892
config: ResolvedConfig
@@ -914,24 +935,40 @@ export function postImportMapHook(): IndexHtmlTransformHook {
914935

915936
export function resolveHtmlTransforms(
916937
plugins: readonly Plugin[]
917-
): [IndexHtmlTransformHook[], IndexHtmlTransformHook[]] {
938+
): [
939+
IndexHtmlTransformHook[],
940+
IndexHtmlTransformHook[],
941+
IndexHtmlTransformHook[]
942+
] {
918943
const preHooks: IndexHtmlTransformHook[] = []
944+
const normalHooks: IndexHtmlTransformHook[] = []
919945
const postHooks: IndexHtmlTransformHook[] = []
920946

921947
for (const plugin of plugins) {
922948
const hook = plugin.transformIndexHtml
923-
if (hook) {
924-
if (typeof hook === 'function') {
925-
postHooks.push(hook)
926-
} else if (hook.enforce === 'pre') {
927-
preHooks.push(hook.transform)
949+
if (!hook) continue
950+
951+
if (typeof hook === 'function') {
952+
normalHooks.push(hook)
953+
} else {
954+
// `enforce` had only two possible values for the `transformIndexHtml` hook
955+
// `'pre'` and `'post'` (the default). `order` now works with three values
956+
// to align with other hooks (`'pre'`, normal, and `'post'`). We map
957+
// both `enforce: 'post'` to `order: undefined` to avoid a breaking change
958+
const order = hook.order ?? (hook.enforce === 'pre' ? 'pre' : undefined)
959+
// @ts-expect-error union type
960+
const handler = hook.handler ?? hook.transform
961+
if (order === 'pre') {
962+
preHooks.push(handler)
963+
} else if (order === 'post') {
964+
postHooks.push(handler)
928965
} else {
929-
postHooks.push(hook.transform)
966+
normalHooks.push(handler)
930967
}
931968
}
932969
}
933970

934-
return [preHooks, postHooks]
971+
return [preHooks, normalHooks, postHooks]
935972
}
936973

937974
export async function applyHtmlTransforms(

‎packages/vite/src/node/server/middlewares/indexHtml.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,17 @@ interface AssetNode {
4242
export function createDevHtmlTransformFn(
4343
server: ViteDevServer
4444
): (url: string, html: string, originalUrl: string) => Promise<string> {
45-
const [preHooks, postHooks] = resolveHtmlTransforms(server.config.plugins)
45+
const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(
46+
server.config.plugins
47+
)
4648
return (url: string, html: string, originalUrl: string): Promise<string> => {
4749
return applyHtmlTransforms(
4850
html,
4951
[
5052
preImportMapHook(server.config),
5153
...preHooks,
5254
devHtmlHook,
55+
...normalHooks,
5356
...postHooks,
5457
postImportMapHook()
5558
],

‎playground/html/vite.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ module.exports = {
3636
{
3737
name: 'pre-transform',
3838
transformIndexHtml: {
39-
enforce: 'pre',
40-
transform(html, { filename }) {
39+
order: 'pre',
40+
handler(html, { filename }) {
4141
if (html.includes('/@vite/client')) {
4242
throw new Error('pre transform applied at wrong time!')
4343
}

0 commit comments

Comments
 (0)
Please sign in to comment.