Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sourceMapIn option #253

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
npm-debug.log
tmp
tmp
tmp2
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-api) 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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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();
}
};