Extract text from a bundle, or bundles, into a separate file.
InstallUsagenpm install --save-dev extract-text-webpack-plugin
npm install --save-dev extract-text-webpack-plugin@2.1.2
npm install --save-dev extract-text-webpack-plugin@1.0.1
â ï¸ 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.
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
Name Type Descriptionnew ExtractTextPlugin(options: filename | object)
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))
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
hashType
s, e.g. sha1
, md5
, sha256
, sha512
digestType
s, e.g. hex
, base26
, base32
, base36
, base49
, base52
, base58
, base62
, base64
length
, the length of the hash in charsâ ï¸
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} }
.
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
.
Extracting Sass or LESSconst ExtractTextPlugin = require('extract-text-webpack-plugin');
Â
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
  ]
};
The configuration is the same, switch out sass-loader
for less-loader
when necessary.
Modify filenameconst ExtractTextPlugin = require('extract-text-webpack-plugin');
Â
module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
         Â
          use: ['css-loader', 'sass-loader']
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin('style.css')
   Â
   Â
   Â
   Â
  ]
}
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
.
Maintainersentry:Â {
  'js/a': "./a"
},
plugins:Â [
  new ExtractTextPlugin({
    filename:  (getPath) => {
      return getPath('css/[name].css').replace('css/js', 'css');
    },
    allChunks: true
  })
]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4