GithubHelp home page GithubHelp logo

runjuu / html-inline-css-webpack-plugin Goto Github PK

View Code? Open in Web Editor NEW
79.0 3.0 21.0 730 KB

☄️ A webpack plugin to convert external stylesheets into embedded stylesheets

Home Page: https://www.npmjs.com/package/html-inline-css-webpack-plugin

License: MIT License

TypeScript 100.00%
webpack webpack4 webpack-plugin inline-css inline-styles html-webpack-plugin mini-css-extract-plugin embedded-stylesheet document-stylesheet webpack5

html-inline-css-webpack-plugin's Introduction

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>

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.