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

More stable sourcemap filerevving #80

Merged
merged 1 commit into from
Mar 26, 2015
Merged
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
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = function (grunt) {
withSourceMaps: {
expand: true,
cwd: 'test/fixtures',
src: ['*.js'],
src: ['*.js', '*.css'],
dest: 'test/tmp/withSourceMaps'
}
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
],
"dependencies": {
"chalk": "^1.0.0",
"convert-source-map": "^1.0.0",
"each-async": "^0.1.3"
},
"devDependencies": {
Expand Down
7 changes: 6 additions & 1 deletion tasks/filerev.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var path = require('path');
var fs = require('fs');
var chalk = require('chalk');
var eachAsync = require('each-async');
var convert = require('convert-source-map');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this empty line.

module.exports = function (grunt) {
grunt.registerMultiTask('filerev', 'File revisioning based on content hashing', function () {
Expand Down Expand Up @@ -87,7 +88,11 @@ module.exports = function (grunt) {

// rewrite the sourceMappingURL in files
var fileContents = grunt.file.read(resultPath, {encoding: 'utf8'});
var newSrcMap = fileContents.replace('//# sourceMappingURL=' + path.basename(file) + '.map', '//# sourceMappingURL=' + path.basename(resultPathMap));
// use regex that matches single-line and multi-line sourcemap urls
// note: this will ignore inline base64-encoded sourcemaps
var matches = convert.mapFileCommentRegex.exec(fileContents);
var sourceMapFile = matches[1] || matches[2]; // 1st is single line, 2nd is multiline
var newSrcMap = fileContents.replace(sourceMapFile, path.basename(resultPathMap));
grunt.file.write(resultPath, newSrcMap, {encoding: 'utf8'});
sourceMap = true;
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/inline.js

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

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

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

32 changes: 32 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ var hashes = {
'test/fixtures/math.js' : 'test/tmp/withSourceMaps/math.6272e937.js',
'test/fixtures/math.js.map' : 'test/tmp/withSourceMaps/math.6272e937.js.map',
'test/fixtures/physics.js' : 'test/tmp/withSourceMaps/physics.28cb15fd.js',
'test/fixtures/styles.css' : 'test/tmp/withSourceMaps/styles.a6aa2292.css',
'test/fixtures/styles.css.map' : 'test/tmp/withSourceMaps/styles.a6aa2292.css.map',
'test/fixtures/more-styles.css' : 'test/tmp/withSourceMaps/more-styles.dce8e0e5.css',
'test/fixtures/inline.js' : 'test/tmp/withSourceMaps/inline.8b435ef2.js',
'test/fixtures/another.png' : 'test/tmp/another-processed-92279d3f.png'
};

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

it('should use same revision as .css source for the .map', function () {
var file = 'test/fixtures/styles.css.map';
var original = fs.statSync(file).size;
var revisioned = fs.statSync(hashes[file]).size;
assert(revisioned === original);
});

it('should point the .css sourceMappingURL to the revisioned .map', function () {
var file = 'test/fixtures/styles.css';
var map = 'styles.a6aa2292.css.map';
var revisioned = fs.readFileSync(hashes[file], {encoding: 'utf8'});
assert(revisioned.indexOf('/*# sourceMappingURL=' + map) !== -1);
});

it('should revision .css file ok without any .map', function () {
var file = 'test/fixtures/more-styles.css';
var original = fs.statSync(file).size;
var revisioned = fs.statSync(hashes[file]).size;
assert(revisioned === original);
});

it('should ignore inline base64-encoded sourcemaps', function () {
var file = 'test/fixtures/inline.js';
var original = fs.statSync(file).size;
var revisioned = fs.statSync(hashes[file]).size;
assert(revisioned === original);
});

it('should allow a filename processing function', function () {
var file = 'test/fixtures/another.png';
var original = fs.statSync(file).size;
Expand Down