2.1.2 • Published 7 years ago

extract-jss-webpack-plugin v2.1.2

Weekly downloads
3
License
MIT
Repository
-
Last release
7 years ago

npm node deps tests coverage chat

# for webpack 2
npm install --save-dev extract-jss-webpack-plugin
# for webpack 1
npm install --save-dev extract-jss-webpack-plugin@1.0.1

:warning: For webpack v1, see the README in the webpack-1 branch.

const ExtractJssPlugin = require("extract-jss-webpack-plugin");

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ExtractJssPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        })
      }
    ]
  },
  plugins: [
    new ExtractJssPlugin("styles.css"),
  ]
}

It moves all the required *.css modules in entry chunks into a separate CSS file. So your styles are no longer inlined into the JS bundle, but in a separate CSS file (styles.css). If your total stylesheet volume is big, it will be faster because the CSS bundle is loaded in parallel to the JS bundle.

AdvantagesCaveats
Fewer style tags (older IE has a limit)Additional HTTP request
CSS SourceMap (with devtool: "source-map" and extract-jss-webpack-plugin?sourceMap)Longer compilation time
CSS requested in parallelNo runtime public path modification
CSS cached separateNo Hot Module Replacement
Faster runtime (less code and DOM operations)...
new ExtractJssPlugin(options: filename | object)
NameTypeDescription
id{String}Unique ident for this plugin instance. (For advanced usage only, by default automatically generated)
filename{String\|Function}Name of the result file. May contain [name], [id] and [contenthash]
allChunks{Boolean}Extract from all additional chunks too (by default it extracts only from the initial chunk(s))When using CommonsChunkPlugin and there are extracted chunks (from ExtractJssPlugin.extract) in the commons chunk, allChunks must be set to true
disable{Boolean}Disables the plugin
ignoreOrder{Boolean}Disables order check (useful for CSS Modules!), false by default
  • [name] name of the chunk
  • [id] number of the chunk
  • [contenthash] hash of the content of the extracted file

:warning: ExtractJssPlugin generates a file per entry, so you must use [name], [id] or [contenthash] when using multiple entries.

#extract

ExtractJssPlugin.extract(options: loader | object)

Creates an extracting loader from an existing loader. Supports loaders of type { loader: [name]-loader -> {String}, options: {} -> {Object} }.

NameTypeDescription
options.use{String}/{Array}/{Object}Loader(s) that should be used for converting the resource to a CSS exporting module (required)
options.fallback{String}/{Array}/{Object}loader(e.g 'style-loader') that should be used when the CSS is not extracted (i.e. in an additional chunk when allChunks: false)
options.publicPath{String}Override the publicPath setting for this loader

Multiple Instances

There is also an extract function on the instance. You should use this if you have more than one instance of ExtractJssPlugin.

const ExtractJssPlugin = require('extract-jss-webpack-plugin');

// Create multiple instances
const extractCSS = new ExtractJssPlugin('stylesheets/[name]-one.css');
const extractLESS = new ExtractJssPlugin('stylesheets/[name]-two.css');

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: extractCSS.extract([ 'css-loader', 'postcss-loader' ])
      },
      {
        test: /\.less$/i,
        use: extractLESS.extract([ 'css-loader', 'less-loader' ])
      },
    ]
  },
  plugins: [
    extractCSS,
    extractLESS
  ]
};

Extracting Sass or LESS

The configuration is the same, switch out sass-loader for less-loader when necessary.

const ExtractJssPlugin = require('extract-jss-webpack-plugin');

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: ExtractJssPlugin.extract({
          fallback: 'style-loader',
          //resolve-url-loader may be chained before sass-loader if necessary
          use: ['css-loader', 'sass-loader']
        })
      }
    ]
  },
  plugins: [
    new ExtractJssPlugin('style.css')
    //if you want to pass in options, you can do so:
    //new ExtractJssPlugin({
    //  filename: 'style.css'
    //})
  ]
}

Modify filename

filename parameter could be Function. It passes getPath to process the format like css/[name].css and returns the real file name, css/js/a.css. You can replace css/js with css then you will get the new path css/a.css.

entry: {
  'js/a': "./a"
},
plugins: [
  new ExtractJssPlugin({
    filename:  (getPath) => {
      return getPath('css/[name].css').replace('css/js', 'css');
    },
    allChunks: true
  })
]