Vite plugin (v1.5.2) for injecting HTML, JS, and CSS code snippets into index.html at the head or body, with support for different build modes (dev/prod), HMR during development, and strongly typed configuration. Unlike similar tools that require placeholder tags, this plugin injects content by file path and position. Designed for managing third-party scripts, meta tags, and analytics without cluttering index.html. Requires Vite >= 4.0.0, ships TypeScript types, and is actively maintained.
npm install vite-plugin-html-injectionVerified import paths — ran on the pinned version, not inferred.
Configures two injections: Open Graph meta tags always, Google Analytics only in production. Files are relative to project root.
Use `import { htmlInjectionPlugin } from 'vite-plugin-html-injection'` instead.Add `order: 'pre'` to the config if environment variable substitution is needed in injected files.
Place injection files inside the project root (e.g., `./src/injections/`) and reference them with relative paths starting with `./` or `../`.
Use `type: 'raw'` and include the `<script>` or `<style>` tags in the file yourself if you need more control.
Test injection files manually before relying on them.
Run `npm install vite-plugin-html-injection --save-dev` or `pnpm add vite-plugin-html-injection -D`.
Use `import { htmlInjectionPlugin } from 'vite-plugin-html-injection'` (curly braces).Ensure the import statement is `import { htmlInjectionPlugin } from 'vite-plugin-html-injection'` and verify the package version includes that export.Use a path relative to the project root, e.g., `'./src/injections/og.html'`, not an absolute path.