Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug CSS bundled by Webpack?

I've switched from including stylesheets the oldschool way:

<link rel="stylesheet" href="./../css/main.css">

to Webpack approach:

var css = require('./../css/main.css');

It works, but I don't like that it extracts the css from this file into inline tag, because then it's harder to debug it in Dev Tools. For example I have no idea from which file and line these attributes are coming from:
enter image description here

How can I move it to separate file or generate a source map that would point me to the source file? So when I inspect it Dev Tools it will look like this:

enter image description here

My webpack.config.js:

var autoprefixer = require('autoprefixer');

module.exports = {
    devtool: "css-loader?sourceMap",
    module:  {
        loaders: [
            {test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000'},
            {
                test:   /\.css$/,
                loader: "style-loader!css-loader!postcss-loader"
            }
        ]
    },
    entry:   [
        './static/js/app.js'
    ],
    output:  {
        filename: './static/js/bundle.js'
    },
    watch:   false,
    postcss: function () {
        return [autoprefixer];
    }
};

My app.js:

var $ = require('jquery');
window.jQuery = $;
window.$ = $;
require('bootstrap-loader');

module.exports = (function () {
    alert('IT WORKS!');
});

window.app = module.exports;
var css = require('./../css/main.css');
like image 342
van_folmert Avatar asked Nov 18 '16 10:11

van_folmert


People also ask

How do I debug a webpack bundle file?

You can use source maps to preserve the mapping between your source code and the bundled/minified one. Webpack provides the devtool option to enhance debugging in the developer tool just creating a source map of the bundled file for you. This option can be used from the command line or used in your webpack.

How do I bundle CSS with webpack?

By default, Webpack will inline your CSS as Javascript tags that injects the CSS into the page. This sounds strange but it lets you do hot reloading in development. In production you extract them to a file using the ExtractTextPlugin, and require it with a normal link tag.

Does webpack compile CSS?

Loaders are the node-based utilities built for webpack to help webpack to compile and/or transform a given type of resource that can be bundled as a javascript module. css-loader is the npm module that would help webpack to collect CSS from all the css files referenced in your application and put it into a string.


1 Answers

To extract your styles from your bundle to an external stylesheet you need to use extract text plugin.

The general webpack config looks like this:

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

module.exports = {
    devtool: "source-map",
    entry: 'path/to/entry.js',
    output:  {
        path: 'path/to/bundle'
        filename: 'bundle.js'
    },
    module:  {
        loaders: [
            {
                test:   /\.css$/,
                loader: ExtracTextPlugin.extract('css-loader?sourceMap')
            }
        ]
    },
    plugins: [
         new ExtractTextPlugin('styles.css')
    ]
};

The parts that you should notice are using devtool: "source-map" for the debugging part and that with the plugin you provide the loader as the parameter to extract method of the plugin:

loaders: [
    {
        test:   /\.css$/,
        loader: ExtracTextPlugin.extract('css-loader?sourceMap')
    }
]

Here you can also chain loaders if you need for example sass-loader too:

loaders: [
    {
        test:   /\.scss$/,
        loader: ExtracTextPlugin.extract('css-loader?sourceMap!sass-loader?sourceMap')
    }
]

You don't need the style-loader anymore but you can also provide it as a fallback loader to the plugin:

loaders: [
    { 
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
            fallbackLoader: "style-loader",
            loader: "css-loader?sourceMap"
        })
    }
]

and in your plugins array you instantiate the plugin and provide the name you want for the external stylesheet:

plugins: [
     new ExtractTextPlugin('styles.css')
]

You can also use [name] placeholder if you have multiple entry points and you will have a stylesheet per entry.

like image 127
jal Avatar answered Oct 23 '22 16:10

jal