GithubHelp home page GithubHelp logo

vite-plugin-simple-html's Introduction

npm downloads CI

vite-plugin-simple-html

Vite plugin for HTML processing and minification. "Lite" version of vite-plugin-html, supporting a subset of its features.

tl;dr

  • Install by executing npm install vite-plugin-simple-html or yarn add vite-plugin-simple-html.
  • Import by adding import simpleHtmlPlugin from 'vite-plugin-simple-html'.
  • Use it by adding simpleHtmlPlugin() to plugins section of your Vite config.

Usage

Here's an example of basic configuration:

import { defineConfig } from 'vite';
import simpleHtmlPlugin from 'vite-plugin-simple-html';

export default defineConfig({
  plugins: [
    simpleHtmlPlugin({
      inject: {
        data: {
          title: 'My app',
          script: '<script src="index.js"></script>',
        },
        tags: [
          {
            tag: 'meta',
            attrs: {
              name: 'description',
              content: 'My awesome app',
            },
          },
        ],
      },
      minify: true,
    }),
  ],
});

User guide

Minification

HTML minification is done using html-minifier-terser.

To minify your HTML files, set minify to true:

import { defineConfig } from 'vite';
import simpleHtmlPlugin from 'vite-plugin-simple-html';

export default defineConfig({
  plugins: [
    simpleHtmlPlugin({
      minify: true,
    }),
  ],
});

The default configuration in this case is:

{
  collapseWhitespace: true,
  keepClosingSlash: true,
  removeComments: true,
  removeRedundantAttributes: true,
  removeScriptTypeAttributes: true,
  removeStyleLinkTypeAttributes: true,
  useShortDoctype: true,
  minifyCSS: true,
}

If you want to customize the minification process, for example to minify JS, you can pass your own configuration object:

import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    simpleHtmlPlugin({
      minify: {
        minifyJS: true,
      },
    }),
  ],
});

EJS variables support

You can inject variables into your HTML files using EJS syntax.

import { defineConfig } from 'vite';
import simpleHtmlPlugin from 'vite-plugin-simple-html';

export default defineConfig({
  plugins: [
    simpleHtmlPlugin({
      inject: {
        data: {
          title: 'My app',
        },
      },
    }),
  ],
});
<!doctype html>
<html lang="en">
  <head>
    <title><%= title %></title>
  </head>
  <body>
    <h1><%= title %></h1>
  </body>
</html>

Tag injection

You can inject tags into your HTML files.

import { defineConfig } from 'vite';
import simpleHtmlPlugin from 'vite-plugin-simple-html';

export default defineConfig({
  plugins: [
    simpleHtmlPlugin({
      inject: {
        tags: [
          {
            tag: 'meta',
            attrs: {
              name: 'description',
              content: 'My awesome app',
            },
          },
        ],
      },
    }),
  ],
});

By default, they are injected at the end of the <head> section of your HTML file. You can change that behavior by setting injectTo:

  • head: Injects tags at the end of the <head> section of your HTML file (default).
  • head-prepend: Injects tags at the beginning of the <head> section of your HTML file.
  • body: Injects tags at the end of the <body> section of your HTML file.
  • body-prepend: Injects tags at the beginning of the <body> section of your HTML file.

Detailed comparison with vite-plugin-html

Feature vite-plugin-simple-html vite-plugin-html
EJS support ⚠️ Variables only
HTML tags injection
HTML/CSS/JS minification
entry script injection
template customization
multi-page support

Why bother?

  • vite-plugin-simple-html has fewer dependencies.

  • vite-plugin-simple-html does not suffer from issue that breaks Vite proxy (which was the reason I created this plugin in the first place).

  • vite-plugin-simple-html does not use options deprecated in Vite 5, and thus does not produce deprecation warnings:

     WARN  plugin 'vite:html' uses deprecated 'enforce' option. Use 'order' option instead.
    
     WARN  plugin 'vite:html' uses deprecated 'transform' option. Use 'handler' option instead.
    

License

The MIT License.

Author

Wojciech Maj Wojciech Maj
```

vite-plugin-simple-html's People

Contributors

wojtekmaj avatar dependabot[bot] avatar

Stargazers

Kavi R avatar  avatar  avatar  avatar Adam avatar Andrei Badescu avatar deyu avatar Mehdi avatar Lawrence Onah avatar

Watchers

 avatar Mehdi avatar  avatar

vite-plugin-simple-html's Issues

Maybe the execution life cycle is wrong?

I can write like this when using "vite-plugin-html", but I get an error when using "vite-plugin-simple-html".

image

So I looked at your implementation and adjusted it to:
image

Still not working,Maybe the execution life cycle is wrong?

HTML minifier doesn't process generated asset elements

vite.config.js:

import { defineConfig } from "vite";
import viteSimpleHtml from "vite-plugin-simple-html";

export default defineConfig({
    plugins: [
        viteSimpleHtml({
            minify: true,
        }),
    ],
});

index.html:

<!DOCTYPE html>
<html lang="en-US" dir="ltr">
    <head>
        <link rel="stylesheet" type="text/css" href="./main.css" />
        <title>Sample Site</title>
    </head>
</html>

vite build - dist/index.html:

<!doctype html><html lang="en-US" dir="ltr"><head><title>Sample Site</title>  <link rel="stylesheet" crossorigin href="/assets/index-MCPePPYg.css">
</head></html>

Notice the following:

  • whitespace before the <link> element
  • a newline after the <link> element

html-minifier-terser's collapseWhitespace should take care of that, but it seems to run before assets get replaced. For reference:

$ ./yarnw dlx html-minifier-terser --collapse-whitespace dist/index.html
<!doctype html><html lang="en-US" dir="ltr"><head><title>Sample Site</title><link rel="stylesheet" crossorigin href="/assets/index-MCPePPYg.css"></head></html>

How to inject HTML into the template?

From the docs I don't understand how to template my html.
Let's say I have a footer.html which I want to integrate in every html file from my project at the bottom of the body. How could I do this using this plugin?

Not a true EJS plug-in

Hello,

I write this, I suppose, to let people know that this is not a true EJS plug-in. It simply does replacement of EJS variables. It does not evaluate expressions like <% if (isLocal) { %>... <% } %>. The author of the plug-in may correct me if I'm mistaken.

I would suggest the README explicitly states that it simply replaces EJS variables, but doesn't do anything else.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.