Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sourcemaps with webpack css-loader

Tags:

I am struggling to get sourcemaps working with css-loader.

Output in console:

enter image description here

What the documentation from css-loader says:

SourceMaps

To include SourceMaps set the sourceMap query param.

require("css-loader?sourceMap!./file.css")

My webpack.config

var webpack = require('webpack')  module.exports = {   entry: './src/client/js/App.js',    output: {     path: './public',     filename: 'bundle.js',     publicPath: '/'   },    plugins: process.env.NODE_ENV === 'production' ? [     new webpack.optimize.DedupePlugin(),     new webpack.optimize.OccurrenceOrderPlugin(),     new webpack.optimize.UglifyJsPlugin()   ] : [],    module: {     loaders: [       { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?presets[]=es2015&presets[]=react' },       { test: /\.scss$/, loaders: ['style', 'css', 'sass']},       { test: /\.css$/, loader: "style-loader!css-loader?sourceMap!./file.css" },       { test: /\.png$/, loader: "url-loader?limit=100000" },       { test: /\.jpg$/, loader: "file-loader" }     ]   } } 

How i include the sass

import React from 'react' import { render } from 'react-dom' import { Router, browserHistory } from 'react-router' import routes from './routes' import '../scss/main.scss'  render(   <Router routes={routes} history={browserHistory}/>,   document.getElementById('app') ) 
like image 689
Jamie Hutber Avatar asked May 18 '16 22:05

Jamie Hutber


People also ask

How do I use CSS modules with Webpacks?

To be able to use CSS in your webpack app, you need to set up a new loader. Out-of-the-box, webpack only understands Javascript and JSON. With a loader, you can translate another type of file to a format that webpack understands and can work with. There are many webpack loaders and each loader has a specific purpose.

Do I need Postcss loader?

It's not required but I do highly recommend the autoprefixer plugin. The loaders allow you to import the specified file types.

How do I enable source map in webpack?

Using Webpack, specifying devtool: "source-map" in your Webpack config will enable source maps, and Webpack will output a sourceMappingURL directive in your final, minified file. You can customize the source map filename itself by specifying sourceMapFilename .


1 Answers

  1. Enable source-maps via webpack

    ... devtool: 'source-map' ... 
  2. You might want to enable source-maps for Sass-Files as well

    module: {   loaders: [     ...     {       test: /\.scss$/,       loaders: [         'style-loader',         'css-loader?sourceMap',         'sass-loader?sourceMap'       ]     }, {       test: /\.css$/,       loaders: [         "style-loader",         "css-loader?sourceMap"       ]     },     ...   ] } 
  3. Use extract text plugin to extract your css into a file.

    ... plugins: [   ...   new ExtractTextPlugin('file.css') ] ... 
like image 137
HaNdTriX Avatar answered Sep 17 '22 06:09

HaNdTriX