Extract Text Plugin

Bharat Tiwari
May 13, 2017 · 2 min read

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 ]

A beginner’s guide to Webpack 2

A beginner’s guide to Webpack 2

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store