html-inline-css-webpack-plugin

1.3.0 • Public • Published

html-inline-css-webpack-plugin

MIT Licence PRs Welcome npm version

Convert external style sheet(<link rel="stylesheet"/>) to internal style sheet(<style>...<style/>).

Require mini-css-extract-plugin and html-webpack-plugin

Install

NPM

npm i html-inline-css-webpack-plugin -D

Yarn

yarn add html-inline-css-webpack-plugin -D

Minimal example

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HTMLInlineCSSWebpackPlugin = require("html-inline-css-webpack-plugin").default;
 
module.exports = {
  plugins: [
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    }),
    new HtmlWebpackPlugin(),
    new HTMLInlineCSSWebpackPlugin(),
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader"
        ]
      }
    ]
  }
}

Config

interface Config {
  filter?(fileName: string): boolean
  replace?: {
    target: string
    position?: 'before' | 'after'
    removeTarget?: boolean
  }
}

filter(optional)

filter?(fileName: string): boolean

Return true to make current file internal, otherwise ignore current file.

example
...
  new HTMLInlineCSSWebpackPlugin({
    filter(fileName) {
      return fileName.includes('main');
    },
  }),
...

replace(optional)

replace?: {
  targetstring
  position?: 'before' | 'after' // default is 'before'
  removeTarget?: boolean // default is false
}

A config for customizing the location of injection, default will add internal style sheet before the </head>

target

A target for adding the internal style sheet

position(optional)

Add internal style sheet before/after the target

removeTarget(optional)

if true, it will remove the target from the output HTML

example
<head>
    <!-- inline_css_plugin -->
    <style>
        /* some hard code style */
    </style> 
</head>
...
  new HTMLInlineCSSWebpackPlugin({
    replace: {
      removeTarget: true,
      target: '<!-- inline_css_plugin -->',
    },
  }),
...
output:
<head>
    <style>
        /* style from *.css files */
    </style> 
    <style>
        /* some hard code style */
    </style> 
</head>

install

npm i html-inline-css-webpack-plugin

Downloadsweekly downloads

125

version

1.3.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability