Skip to content

Commit

Permalink
Merge pull request #327 from RyanFrench/master
Browse files Browse the repository at this point in the history
Add 'sourceMapUrl' option
  • Loading branch information
vladikoff committed Mar 4, 2016
2 parents e2e019d + e2da335 commit 2c17823
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 6 additions & 0 deletions docs/uglify-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 3 additions & 1 deletion tasks/lib/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,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 = {
Expand Down
14 changes: 10 additions & 4 deletions tasks/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,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;
}
}

// Minify files, warn and fail on error.
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/expected/sourcemap_customUrl.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/uglify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 2c17823

Please sign in to comment.