I have a Grunt project that uses both Browserify and Uglify. Here are the core bits of it:
browserify: {
myapp: {
options: {
transform: ['babelify'],
browserifyOptions: {
debug: true
},
},
src: 'src/index.js',
dest: 'build/myapp.js'
}
},
uglify: {
options: {
sourceMap: true,
banner: bannerContent
},
target: {
src: 'build/myapp.js',
dest: 'build/myapp.min.js'
}
},
It seems to generate a myapp.min.js.map file but it no longer has the raw sources in the source-map that existed prior to the Browserification.
Here's what the resultant source-map file contains:
{
"version":3,
"sources":[
"myapp.js"
],
"names":[
...
...
...
],
"mappings":".........",
"file":"myapp.min.js"
}
I've tried using the uglifyify transform for Browserify but that does not seem to generate as small files as the Uglify task.
I've also bumped all my dependencies to the latest but I haven't been able to resolve this issue.
grunt-contrib-uglifyhas asourceMapInoption that allows you to specify the location of an input source map file from an earlier compilation - which in your scenario is thebrowserifytask.However, whilst setting
browserifyOptions: { debug: true }in yourbrowserifytask does generate an inline source map in the resultant.jsfile (i.e. inbuild/myapp.js), the crux of the problem is twofold:We don't have an external source map file that we can configure the
sourceMapInoption of the subsequentgrunt-contrib-uglifytask to utilize.grunt-browserifydoesn't provide a feature to create an external.mapfile, it only creates them inline (see here)To address the aforementioned issue consider utilizing grunt-extract-sourcemap to extract the inline source map from
build/myapp.js(i.e. from the output file generated by yourbrowserifytask) after it has been produced.Gruntfile.js
The following gist shows how your Gruntfile.js should be configured:
Explanation
First the
browserifytask is invoked which outputs a new file (i.e.build/myapp.js) containing your bundled JavaScript and an "inlined" source map info. If you were to inspect the content ofbuild/myapp.jsat this stage it includes something like the following at the end:Next the
extract_sourcemaptask is invoked. This essentially extracts the "inlined" source map info frombuild/myapp.jsand writes it to a new file namedmyapp.js.mapwhich is saved in yourbuilddirectory.The original "inlined" source map info in
build/myapp.jsis replaced with a link to the newly generated source map file, i.e.myapp.js.map. If you inspect the content ofbuild/myapp.jsyou'll now notice the following at the end of the file instead:Lastly the
uglifytask is invoked. Notice how itssourceMapInoption is configured to readbuild/myapp.js.map, i.e the source map file we generated at step 2.This task creates your desired
build/myapp.min.jsfile containing; your minified JS, and a link to a newly generated source map filebuild/myapp.min.js.map.Note The final resultant file (i.e.
build/myapp.min.js) now correctly maps back to the originalsrc/index.jsfile and any file(s) thatindex.jsitself may haveimport'ed orrequire()'d