1.11.2 • Published 10 months ago

html-inline-css-webpack-plugin v1.11.2

Weekly downloads
9,898
License
MIT
Repository
github
Last release
10 months ago

html-inline-css-webpack-plugin

MIT Licence PRs Welcome Total downloads npm version

Convert external stylesheet to embedded stylesheet, aka document stylesheet.

<link rel="stylesheet" /> => <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
  styleTagFactory?: (params: { style: string }) => string
  leaveCSSFile?: 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. Include both css file and html file name.

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

styleTagFactory(optional)

styleTagFactory?: (params: { style: string }) => string

Used to customize the style tag.

example
...
  new HTMLInlineCSSWebpackPlugin({
    styleTagFactory({ style }) {
      return `<style type="text/css">${style}</style>`;
    },
  }),
...

leaveCSSFile(optional)

if true, it will leave CSS files where they are when inlining

replace(optional)

replace?: {
  target: string
  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

Please note that HTML comment is removed by default by the html-webpack-plugin in production mode. #16

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>
@vdjurdjevic/generator-react-appbuilder-webpack4-vuebuilder-webpack-fourtwobuilder-webpack-yddwebpack-build-pengcjunrvg-clibuilder-webpack-shuimu@everything-registry/sub-chunk-1867gsn-builder-webpacklearning-builder-webpackku-builder-webpacklx-webpacklearn-webpack-buildmatt-builder-webpack@bluescloud/webpack-builder-test@ar0/reactor@digabi/exam-engine-rendering@enhanced-dom/webpack@heibanfe/pack@feflow/feflow-devkit-example@journeyapps/pdf-clieasy-creator-kukuxifox-webpack-builderfull-webpackfirst-webpackfeflow-devkit-ivwebfeflow-devkit-ivweb-examplefeflow-devkit-qmh-webpack4feflow-devkit-react-startkitfeflow-devkit-testfeflow-devkit-webpack4-reactfeflow-devkit-xbc-webpack4feflow-devkit-xbc-webpack4-reactfeflow-devkit-xbc-webpack5my-project-builder-webpacknpm-sebi-test-packagemy-build-webpackphotosynthesis-for-youphotosynthesis-twoproject-webpackreact-builder-libraryry-builder-webpackrsmerreact-builder-libraryreact-webpack-buildersty-builderstudy-build-webpackstudy-my-projectsmallker_webpack_configsimple-webpack-builderyuewen-packyijiahao-webpackvtex-email-frameworkwebpack-build-lgywebpack-build-zqtwebpack-builder-demowebpack-builder-pubwebpack-builder-test-lyhwebpack-builder-toolswebpack-config-devwebpack-config-jaidwebpack-demo-testwebpack-project-builderwebpack-ycxwebpack-package-ytwebpack-template-letttgacowebpack_build_reactxiaofeng-builder-webpackzby-builder-webpack@2fn/toolkit@infinitebrahmanuniverse/nolb-html-i@rockpack/compiler@react-app/react-scripts@sot.app/builder-webpack@steinwei/builder-webpack-demoal-webpack-builderbuild-webpack-cfbuild-webpack-jiebuild-webpack-wbuil-webpackbuilder-webpack-pplbuilder-webpack-reactbuilder-webpack-react-laocebuilder-webpack-sjbuilder-webpack-tangbuilder-webpack-testbuilder-webpack-timbuilder-webpack-tmpbuilder-webpack-xwybuilder-webpack-zhbuilder-webpack-amberbuilder-webpack-benyibuilder-webpack-configbuilder-webpack-crane0builder-webpack-znbuilder-webpack-zpcbuilder-webpack4builder-webpack4-reactbuilder-webpack4-testbuilder_webpack_demo_tsl
1.11.2

10 months ago

1.11.1

3 years ago

1.11.0-alpha.1

3 years ago

1.11.0

3 years ago

1.10.1

3 years ago

1.10.0-beta.0

3 years ago

1.10.0

3 years ago

1.9.1

3 years ago

1.9.0

4 years ago

1.8.1

4 years ago

1.8.0

5 years ago

1.8.0-beta.0

5 years ago

1.7.0

5 years ago

1.7.0-beta.2

5 years ago

1.7.0-beta.1

5 years ago

1.7.0-beta.0

5 years ago

1.6.0

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago