Skip to content
This repository was archived by the owner on Mar 26, 2018. It is now read-only.

Commit 4eac382

Browse files
committed
update sourcemap urls for css files
1 parent e7ddab5 commit 4eac382

File tree

7 files changed

+35
-3
lines changed

7 files changed

+35
-3
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = function (grunt) {
7474
withSourceMaps: {
7575
expand: true,
7676
cwd: 'test/fixtures',
77-
src: ['*.js'],
77+
src: ['*.js', '*.css'],
7878
dest: 'test/tmp/withSourceMaps'
7979
}
8080
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
],
2525
"dependencies": {
2626
"chalk": "^1.0.0",
27+
"convert-source-map": "^1.0.0",
2728
"each-async": "^0.1.3"
2829
},
2930
"devDependencies": {

tasks/filerev.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var path = require('path');
44
var fs = require('fs');
55
var chalk = require('chalk');
66
var eachAsync = require('each-async');
7+
var convert = require('convert-source-map');
8+
79

810
module.exports = function (grunt) {
911
grunt.registerMultiTask('filerev', 'File revisioning based on content hashing', function () {
@@ -84,10 +86,12 @@ module.exports = function (grunt) {
8486
} else {
8587
grunt.file.copy(map, resultPathMap);
8688
}
87-
8889
// rewrite the sourceMappingURL in files
8990
var fileContents = grunt.file.read(resultPath, {encoding: 'utf8'});
90-
var newSrcMap = fileContents.replace('//# sourceMappingURL=' + path.basename(file) + '.map', '//# sourceMappingURL=' + path.basename(resultPathMap));
91+
var matches = convert.mapFileCommentRegex.exec(fileContents);
92+
// var sourceMapComment = matches[0]; // full comment
93+
var sourceMapFile = matches[1] || matches[2]; // 1st is single line, 2nd is multiline
94+
var newSrcMap = fileContents.replace(sourceMapFile, path.basename(resultPathMap));
9195
grunt.file.write(resultPath, newSrcMap, {encoding: 'utf8'});
9296
sourceMap = true;
9397
}

test/fixtures/more-styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.more-styles{color:#fff}

test/fixtures/styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
div{width:100%;height:100%;color:#fff}/*# sourceMappingURL=styles.css.map */

test/fixtures/styles.css.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ var hashes = {
88
'test/fixtures/math.js' : 'test/tmp/withSourceMaps/math.6272e937.js',
99
'test/fixtures/math.js.map' : 'test/tmp/withSourceMaps/math.6272e937.js.map',
1010
'test/fixtures/physics.js' : 'test/tmp/withSourceMaps/physics.28cb15fd.js',
11+
'test/fixtures/styles.css' : 'test/tmp/withSourceMaps/styles.a6aa2292.css',
12+
'test/fixtures/styles.css.map' : 'test/tmp/withSourceMaps/styles.a6aa2292.css.map',
13+
'test/fixtures/more-styles.css' : 'test/tmp/withSourceMaps/more-styles.dce8e0e5.css',
1114
'test/fixtures/another.png' : 'test/tmp/another-processed-92279d3f.png'
1215
};
1316

@@ -60,6 +63,27 @@ it('should revision .js file ok without any .map', function () {
6063
assert(revisioned === original);
6164
});
6265

66+
it('should use same revision as .css source for the .map', function () {
67+
var file = 'test/fixtures/styles.css.map';
68+
var original = fs.statSync(file).size;
69+
var revisioned = fs.statSync(hashes[file]).size;
70+
assert(revisioned === original);
71+
});
72+
73+
it('should point the .css sourceMappingURL to the revisioned .map', function () {
74+
var file = 'test/fixtures/styles.css';
75+
var map = 'styles.a6aa2292.css.map';
76+
var revisioned = fs.readFileSync(hashes[file], {encoding: 'utf8'});
77+
assert(revisioned.indexOf('/*# sourceMappingURL=' + map) !== -1);
78+
});
79+
80+
it('should revision .css file ok without any .map', function () {
81+
var file = 'test/fixtures/more-styles.css';
82+
var original = fs.statSync(file).size;
83+
var revisioned = fs.statSync(hashes[file]).size;
84+
assert(revisioned === original);
85+
});
86+
6387
it('should allow a filename processing function', function () {
6488
var file = 'test/fixtures/another.png';
6589
var original = fs.statSync(file).size;

0 commit comments

Comments
 (0)