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

Package detail

extract-text-webpack-plugin

webpack-contrib1.7mMITdeprecated3.0.2TypeScript support: definitely-typed

Deprecated. Please use https://github.com/webpack-contrib/mini-css-extract-plugin

Extract text from bundle into a file.

readme

npm node deps tests coverage chat

Extract Text Plugin

Extract text from a bundle, or bundles, into a separate file.

Install

# for webpack 3
npm install --save-dev extract-text-webpack-plugin
# for webpack 2
npm install --save-dev extract-text-webpack-plugin@2.1.2
# for webpack 1
npm install --save-dev extract-text-webpack-plugin@1.0.1

Usage

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

const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: "css-loader"
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin("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.

Advantages Caveats
Fewer style tags (older IE has a limit) Additional HTTP request
CSS SourceMap (with devtool: "source-map" and extract-text-webpack-plugin?sourceMap) Longer compilation time
CSS requested in parallel No runtime public path modification
CSS cached separate No Hot Module Replacement
Faster runtime (less code and DOM operations) ...

Options

new ExtractTextPlugin(options: filename | object)
Name Type Description
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 ExtractTextPlugin.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
  • [<hashType>:contenthash:<digestType>:<length>] optionally you can configure
    • other hashTypes, e.g. sha1, md5, sha256, sha512
    • other digestTypes, e.g. hex, base26, base32, base36, base49, base52, base58, base62, base64
    • and length, the length of the hash in chars

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

#extract

ExtractTextPlugin.extract(options: loader | object)

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

Name Type Description
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 ExtractTextPlugin.

const ExtractTextPlugin = require('extract-text-webpack-plugin');

// Create multiple instances
const extractCSS = new ExtractTextPlugin('stylesheets/[name]-one.css');
const extractLESS = new ExtractTextPlugin('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 ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          //resolve-url-loader may be chained before sass-loader if necessary
          use: ['css-loader', 'sass-loader']
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin('style.css')
    //if you want to pass in options, you can do so:
    //new ExtractTextPlugin({
    //  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 ExtractTextPlugin({
    filename:  (getPath) => {
      return getPath('css/[name].css').replace('css/js', 'css');
    },
    allChunks: true
  })
]

Maintainers


Juho Vepsäläinen

Joshua Wiens

Kees Kluskens

Sean Larkin

changelog

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

3.0.2 (2017-10-25)

Bug Fixes

  • refer to the entrypoint instead of the first module (module.identifier) (#601) (d5a1de2)

3.0.1 (2017-10-03)

Bug Fixes

  • index: stricter check for shouldExtract !== wasExtracted (#605) (510704f)
  • get real path from __filename instead of __dirname (NS) (8de6558)

3.0.0 (2017-07-10)

Bug Fixes

  • add missing options.ignoreOrder details in Error message (#539) (dd43832)

Code Refactoring

  • Apply webpack-defaults & webpack 3.x support (#540) (7ae32d9)

BREAKING CHANGES

  • Enforces engines of "node": ">=4.3.0 < 5.0.0 || >= 5.10

  • refactor: DeprecationWarning: Chunk.modules 543

  • Updates to Chunk.mapModules. This release is not backwards compatible with Webpack 2.x due to breaking changes in webpack/webpack#4764

  • fix: css generation order issue see: webpack/webpack#5225

  • Enforces peerDependencies of "webpack": "^3.1.0".

3.0.0-rc.2 (2017-07-10)

Bug Fixes

3.0.0-rc.1 (2017-07-07)

Bug Fixes

3.0.0-rc.0 (2017-07-07)

Code Refactoring

  • Update deprecated chunk.modules functions (#553) (be7936d)

BREAKING CHANGES

  • Updates to Chunk.mapModules | forEachModule | getNumberOfModules. This release is not backwards compatible with Webpack 2.x due to breaking changes in webpack/webpack#4764

3.0.0-beta.3 (2017-06-24)

Bug Fixes

  • Distribute schema with package (5d0c28f)

3.0.0-beta.2 (2017-06-24)

  • Skipped due to deployment issues with schema

3.0.0-beta.1 (2017-06-24)

  • Skipped due to deployment issues with schema

3.0.0-beta.0 (2017-06-23)

Bug Fixes

  • add missing options.ignoreOrder details in Error message (#539) (dd43832)

Code Refactoring

BREAKING CHANGES

  • Updates to Chunk.mapModules. This release is not backwards compatible with Webpack 2.x due to breaking changes in webpack/webpack#4764
  • Enforces peerDependencies of "webpack": ">= 3.0.0-rc.0 || ^3.0.0".
  • Enforces engines of "node": ">=4.3.0 < 5.0.0 || >= 5.10

2.1.2 (2017-06-08)

2.1.1 (2017-06-08)

Bug Fixes

  • add a not null check for the content property before throwing error (#404) (58dd5d3)
  • loader: rm unnecessary this.cacheable (caching) (#530) (c3cb091)
  • don't extract from common async chunks (#508) (e595417)
  • validation schema (schema-utils) (#527) (dfeb347)

2.1.0 (2017-03-05)

Features

  • The plugin filename accepts a function now. c9a19ad, closes #423

2.0.0 (2017-02-24)

2.0.0-rc.2 (2017-01-28)

Bug Fixes

  • schema: allow extract to accept omit/remove flags (8ce93d5), closes #371
  • schema: connect loader schema with the code properly (03bb4aa)
  • schema: emit proper error messages (70cbd4b)

Features

  • errors: show nicer errors if there are extra fields (76a171d)

2.0.0-rc.1 (2017-01-28)

Bug Fixes

  • options: pass proper loader options to children (#266) (6abf42d)

2.0.0-rc.0 (2017-01-26)

Bug Fixes

  • readme: Incorrect loader configuration (e477cc7)

Features

  • extract: return an array of loader objects (#343) (74b86e0)

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.