Git Product home page Git Product logo

htmls-webpack-plugin's Introduction

htmls-webpack-plugin

Build Status npm npm

Simple, flexible and fast html webpack plugin.

NOTE: v2 support webpack5, if you still using webpack4, please install [email protected]

Features

  • Simple and flexisble, you can almost controll anything of generate htmls, no need of setup lots of plugins.
  • Support multiple htmls by default
  • Fast, almost 20x faster then html-webpack-plugin for 20+ pages.
  • Rendered via fast & small template engine ejs by default(you can customize via render function to use any template engine or just inject script tags string to the html body).

Install

npm i -D htmls-webpack-plugin

Usage

Options

const HtmlsWebpackPlugin = require('htmls-webpack-plugin')

module.exports = {
    plugins: [
        new HtmlsWebpackPlugin({
             // optional, hooks
            beforeEmit: (compilation, compiler) => void,
             // optional, hooks
            afterEmit: (compilation, compiler) => void,
             // optional, default is ejs. custom template rendering function, support async rendering,
            render: (src: string, params: Params) => string | Promise<string>,
            htmls: [{
                // required, template path
                src: './index.ejs',
                // required, string | ((source, src, params) => string),
                // output filename,
                // relative to output path,
                // can be a function to generate via context
                filename: 'index.html',
                // optional, override global render function
                render: (src: string, params: Params) => string | Promise<string>,
                // optional, override global flushOnDev
                flushOnDev: boolean | string
                // optional, custom params when rendering
                params: () => object | () => Promise<object> | object
                // optional, transformParams, override global transformParams
                transformParams?: (params: Params) => Params & { [k: string]: any }
            }],

             /* boolean | string, optional, flush html files to output folder, can be a string file path, useful for debug or devServer. */
            flushOnDev: false,

             /* optional, override webpackConf's publicPath */
            publicPath: function | string',

            // optional, custom params when rendering, could be an async function
            params: () => object | () => Promise<object> | object

            // optional, transform template variables
            transformParams?: (params: Params) => Params & { [k: string]: any }
        })
    ]
}

ejs template example

<!-- index.ejs -->
<body>
    <% for (let js in entries) {%>
        <script src="<%= js %>"></script>
    <% } %>
</body>

Available variables in html templates:

interface Params {
  entries: string[] // all entrypoints
  files: string[] // all files
  jses: string[] // all files ends with .js
  csses: string[] // all files ends with .css
  options: Props // plugin props
  compilation: Compilation // webpack compilation
  [k: string]: any // custom params via options
}

Minify html

  1. install html-minifier
yarn add -D html-minifier
  1. minify your html inrender function
const HtmlsWebpackPlugin = require('htmls-webpack-plugin')
const htmlMinifier = require('html-minifier')
const ejs = require('ejs')

// webpack.config.js
module.exports = {
    ...,
    plugins: [
        new HtmlsWebpackPlugin({
            htmls: [{
                src: './fixtures/index.ejs',
                filename: `index.html`,
                async render(src, params) {
                    let html = await ejs.renderFile(src, params, { async: true })
                    return require('html-minifier').minify(html)
                }
            }],
        })
    ]
}

Example in tests

index.test.ts

Why not html-webpack-plugin

html-webpack-plugin is really hard to extends and slow for multiple htmls, too much complete features that I don't need. That's why I create this.

License

MIT

htmls-webpack-plugin's People

Contributors

zaaack avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

htmls-webpack-plugin's Issues

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.