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

Commit 2eeaa11

Browse files
committed
update sourcemap urls for css files
1 parent e7ddab5 commit 2eeaa11

8 files changed

+46
-3
lines changed

Gruntfile.js

+1-1
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

+1
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

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ 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');
78

89
module.exports = function (grunt) {
910
grunt.registerMultiTask('filerev', 'File revisioning based on content hashing', function () {
@@ -84,10 +85,14 @@ module.exports = function (grunt) {
8485
} else {
8586
grunt.file.copy(map, resultPathMap);
8687
}
87-
88+
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+
// use regex that matches single-line and multi-line sourcemap urls
92+
// note: this will ignore inline base64-encoded sourcemaps
93+
var matches = convert.mapFileCommentRegex.exec(fileContents);
94+
var sourceMapFile = matches[1] || matches[2]; // 1st is single line, 2nd is multiline
95+
var newSrcMap = fileContents.replace(sourceMapFile, path.basename(resultPathMap));
9196
grunt.file.write(resultPath, newSrcMap, {encoding: 'utf8'});
9297
sourceMap = true;
9398
}

test/fixtures/inline.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/more-styles.css

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

test/fixtures/styles.css

+1
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

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test.js

+32
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ 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',
14+
'test/fixtures/inline.js' : 'test/tmp/withSourceMaps/inline.8b435ef2.js',
1115
'test/fixtures/another.png' : 'test/tmp/another-processed-92279d3f.png'
1216
};
1317

@@ -60,6 +64,34 @@ it('should revision .js file ok without any .map', function () {
6064
assert(revisioned === original);
6165
});
6266

67+
it('should use same revision as .css source for the .map', function () {
68+
var file = 'test/fixtures/styles.css.map';
69+
var original = fs.statSync(file).size;
70+
var revisioned = fs.statSync(hashes[file]).size;
71+
assert(revisioned === original);
72+
});
73+
74+
it('should point the .css sourceMappingURL to the revisioned .map', function () {
75+
var file = 'test/fixtures/styles.css';
76+
var map = 'styles.a6aa2292.css.map';
77+
var revisioned = fs.readFileSync(hashes[file], {encoding: 'utf8'});
78+
assert(revisioned.indexOf('/*# sourceMappingURL=' + map) !== -1);
79+
});
80+
81+
it('should revision .css file ok without any .map', function () {
82+
var file = 'test/fixtures/more-styles.css';
83+
var original = fs.statSync(file).size;
84+
var revisioned = fs.statSync(hashes[file]).size;
85+
assert(revisioned === original);
86+
});
87+
88+
it('should ignore inline base64-encoded sourcemaps', function () {
89+
var file = 'test/fixtures/inline.js';
90+
var original = fs.statSync(file).size;
91+
var revisioned = fs.statSync(hashes[file]).size;
92+
assert(revisioned === original);
93+
});
94+
6395
it('should allow a filename processing function', function () {
6496
var file = 'test/fixtures/another.png';
6597
var original = fs.statSync(file).size;

0 commit comments

Comments
 (0)