InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Gulp tutorials

  • Gulp quick start tutorial
  • Css source map - gulp-sourcemaps
 
  • Home
  • > Tutorials
  • > Web Development
  • > Gulp

Css source map example using gulp-sourcemaps

By admin on Jan 19, 2016

If you are concatenating multiple css files into once css file, it is useful to have sourcemap in your combined css file in development environment. That way chrome developer tools can show the correct source file while debugging. Here is quick example on Ubuntu Linux which generates css sourcemap and then we will see it working in Chrome. In case you are not familiar with gulp, you can visit Gulp quick start tutorial

Files used

These are the files used in this tutorial. Create these files in a proj1 directory inside css folder.

.foo {
  width: 20px;
}

.bar {
  width: 30px;
}

Genereting sourcemap

  1. Create gulpfile.js inside proj1 directory with the following content.
    var gulp = require('gulp');
    var concat = require('gulp-concat');
    var sourcemaps = require('gulp-sourcemaps');
    
    gulp.task('css', function() {
      return gulp.src(['./css/file1.css', './css/file2.css'])
        .pipe(sourcemaps.init())
        .pipe(concat('all.css'))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('./dest/'));
    });
  2. Run the following on command line to generate all.css with source map.

    $ gulp css
    [12:27:59] Using gulpfile ~/tmp/proj1/gulpfile.js
    [12:27:59] Starting 'css'...
    [12:27:59] Finished 'css' after 39 ms
    
  3. Look at the content of all.css

    $ cat dest/all.css
    .foo {
      width: 20px;
    }
    
    .bar {
      width: 30px;
    }
    
    /*# sourceMappingURL=all.css.map */
    

    And all.css.map

    $ cat dest/all.css.map 
    {"version":3,"sources":["file1.css","file2.css"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;ACHA;AACA;AACA;AACA","file":"all.css","sourcesContent":[".foo {\n  width: 20px;\n}\n",".bar {\n  width: 30px;\n}\n"],"sourceRoot":"/source/"}
    
  4. The comment /*# sourceMappingURL=all.css.map */ tells the browser the location of source map. And sourcemap file all.css.map contains the actual source mapping.

Viewing source css file in chrome

  1. Create a test html file which uses all.css as shown below.
    <link rel='stylesheet' id='style-css' href='https://dev.infoheap.com/dest/all.css' type='text/css' media='all' />
    <div class="foo">
    hello
    </div>
  2. Now try to inspect the div element in Chrome developer tools. You should be able to see style with original source files (e. g. file1.css) as shown below:
    chrome-developer-tools-sourcemap-original-source-css

Suggested posts:

  1. jQuery – find total number of DOM elements
  2. How to install drupal on Mac usin MAMP
  3. mac pkgutil – quick start guide
  4. PHP – How to log custom data in apache access log
  5. How to install ViewVC for svn repository on Ubuntu Linux
  6. CSS margin area of a box
  7. Mac – open a terminal from a folder
  8. Git display local HEAD location
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Chrome developer tools, CSS, CSS cookbook, Gulp, Node.js, Tutorials, Web Development
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress