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

Updated PR#68 to also change the file reference inside of a source map file to point to file revisioned source file; updates a test #69

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
17 changes: 14 additions & 3 deletions tasks/filerev.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,24 @@ module.exports = function (grunt) {
var sourceMap = false;
if (ext === '.js' || ext === '.css') {
var map = file + '.map';
resultPath += '.map';
var resultPathMap = resultPath + '.map';
if (grunt.file.exists(map)) {
if (move) {
fs.renameSync(map, resultPath);
fs.renameSync(map, resultPathMap);
} else {
grunt.file.copy(map, resultPath);
grunt.file.copy(map, resultPathMap);
}

// rewrite the sourceMappingURL in files
var fileContents = grunt.file.read(resultPath, {encoding: 'utf8'});
Copy link
Member

Choose a reason for hiding this comment

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

the default encoding is utf8, so the option here is moot. same with below.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was wondering about that, since the grunt.file docs aren't super clear. Good to know, thanks.

Copy link
Author

Choose a reason for hiding this comment

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

I will update, thanks.

var newSrcMap = fileContents.replace('//# sourceMappingURL=' + path.basename(file) + '.map', '//# sourceMappingURL=' + path.basename(resultPathMap));
Copy link
Member

Choose a reason for hiding this comment

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

This will fail when the sourceMappingURL is a relative path, not just a filename.

Use a regex to only replace the filename.

// update file reference inside source map file
var mapFileContents = grunt.file.readJSON(resultPathMap, { encoding: 'utf-8' });
mapFileContents.file = path.basename(resultPath) + '.map';
Copy link
Member

Choose a reason for hiding this comment

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

same here about relative paths.


// write files
Copy link
Member

Choose a reason for hiding this comment

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

useless comment

grunt.file.write(resultPath, newSrcMap, {encoding: 'utf8'});
grunt.file.write(resultPathMap, JSON.stringify(mapFileContents), { encoding: 'utf-8' });
sourceMap = true;
}
}
Expand Down
12 changes: 10 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var fs = require('fs');
var path = require('path');
var assert = require('assert');

var hashes = {
Expand Down Expand Up @@ -40,11 +41,18 @@ it('should allow sources defined with expand', function () {

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

it('should point the .js sourceMappingURL to the revisioned .map', function() {
var file = 'test/fixtures/math.js';
var map = 'math.2f56179e.js.map';
var revisioned = fs.readFileSync(hashes[file], {encoding: 'utf8'});
assert(revisioned.indexOf('//# sourceMappingURL=' + map) !== -1);
Copy link
Member

Choose a reason for hiding this comment

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

assert.notStrictEqual

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, I wasn't aware of that. @sgnl would you mind making that change?

Copy link
Author

Choose a reason for hiding this comment

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

I'll add it.

});

it('should revision .js file ok without any .map', function () {
var file = 'test/fixtures/physics.js';
var original = fs.statSync(file).size;
Expand Down