Skip to content

Commit

Permalink
Add sourceMapIn option
Browse files Browse the repository at this point in the history
This closes gruntjs#231
  • Loading branch information
Meinaart van Straalen committed Jan 19, 2016
1 parent 5452fd8 commit e868525
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,3 +1,4 @@
node_modules
npm-debug.log
tmp
tmp
tmp2
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -4,3 +4,4 @@ Thomas Boyt (http://www.thomasboyt.com/)
Liam Kaufman (http://liamkaufman.com/)
Jörn Zaefferer (http://bassistance.de)
Braden Anderson (http://google.com/profiles/bluej100)
Meinaart van Straalen (http://github.com/meinaart)
4 changes: 4 additions & 0 deletions CHANGELOG
@@ -1,3 +1,7 @@
v0.15.0:
date: 2016-01-19
changes:
- added sourceMapIn option
v0.14.0:
date: 2015-09-15
changes:
Expand Down
24 changes: 24 additions & 0 deletions Gruntfile.js
Expand Up @@ -53,6 +53,30 @@ module.exports = function (grunt) {
'test/fixtures/inner/input_inline_import.css'
]
}
},
sourceMapIn: {
options: {
sourceMap: true,
sourceMapInlineSources: true,
sourceMapIn: true
},
files: [{
src: [absolutePath('test/fixtures/sourcemap.css')],
dest: absolutePath('tmp/sourcemap.css')
}]
},
sourceMapInFunction: {
options: {
sourceMap: true,
sourceMapInlineSources: true,
sourceMapIn: function(filename) {
return grunt.file.read(filename + '.map', {encoding: 'utf8'});
}
},
files: [{
src: [absolutePath('test/fixtures/sourcemap.css')],
dest: absolutePath('tmp2/sourcemap.css')
}]
}
},
nodeunit: {
Expand Down
16 changes: 14 additions & 2 deletions README.md
Expand Up @@ -29,7 +29,7 @@ _Run this task with the `grunt cssmin` command._

### Options

Options are passed to [clean-css](https://github.com/jakubpawlowicz/clean-css#how-to-use-clean-css-api). In addition this task defines some extra options:
Options are passed to [clean-css](https://github.com/jakubpawlowicz/clean-css#how-to-use-clean-css-programmatically). In addition this task defines some extra options:


#### report
Expand All @@ -50,6 +50,18 @@ Default: `false`

Enable Source Maps.

#### sourceMapIn

Type: `boolean`, `function`
Choices: `true`, `false`, `function`
Default: `undefined`

Method for processing existing sourceMap. When set to true `cssmin` looks for the location of the sourceMap by parsing the `sourceMappingURL` and reading that file from disk (relative to input CSS file).

When a function is supplied cssmin calls this function with the filename of the input CSS file as argument. The function is expected to return a value that is supplied to `clean-css` as `sourceMap` argument. See [clean-css documentation](https://github.com/jakubpawlowicz/clean-css#how-to-use-clean-css-programmatically) for further explanation.

Note: this property only works when `sourceMap` is also set to true.

### Usage

#### Combine two files into one output file
Expand Down Expand Up @@ -115,4 +127,4 @@ cssmin: {

Task submitted by [Tim Branyen](http://tbranyen.com/)

*This file was generated on Tue Sep 15 2015 10:19:12.*
*This file was generated on Tue Jan 19 2016 09:11:31.*
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "grunt-contrib-cssmin",
"description": "Minify CSS",
"version": "0.14.0",
"version": "0.15.0",
"author": {
"name": "Grunt Team",
"url": "http://gruntjs.com/"
Expand Down
18 changes: 18 additions & 0 deletions tasks/cssmin.js
Expand Up @@ -41,6 +41,24 @@ module.exports = function (grunt) {
options.relativeTo = path.dirname(availableFiles[0]);

try {
if(options.sourceMap && options.sourceMapIn) {
if(typeof options.sourceMapIn === 'function') {
options.sourceMap = options.sourceMap && options.sourceMapIn(availableFiles[0]);
} else if(options.sourceMapIn === true) {
var content = grunt.file.read(availableFiles[0], {encoding: 'utf8'});
var sourceMapMatch = content.match(/sourceMappingURL\=([^ ]*)/);
if(sourceMapMatch && sourceMapMatch.length === 2) {
var sourceMap = sourceMapMatch[1];
if(sourceMap.indexOf('/') === -1 || sourceMap.indexOf('/') > 0) {
sourceMap = options.relativeTo + '/' + sourceMap;
if(grunt.file.exists(sourceMap)) {
options.sourceMap = grunt.file.read(sourceMap, {encoding: 'utf8'});
}
}
}
}
}

compiled = new CleanCSS(options).minify(availableFiles);

if (compiled.errors.length) {
Expand Down
2 changes: 2 additions & 0 deletions test/expected/sourcemap.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/expected/sourcemap.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/fixtures/sourcemap.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/fixtures/sourcemap.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 30 additions & 15 deletions test/test.js
Expand Up @@ -6,32 +6,47 @@ function readFileAndRemoveNewlines(file) {
return grunt.file.read(file).replace(/\n/g, '');
}

function filesEqual(test, filename, filename2, message) {
var expect = readFileAndRemoveNewlines(filename);
var result = readFileAndRemoveNewlines(filename2);
test.equal(expect, result, message);
}

function testFilesEqual(test, filename, message) {
return filesEqual(test, 'test/expected/' + filename, 'tmp/' + filename, message);
}

exports.cssmin = {
main: function(test) {
test.expect(1);

var expect = readFileAndRemoveNewlines('test/expected/style.css');
var result = readFileAndRemoveNewlines('tmp/style.css');
test.equal(expect, result, 'should concat and minify an array of CSS files in order using clean-css');

testFilesEqual(test, 'style.css',
'should concat and minify an array of CSS files in order using clean-css');
test.done();
},
imports: function(test) {
test.expect(1);

var expect = readFileAndRemoveNewlines('test/expected/inline_import.css');
var result = readFileAndRemoveNewlines('tmp/inline_import.css');
test.equal(expect, result, 'should inline @import');

testFilesEqual(test, 'inline_import.css', 'should inline @import');
test.done();
},
absolute: function(test) {
test.expect(1);

var expect = readFileAndRemoveNewlines('test/expected/absolute.css');
var result = readFileAndRemoveNewlines('tmp/absolute.css');
test.equal(expect, result, 'should perform the standard tasks when given absolute paths');

testFilesEqual(test, 'absolute.css', 'should perform the standard tasks when given absolute paths');
test.done();
},
sourceMapIn: function(test) {
test.expect(3);
testFilesEqual(test, 'sourcemap.css', 'should have correct format with sourceMappingURL');
test.ok(grunt.file.exists('tmp/sourcemap.css.map'), 'sourceMap file should exist');
testFilesEqual(test, 'sourcemap.css.map', 'sourceMap should have correct format');
test.done();
},
sourceMapInFunction: function(test) {
test.expect(3);
filesEqual(test, 'test/expected/sourcemap.css', 'tmp2/sourcemap.css',
'should have correct format with sourceMappingURL');
test.ok(grunt.file.exists('tmp2/sourcemap.css.map'), 'sourceMap file should exist');
filesEqual(test, 'test/expected/sourcemap.css.map', 'tmp2/sourcemap.css.map',
'sourceMap should have correct format');
test.done();
}
};

0 comments on commit e868525

Please sign in to comment.