Giter Club home page Giter Club logo

extract-text-webpack-plugin's Introduction

npm node deps tests coverage chat

Extract Text Plugin

Install

npm install --save-dev extract-text-webpack-plugin

Usage

⚠️ 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({
          fallbackLoader: "style-loader",
          loader: "css-loader"
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin("styles.css"),
  ]
}

It moves every require("style.css") in entry chunks into a separate CSS file. So your styles are no longer inlined into the JS bundle, but separate in a CSS bundle 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} Name of the result file. May contain [name], [id] and [contenthash]
options.allChunks {Boolean} Extract from all additional chunks too (by default it extracts only from the initial chunk(s))
options.disable {Boolean} Disables the plugin
  • [name] name of the chunk
  • [id] number of the chunk
  • [contenthash] hash of the content of the extracted file

⚠️ 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.loader {String}/{Object} Loader(s) that should be used for converting the resource to a CSS exporting module (required)
options.fallbackLoader {String}/{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].css');
const extractLESS = new ExtractTextPlugin('stylesheets/[name].less');

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

Maintainer


Tobias Koppers

extract-text-webpack-plugin's People

Contributors

ai avatar andreypopp avatar bcbcb avatar bebraw avatar ctrlzvi avatar daniel15 avatar diurnalist avatar fortruce avatar frederikprijck avatar insin avatar izaakschroeder avatar joshwiens avatar jpeer264 avatar mateatslc avatar michael-ciniawsky avatar pgilad avatar rabidfx avatar ratson avatar renarsvilnis avatar scottdj92 avatar sherylhohman avatar simenb avatar sokra avatar spacek33z avatar spicydonuts avatar texastoland avatar wtgtybhertgeghgtwtg avatar

Watchers

 avatar

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.