Skip to content

feat(config): transforms for index.html#793

Merged
yyx990803 merged 3 commits into
vitejs:masterfrom
antfu:feat/trans-html
Oct 26, 2020
Merged

feat(config): transforms for index.html#793
yyx990803 merged 3 commits into
vitejs:masterfrom
antfu:feat/trans-html

Conversation

@antfu
Copy link
Copy Markdown
Member

@antfu antfu commented Sep 10, 2020

This PR introduced a new config indexHtmlTransforms which allows plugins and vite.config.js to transform the index.html for various needs. (not sure about the naming though, feel free to leave comments)

For example, it would be useful for these plugins(probably not exist yet, just giving the context

  • vite-plugin-meta
  • vite-plugin-sitemap
  • vite-plugin-pwa
  • vite-plugin-minify
  • etc.

the following config replaces page's title to Vite Playground

// vite.config.js

export default {
  indexHtmlTransforms: [
    {
      flush: 'pre', // or 'post'
      transform({ code, isBuild }) {
        return code.replace(/<title>.*?<\/title>/, '<title>Vite Playground</title>')
      }
    }
  ]
}

flush also provides the ability to control when should the transforms apply (before Vite's processing or after)

Resolves #528, #739

Related #639, vuejs/vitepress#77 (comment)

@antfu antfu requested a review from underfin September 10, 2020 14:28
@antfu antfu mentioned this pull request Sep 21, 2020
@antfu
Copy link
Copy Markdown
Member Author

antfu commented Oct 15, 2020

@knightly-bot build this

@knightly-bot
Copy link
Copy Markdown

Nightly Build

🌒 Knightly build enabled, release every night at 00:00 UTC (skip if no change)

📦 npm package

Copy link
Copy Markdown
Member

@yyx990803 yyx990803 left a comment

Choose a reason for hiding this comment

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

Made some small adjustments:

  • flush -> apply
  • Defaults to apply: 'post', so the transform can directly be a function instead of always having to use an object with explicit apply timing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide path option and apply transform for index.html

3 participants