Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: align object interface for transformIndexHtml hook #9669

Merged
merged 6 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion docs/guide/api-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo

### `transformIndexHtml`

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

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.
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export interface Plugin extends RollupPlugin {
*
* By default the transform is applied **after** vite's internal html
* transform. If you need to apply the transform before vite, use an object:
* `{ enforce: 'pre', transform: hook }`
* `{ order: 'pre', handler: hook }`
*/
transformIndexHtml?: IndexHtmlTransform
/**
Expand Down
63 changes: 48 additions & 15 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ function handleParseError(
* Compiles index.html into an entry js module
*/
export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
const [preHooks, postHooks] = resolveHtmlTransforms(config.plugins)
const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(
config.plugins
)
preHooks.unshift(preImportMapHook(config))
postHooks.push(postImportMapHook())
const processedHtml = new Map<string, string>()
Expand Down Expand Up @@ -721,12 +723,16 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
if (s) {
result = s.toString()
}
result = await applyHtmlTransforms(result, postHooks, {
path: '/' + relativeUrlPath,
filename: id,
bundle,
chunk
})
result = await applyHtmlTransforms(
result,
[...normalHooks, ...postHooks],
{
path: '/' + relativeUrlPath,
filename: id,
bundle,
chunk
}
)
// resolve asset url references
result = result.replace(assetUrlRE, (_, fileHash, postfix = '') => {
return (
Expand Down Expand Up @@ -799,9 +805,24 @@ export type IndexHtmlTransformHook = (
export type IndexHtmlTransform =
| IndexHtmlTransformHook
| {
order?: 'pre' | 'post' | null
/**
* @deprecated renamed to `order`
*/
enforce?: 'pre' | 'post'
/**
* @deprecated renamed to `handler`
*/
transform: IndexHtmlTransformHook
}
| {
order?: 'pre' | 'post' | null
/**
* @deprecated renamed to `order`
*/
enforce?: 'pre' | 'post'
handler: IndexHtmlTransformHook
}

export function preImportMapHook(
config: ResolvedConfig
Expand Down Expand Up @@ -850,24 +871,36 @@ export function postImportMapHook(): IndexHtmlTransformHook {

export function resolveHtmlTransforms(
plugins: readonly Plugin[]
): [IndexHtmlTransformHook[], IndexHtmlTransformHook[]] {
): [
IndexHtmlTransformHook[],
IndexHtmlTransformHook[],
IndexHtmlTransformHook[]
] {
const preHooks: IndexHtmlTransformHook[] = []
const normalHooks: IndexHtmlTransformHook[] = []
const postHooks: IndexHtmlTransformHook[] = []

for (const plugin of plugins) {
const hook = plugin.transformIndexHtml
if (hook) {
if (typeof hook === 'function') {
postHooks.push(hook)
} else if (hook.enforce === 'pre') {
preHooks.push(hook.transform)
if (!hook) continue

if (typeof hook === 'function') {
normalHooks.push(hook)
} else {
const order = hook.order ?? hook.enforce
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
// @ts-expect-error union type
const handler = hook.handler ?? hook.transform
if (order === 'pre') {
preHooks.push(handler)
} else if (order === 'post') {
postHooks.push(handler)
} else {
postHooks.push(hook.transform)
normalHooks.push(handler)
}
}
}

return [preHooks, postHooks]
return [preHooks, normalHooks, postHooks]
}

export async function applyHtmlTransforms(
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@ interface AssetNode {
export function createDevHtmlTransformFn(
server: ViteDevServer
): (url: string, html: string, originalUrl: string) => Promise<string> {
const [preHooks, postHooks] = resolveHtmlTransforms(server.config.plugins)
const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(
server.config.plugins
)
return (url: string, html: string, originalUrl: string): Promise<string> => {
return applyHtmlTransforms(
html,
[
preImportMapHook(server.config),
...preHooks,
devHtmlHook,
...normalHooks,
...postHooks,
postImportMapHook()
],
Expand Down
4 changes: 2 additions & 2 deletions playground/html/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ module.exports = {
{
name: 'pre-transform',
transformIndexHtml: {
enforce: 'pre',
transform(html, { filename }) {
order: 'pre',
handler(html, { filename }) {
if (html.includes('/@vite/client')) {
throw new Error('pre transform applied at wrong time!')
}
Expand Down