I just started using Grunt. I try to remove a set of files using Grunt and then create a revision of this file (with grunt-filerev). Everything is fine except for the source files for the file.
I clean the files into 1 file called shared.min.js. I also create a sourcemap for this file, and filerev creates the patch file and the source map for the patch file. This means that it will generate 4 files:
- shared.min.js
- shared.min.js.map
- shared.min.153987b3.js
- shared.min.153987b3.js.map
The problem is that shared.min.153987b3.js.map uses shared.min.js as the source, I think it should be shared.min.153987b3.js. As you can see in my Grunt file, I dirtyly fixed this with the fixFilerevSourcemap task.
Then the following problem occurred: shared.min.153987b3.js uses shared.min.js.map as the source sourcemap, I think it should be shared.min.153987b3.js.map. Should I messily fix this? Or is there a better solution?
I have the following Grunt configuration:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
sourceMap: true,
sourceMapIncludeSources: true
},
dist: {
files: {
'somedir/shared.min.js': ['otherdir/file1.js', 'otherdir/file2.js'],
}
}
},
filerev: {
options: {
sourceMap: true
},
scripts: {
src: ['somedir/shared.min.js'],
dest: 'somedir/'
}
}
});
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-filerev');
grunt.registerTask('fixFilerevSourcemap', 'Blaat', function() {
var revisionedSource;
for (var file in grunt.filerev.summary) {
var revisionedFile = grunt.filerev.summary[file];
if (revisionedFile.substr(revisionedFile.length - 3) == 'map') {
if (null == revisionedSource) {
throw "No revisionedSource found!";
}
var mapData = grunt.file.readJSON(revisionedFile);
mapData.file = basename(revisionedSource);
grunt.file.write(revisionedFile, JSON.stringify(mapData));
} else {
revisionedSource = basename(revisionedFile);
}
}
});
grunt.registerTask('default', ['uglify', 'filerev', 'fixFilerevSourcemap']);
function basename(path, suffix) {
var b = path;
var lastChar = b.charAt(b.length - 1);
if (lastChar === '/' || lastChar === '\\') {
b = b.slice(0, -1);
}
b = b.replace(/^.*[\/\\]/g, '');
if (typeof suffix === 'string' && b.substr(b.length - suffix.length) == suffix) {
b = b.substr(0, b.length - suffix.length);
}
return b;
}
};
source
share