bienzaaron / vite-plugin-inline-source

A https://vitejs.dev plugin for inlining source files into HTML files
MIT License
13 stars 4 forks source link

vite-plugin-inline-source

ci npm version

A Vite plugin which inlines source files in HTML files where the inline-source attribute is present. This is similar in premise to vite-plugin-singlefile, but allows you to control which assets are inlined.

This plugin was heavily influenced by markup-inline-loader for webpack. I wanted the same functionality, but couldn't find a suitable replacement in the Vite ecosystem.

Example Usage

Check out the interactive demo on StackBlitz!

vite.config.ts

import { defineConfig } from "vite";
import inlineSource from "vite-plugin-inline-source";

export default defineConfig({
  plugins: [
    inlineSource(),
  ],
});

style.css

body {
  background-color: red;
}

index.html

<html>
  <style inline-source src="https://github.com/bienzaaron/vite-plugin-inline-source/raw/main/style.css"></style>
</html>

Resulting index.html after build

<html>
  <style inline-source>body {
  background-color: red;
}
  </style>
</html>

Options

replaceTags

Tags that should be replaced in the HTML file.

For example, style and script tags are not replaced by default because the corresponding css/js file would not contain the style or script tag. However, with svg tags, the corresponding svg file would contain the svg tag, so the svg tags in the HTML file are replaced. See the unit tests for more explicit examples.

optimizeSvgs

Whether or not to optimize SVGs using svgo.

svgoOptions

Options to pass to svgo. Only used if optimizeSvgs is true.

optimizeCss

Whether or not to optimize CSS using csso.

cssoOptions

Options to pass to csso. Only used if optimizeCss is true.

optimizeJs

Whether or not to optimize JS using terser.

terserOptions

Options to pass to terser. Only used if optimizeJs is true.

compileSass

Whether or not to compile SASS using sass.

sassOptions

Options to pass to sass. Only used if compileSass is true.