1.11.2 • Published 2 years ago

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

Weekly downloads
9,898
License
MIT
Repository
github
Last release
2 years 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-1867webpack_build_reactxiaofeng-builder-webpackvtex-email-framework@bluescloud/webpack-builder-testku-builder-webpackstudy-my-projectsty-builderstudy-build-webpack@ar0/reactor@2fn/toolkit@digabi/exam-engine-renderingyijiahao-webpackzby-builder-webpackyuewen-packwebpack-project-builderwebpack-template-letttgacowebpack-ycxwebpack-package-ytwebpack-config-devwebpack-config-jaidwebpack-build-lgywebpack-build-zqtwebpack-builder-demowebpack-builder-pubwebpack-builder-test-lyhwebpack-builder-toolswebpack-demo-test@feflow/feflow-devkit-example@heibanfe/pack@infinitebrahmanuniverse/nolb-html-i@enhanced-dom/webpack@journeyapps/pdf-cli@react-app/react-scripts@sot.app/builder-webpack@rockpack/compilermy-project-builder-webpackmy-build-webpacknpm-sebi-test-packagereact-builder-libraryrreact-builder-libraryrsmery-builder-webpackphotosynthesis-twophotosynthesis-for-youproject-webpacksmallker_webpack_configsimple-webpack-builderreact-webpack-buildergsn-builder-webpacklearn-webpack-buildlearning-builder-webpacklx-webpackmatt-builder-webpack@steinwei/builder-webpack-demoafront@zzq0608/builder-webpackal-webpack-builderchst-builder-webpackcreate-kocia-kreacjacsc_webpackbeast-sheriff-webpackbase-webpack-builderbuider-webpack-vurtne-pluginbuil-webpackbuilde-webpack-projectbuilder-webpack-albuilder-webpack-alxbuilder-webpack-amberbuilder-webpack-benyibuilder-webpack-configbuilder-webpack-crane0builder-webpack-demo1builder-webpack-devbuilder-webpack-dollybuilder-webpack-examplebuilder-webpack-fxbuilder-webpack-geektimebuilder-webpack-geektimesbuilder-webpack-genericbuilder-webpack-haoxuanbuilder-webpack-hmbuilder-webpack-jasonbuilder-error2builder-webpack-xwybuilder-webpack-zhbuilder-webpack-zkbuilder-webpack-znbuilder-webpack-zpcbuilder-webpack4builder-webpack4-reactbuilder-webpack4-testbuilder-webpack5
1.11.2

2 years ago

1.11.1

4 years ago

1.11.0-alpha.1

4 years ago

1.11.0

4 years ago

1.10.1

4 years ago

1.10.0-beta.0

4 years ago

1.10.0

4 years ago

1.9.1

5 years ago

1.9.0

5 years ago

1.8.1

5 years ago

1.8.0

6 years ago

1.8.0-beta.0

6 years ago

1.7.0

6 years ago

1.7.0-beta.2

6 years ago

1.7.0-beta.1

6 years ago

1.7.0-beta.0

6 years ago

1.6.0

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago