Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

css-hot-loader

shepherdwind123.3kISC1.4.4

css hot reload work with extract-text-webpack-plugin

readme

CSS Hot Loader

build status Test coverage NPM version npm download

This is a css hot loader, which support hot module replacement for an extracted css file.

Why we need css hot loader

In most cases, we can realize css hot reload by style-loader . But style-loader need inject style tag into document, Before js ready, the web page will have no any style. That is not good experience.

Also, a lots of people thought about that, How can realize hot reload with extract-text-webpack-plugin. For example #30 , #!89.

So I wrote this loader, which supports hot module replacement for an extracted css file.

Install

First install package from npm

$ npm install css-hot-loader --save-dev

Then config webpack.config.js

module: {
  rules: [
    {
      test: /\.css/,
      use: [
        'css-hot-loader',
        MiniCssExtractPlugin.loader,
        'css-loader',
      ],
    },
  ] // end rules
},

There is an issue to work with webpack4 #37. Please use mini-css-extract-plugin to replace extract-text-webpack-plugin.

Attention

This plugin require the output css file name static. If output file name depend on css content, for example 'bundle.[name].[contenthash].css', HMR reload will fail, more detail refer to #21.

webpack 1.x

Config file example should like this

  module: {
    loaders: [{
      test: /\.less$/,
      loaders: [
        'css-hot-loader',
        'extract-text-webpack-plugin',
        'less',
        ...
       ],
      include: path.join(__dirname, 'src')
    }]
  }

See more examples code from https://github.com/shepherdwind/css-hot-loader/tree/v1.4.3/examples

options

fileMap

Option to define you css file reload rule. Since 1.1.0 .

For example 'css-hot-loader?fileMap='../css/{fileName}' , which mean

js/foo.js => css/foo.css

Default value is {fileName}.

see #3.

reloadAll

Force reload all css file.

cssModule

When this option is opened, every time you modify the css file, the js file will reload too. Default closed, this option use with css module.

see !47 and !51

How

The realization principle of this loader is very simple. There are some assumed condition:

  1. css required by js , so css also be a js file
  2. The name of css file, which need hot reload , is the same as js file excuted.

The secend assumption is often established. If you use extract-text-webpack-plugin , entry foo.js will extract css file foo.css. This principle will help us to locate the url of css file extracted.

Because every css file will be a js module , every css file change can affect a module change. CSS hot loader will accept this kind change, then find extracted css file by document.currentScript.

So when a css file changed, We just need find which css file link element, and reload css file.

License

(The MIT License)

changelog

1.4.4 2019-03-13

  • fix: replace DOM api remove to removeChild, support ie !57

1.4.3 2018-12-12

1.4.2 2018-08-15

  • feat: add cssModule option !51

1.4.1 2018-07-26

1.4.0 2018-07-11

  • feat: support reloadAll config, support code split issue #44

1.3.9 2018-03-27

  • fix: Webpack 4 compatibility fix by @vagusX !39

1.3.8 2018-03-01

  • feat: using debounce for update in DOM by @53c701d !35

1.3.7 / 2018-02-10

  • Fix issue causing multiple instances of link tags to be inserted by @aminland !33

1.3.6 / 2018-01-23

  • Safe access to 'src' of the last "script" tag#30

1.3.5 / 2017-12-24

  • Refactor hotModuleReplacement #27 by @GeorgeTaveras1231

1.3.3 / 2017-11-08

  • Replace css link instead of adding new one #24

1.3.2 / 2017-10-07

  • memory leak fix, only reload css files #22 by @nickaversano

1.3.1 / 2017-09-04

  • Use var instead of const for IE10 #17

1.3.0 / 2017-07-03

  • Add support for commons chunk #12 by @tfoxy
  • Fix error when use a local host #11 by @tfoxy

1.2.0 / 2017-06-23

  • Applying styles without site jerking !9 by @nvlad

1.1.1 / 2017-05-18

  • Use loaderUtils.stringifyRequest to fix error path on windows !6 by @dannsam

1.1.0 / 2017-05-14

  • Add option fileMap, fix #3

1.0.3 / 2017-05-05

  • IE11 compatibility !2 by @extronics

1.0.2 / 2017-04-05

  • css file change md5 hash , support webpack 2.0, fix issue #1

1.0.1 / 2017-01-26

  • First version, work fine