From e2da335029a4d5297bcf5a5c305b6f55dc48abfd Mon Sep 17 00:00:00 2001 From: Ryan French Date: Sun, 26 Apr 2015 16:35:15 +1200 Subject: [PATCH] Add 'sourceMapUrl' option Added an option to override the calcuated sourceMappingUrl value in the minified output --- Gruntfile.js | 8 ++++++++ docs/uglify-options.md | 6 ++++++ tasks/lib/uglify.js | 4 +++- tasks/uglify.js | 14 ++++++++++---- test/fixtures/expected/sourcemap_customUrl.js | 2 ++ test/uglify_test.js | 1 + 6 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/expected/sourcemap_customUrl.js diff --git a/Gruntfile.js b/Gruntfile.js index 02429525..2ac2b100 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -184,6 +184,14 @@ module.exports = function(grunt) { sourceMapRoot: 'https://github.com/RReverser/grunt-contrib-uglify/tree/master/tmp' } }, + sourcemap_customUrl: { + src: 'test/fixtures/src/simple.js', + dest: 'tmp/sourcemap_customUrl.js', + options: { + sourceMap: true, + sourceMapUrl: 'http://www.test.com/test/sourcemap_customUrl.js.map' + } + }, sourcemap_functionName: { src: 'test/fixtures/src/simple.js', dest: 'tmp/sourcemap_functionName.js', diff --git a/docs/uglify-options.md b/docs/uglify-options.md index 4f33351c..7d7f26bd 100644 --- a/docs/uglify-options.md +++ b/docs/uglify-options.md @@ -71,6 +71,12 @@ With this option you can customize root URL that browser will use when looking f If the sources are not absolute URLs after prepending of the `sourceMapRoot`, the sources are resolved relative to the source map. +## sourceMapUrl +Type: `String` +Default: `undefined` + +Override the calculated value for `sourceMappingURL` in the source map. This is useful if the source map location is not relative to the base path of the minified file, i.e. when using a CDN + #### enclose Type: `Object` Default: `undefined` diff --git a/tasks/lib/uglify.js b/tasks/lib/uglify.js index 3e597d5d..b7c47a02 100644 --- a/tasks/lib/uglify.js +++ b/tasks/lib/uglify.js @@ -177,7 +177,9 @@ exports.init = function(grunt) { // Add the source map reference to the end of the file if (options.sourceMap) { // Set all paths to forward slashes for use in the browser - min += '\n//# sourceMappingURL=' + uriPath(options.destToSourceMap); + var sourceMappingURL; + sourceMappingURL = options.destToSourceMap.match(/^http[s]?\:\/\//) === null ? uriPath(options.destToSourceMap) : options.destToSourceMap; + min += '\n//# sourceMappingURL=' + sourceMappingURL; } var result = { diff --git a/tasks/uglify.js b/tasks/uglify.js index e5420283..dc154cc3 100644 --- a/tasks/uglify.js +++ b/tasks/uglify.js @@ -122,10 +122,16 @@ module.exports = function(grunt) { // Calculate the path from the dest file to the sourcemap for the // sourceMappingURL reference - if (options.sourceMap) { - var destToSourceMapPath = relativePath(f.dest, options.generatedSourceMapName); - var sourceMapBasename = path.basename(options.generatedSourceMapName); - options.destToSourceMap = destToSourceMapPath + sourceMapBasename; + // If sourceMapUrl is defined, use this instead + if(options.sourceMap) { + var destToSourceMapPath, sourceMapBasename; + if (!options.sourceMapUrl) { + destToSourceMapPath = relativePath(f.dest, options.generatedSourceMapName); + sourceMapBasename = path.basename(options.generatedSourceMapName); + options.destToSourceMap = destToSourceMapPath + sourceMapBasename; + } else { + options.destToSourceMap = options.sourceMapUrl; + } } if (options.screwIE8) { diff --git a/test/fixtures/expected/sourcemap_customUrl.js b/test/fixtures/expected/sourcemap_customUrl.js new file mode 100644 index 00000000..c063d4b6 --- /dev/null +++ b/test/fixtures/expected/sourcemap_customUrl.js @@ -0,0 +1,2 @@ +function longFunctionC(a,b){return longNameA+longNameB+a+b}var longNameA=1,longNameB=2,result=longFunctionC(3,4); +//# sourceMappingURL=http://www.test.com/test/sourcemap_customUrl.js.map \ No newline at end of file diff --git a/test/uglify_test.js b/test/uglify_test.js index 9fae9df2..253c76dd 100644 --- a/test/uglify_test.js +++ b/test/uglify_test.js @@ -32,6 +32,7 @@ exports.contrib_uglify = { 'sourcemap_customName.js', 'sourcemap_customRoot.js', 'sourcemap_customRoot.js.map', + 'sourcemap_customUrl.js', 'sourcemap_functionName.js', 'sourcemap_functionName.js.fn.map', path.join('deep', 'directory', 'location', 'source_map.js.map'),