Extract Text Plugin

Bharat Tiwari
A beginner’s guide to Webpack 2
2 min readMay 13, 2017

In the last lesson, we got our styles working all good; getting the css and scss files bundled and then getting the resulting css included within the <style> tags. While this is okay at this point of the beginning of the development phase while we have very little css , but as our css will grow , we would wish to have our CSS generated in a separate text file like app.bundle.css instead of having all the css included within the <style> tags on the html file.

This is where ExtractTextPlugin comes handy.

  1. Install the plugin
$ npm install extract-text-webpack-plugin --save-dev

2. Update the webpack.config to use the plugin:

./webpack/webpack.config.js

Here we first imported ExtractTextPlugin (line#2) and then created an instance of the ExtractTextPlugin in the plugins array(line#17), providing it with a filename we want it to create for the css file to which it would extract the bundled css.

Also notice the usage of ExtractTextPlugin.extract method in the use key of our scss|css rule in module.rules array (line# 9–12). And we moved the ‘style-loader’ from the use array to fallback.

3. Run npm start again for webpack to run with the updated config options.

$ npm start

4. Refresh our application on the browser (localhost:9000), we should now see the styles from both the css and .scss files getting applied to the application. And this time if you inspect the html in the browser’s dev tool, you would notice, instead of <style> tag, we have a <link> tag with href to the app.bundle.css

In the next story, we will learn how to handle images when packaging our application with webpack.

[ Prev: Using SASS ] [ Next: Handling Images ]

--

--