Permalink
Browse files

Simplified source map configuration

sourceMap is now a Boolean that determines whether or not to generate
the map. sourceMapName can be used to customize the name and location.

Closes gh-122.
  • Loading branch information...
1 parent 4f2a094 commit d85ba2cf83c5d25b26aa07cb7c7494d7e98cc385 @jmeas jmeas committed with tkellen Jan 16, 2014
Showing with 194 additions and 182 deletions.
  1. +1 −0 .gitignore
  2. +70 −74 Gruntfile.js
  3. +7 −21 README.md
  4. +6 −20 docs/uglify-options.md
  5. +20 −7 tasks/lib/uglify.js
  6. +47 −24 tasks/uglify.js
  7. +0 −1 test/fixtures/expected/compress_mangle_sourcemap
  8. +1 −0 test/fixtures/expected/deep/directory/location/source_map.js.map
  9. +0 −1 test/fixtures/expected/multiple_sourcemaps1.map
  10. +0 −1 test/fixtures/expected/multiple_sourcemaps2.map
  11. +1 −0 test/fixtures/expected/source_map_custom_name
  12. +1 −1 test/fixtures/expected/{sourcemapurl.js → sourcemap_basic.js}
  13. +1 −0 test/fixtures/expected/sourcemap_basic.map
  14. +1 −1 test/fixtures/expected/{multiple_sourcemaps1.js → sourcemap_customDir.js}
  15. +2 −0 test/fixtures/expected/sourcemap_customName.js
  16. +2 −0 test/fixtures/expected/sourcemap_functionName.js
  17. +1 −0 test/fixtures/expected/sourcemap_functionName.js.fn.map
  18. +0 −1 test/fixtures/expected/sourcemap_prefix
  19. +0 −1 test/fixtures/expected/sourcemap_sources
  20. +1 −0 test/fixtures/expected/sourcemap_sources.map
  21. +0 −1 test/fixtures/expected/sourcemapin
  22. +1 −1 test/fixtures/expected/sourcemapin.js
  23. +1 −0 test/fixtures/expected/sourcemapin.map
  24. +2 −0 test/fixtures/expected/sourcemaps_multiple1.js
  25. +1 −0 test/fixtures/expected/sourcemaps_multiple1.map
  26. +2 −0 test/fixtures/expected/sourcemaps_multiple1_fnName.js
  27. +1 −0 test/fixtures/expected/sourcemaps_multiple1_fnName.js.fn.map
  28. +1 −1 test/fixtures/expected/{multiple_sourcemaps2.js → sourcemaps_multiple2.js}
  29. +1 −0 test/fixtures/expected/sourcemaps_multiple2.map
  30. +2 −0 test/fixtures/expected/sourcemaps_multiple2_fnName.js
  31. +1 −0 test/fixtures/expected/sourcemaps_multiple2_fnName.js.fn.map
  32. +1 −1 test/fixtures/src/simple2.map
  33. +18 −25 test/uglify_test.js
View
@@ -2,3 +2,4 @@ node_modules
npm-debug.log
tmp
.idea
+.DS_Store
View
@@ -96,43 +96,6 @@ module.exports = function(grunt) {
mangle: false
}
},
- compress_mangle_sourcemap: {
- files: {
- '/dev/null': ['test/fixtures/src/simple.js']
- },
- options: {
- sourceMap: 'tmp/compress_mangle_sourcemap'
- }
- },
- sourcemapin: {
- files: {
- 'tmp/sourcemapin.js': ['test/fixtures/src/simple2.js']
- },
- options: {
- mangle: false,
- banner: '// Hello World\n',
- sourceMap: 'tmp/sourcemapin',
- sourceMapIn: 'test/fixtures/src/simple2.map',
- sourceMapRoot: 'http://local.host/js/'
- }
- },
- sourcemapurl: {
- files: {
- 'tmp/sourcemapurl.js': ['test/fixtures/src/simple.js']
- },
- options: {
- sourceMappingURL: 'js/sourcemapurl.js.map'
- }
- },
- sourcemap_sources: {
- files: {
- '/dev/null': ['test/fixtures/src/simple.js']
- },
- options: {
- sourceMap: 'tmp/sourcemap_sources',
- sourceMapIncludeSources: true
- }
- },
comments: {
src: 'test/fixtures/src/comments.js',
dest: 'tmp/comments.js',
@@ -158,50 +121,82 @@ module.exports = function(grunt) {
exportAll: true
}
},
- sourcemap_prefix: {
+ sourcemap_basic: {
+ src: 'test/fixtures/src/simple.js',
+ dest: 'tmp/sourcemap_basic.js',
+ options: {
+ sourceMap: true
+ }
+ },
+ sourcemap_customName: {
+ src: 'test/fixtures/src/simple.js',
+ dest: 'tmp/sourcemap_customName.js',
+ options: {
+ sourceMap: true,
+ sourceMapName: 'tmp/source_map_custom_name'
+ }
+ },
+ sourcemap_customDir: {
+ src: 'test/fixtures/src/simple.js',
+ dest: 'tmp/sourcemap_customDir.js',
+ options: {
+ sourceMap: true,
+ sourceMapName: 'tmp/deep/directory/location/source_map.js.map'
+ }
+ },
+ sourcemap_functionName: {
+ src: 'test/fixtures/src/simple.js',
+ dest: 'tmp/sourcemap_functionName.js',
+ options: {
+ sourceMap: true,
+ sourceMapName: function( dest ) {
+ return dest + ".fn.map";
+ }
+ }
+ },
+ sourcemap_multiple: {
files: {
- '/dev/null': ['test/fixtures/src/simple.js']
+ 'tmp/sourcemaps_multiple1.js': ['test/fixtures/src/simple.js'],
+ 'tmp/sourcemaps_multiple2.js': ['test/fixtures/src/comments.js']
},
options: {
- sourceMap: 'tmp/sourcemap_prefix',
- sourceMapPrefix: 3
+ sourceMap: true
}
},
- multiple_sourcemaps: {
+ sourcemap_multipleFunctionNames: {
files: {
- 'tmp/multiple_sourcemaps1.js': ['test/fixtures/src/simple.js'],
- 'tmp/multiple_sourcemaps2.js': ['test/fixtures/src/comments.js']
+ 'tmp/sourcemaps_multiple1_fnName.js': ['test/fixtures/src/simple.js'],
+ 'tmp/sourcemaps_multiple2_fnName.js': ['test/fixtures/src/comments.js']
},
options: {
- sourceMap: function(dest) {
- return dest.replace(/\.js$/,".map");
- },
- sourceMappingURL: function(dest) {
- return dest.replace(/\.js$/,".mapurl");
+ sourceMap: true,
+ sourceMapName: function( dest ) {
+ return dest+'.fn.map';
}
}
},
- sourcemap_in_generator_single_src: {
- files: [{
- expand: true,
- flatten: true,
- src: ['test/fixtures/expected/multiple_sourcemaps*.js'],
- dest: 'tmp',
- ext: '.min.js'
- }],
+ sourcemapin: {
+ files: {
+ 'tmp/sourcemapin.js': ['test/fixtures/src/simple2.js']
+ },
options: {
- sourceMap: function (dest) { return dest.replace(/\.js$/, '') + '.map'; },
- sourceMapIn: function (src) { return src.replace(/\.js$/, '') + '.map'; }
+ mangle: false,
+ banner: '// Hello World\n',
+ sourceMap: true,
+ sourceMapIn: function() {
+ return 'test/fixtures/src/simple2.map';
+ }
}
},
- sourcemap_in_generator_multi_src: {
- src: 'test/fixtures/expected/multiple_sourcemaps*.js',
- dest: 'tmp/multiple_sourcemaps_all.min.js',
- options: {
- sourceMap: function (dest) { return dest.replace(/\.js$/, '') + '.map'; },
- sourceMapIn: function (src) { return src.replace(/\.js$/, '') + '.map'; }
- }
- }
+ sourcemap_sources: {
+ files: {
+ 'tmp/sourcemap_sources.js': ['test/fixtures/src/simple.js']
+ },
+ options: {
+ sourceMap: true,
+ sourceMapIncludeSources: true
+ }
+ },
},
// Unit tests.
@@ -249,18 +244,19 @@ module.exports = function(grunt) {
'uglify:compress_mangle_except',
'uglify:compress_mangle_beautify',
'uglify:multifile',
- 'uglify:compress_mangle_sourcemap',
'uglify:sourcemap_sources',
- 'uglify:sourcemapin',
- 'uglify:sourcemapurl',
'uglify:comments',
'uglify:wrap',
'uglify:exportAll',
- 'uglify:sourcemap_prefix',
- 'uglify:multiple_sourcemaps',
'uglify:enclose',
- 'uglify:sourcemap_in_generator_single_src',
- 'expectFail:uglify:sourcemap_in_generator_multi_src', // fail case
+ 'uglify:sourcemap_basic',
+ 'uglify:sourcemap_customName',
+ 'uglify:sourcemap_customDir',
+ 'uglify:sourcemap_functionName',
+ 'uglify:sourcemap_multiple',
+ 'uglify:sourcemap_multipleFunctionNames',
+ 'uglify:sourcemapin',
+ 'uglify:sourcemap_sources',
'nodeunit'
]);
View
@@ -66,17 +66,16 @@ Gzipped: 20084 bytes.
```
#### sourceMap
-Type: `String` `Function`
-Default: `undefined`
+Type: `Boolean`
+Default: `false`
-The location to output the sourcemap. If a function is provided, the uglify destination is passed as the argument
-and the return value will be used as the sourceMap name.
+If `true`, a source map file will be generated in the same directory as the `dest` file. By default it will have the same basename as the `dest` file, but with a `.map` extension.
-#### sourceMapRoot
-Type: `String`
+#### sourceMapName
+Type: `String` `Function`
Default: `undefined`
-The location where your source files can be found. This sets the sourceRoot field in the source map.
+To customize the name or location of the generated source map, pass a string to indicate where to write the source map to. If a function is provided, the uglify destination is passed as the argument and the return value will be used as the file name.
#### sourceMapIn
Type: `String` `Function`
@@ -86,19 +85,6 @@ The location of an input source map from an earlier compilation, e.g. from Coffe
uglify source is passed as the argument and the return value will be used as the sourceMap name. This only makes sense
when there's one source file.
-#### sourceMappingURL
-Type: `String` `Function`
-Default: `undefined`
-
-The location of your sourcemap. Defaults to the location you use for sourceMap, override if you need finer control. Provide
-a function to dynamically generate the sourceMappingURL based off the destination.
-
-#### sourceMapPrefix
-Type: `Number`
-Default: `undefined`
-
-The number of directories to drop from the path prefix when declaring files in the source map.
-
#### sourceMapIncludeSources
Type: `Boolean`
Default: `false`
@@ -389,4 +375,4 @@ grunt.initConfig({
Task submitted by ["Cowboy" Ben Alman](http://benalman.com)
-*This file was generated on Thu Jan 09 2014 14:39:39.*
+*This file was generated on Thu Jan 16 2014 10:40:08.*
@@ -38,17 +38,16 @@ Gzipped: 20084 bytes.
```
## sourceMap
-Type: `String` `Function`
-Default: `undefined`
+Type: `Boolean`
+Default: `false`
-The location to output the sourcemap. If a function is provided, the uglify destination is passed as the argument
-and the return value will be used as the sourceMap name.
+If `true`, a source map file will be generated in the same directory as the `dest` file. By default it will have the same basename as the `dest` file, but with a `.map` extension.
-## sourceMapRoot
-Type: `String`
+## sourceMapName
+Type: `String` `Function`
Default: `undefined`
-The location where your source files can be found. This sets the sourceRoot field in the source map.
+To customize the name or location of the generated source map, pass a string to indicate where to write the source map to. If a function is provided, the uglify destination is passed as the argument and the return value will be used as the file name.
## sourceMapIn
Type: `String` `Function`
@@ -58,19 +57,6 @@ The location of an input source map from an earlier compilation, e.g. from Coffe
uglify source is passed as the argument and the return value will be used as the sourceMap name. This only makes sense
when there's one source file.
-## sourceMappingURL
-Type: `String` `Function`
-Default: `undefined`
-
-The location of your sourcemap. Defaults to the location you use for sourceMap, override if you need finer control. Provide
-a function to dynamically generate the sourceMappingURL based off the destination.
-
-## sourceMapPrefix
-Type: `Number`
-Default: `undefined`
-
-The number of directories to drop from the path prefix when declaring files in the source map.
-
## sourceMapIncludeSources
Type: `Boolean`
Default: `false`
View
@@ -8,6 +8,8 @@
'use strict';
+var path = require('path');
+
// External libs.
var UglifyJS = require('uglify-js');
var fs = require('fs');
@@ -32,11 +34,18 @@ exports.init = function(grunt) {
// Grab and parse all source files
files.forEach(function(file){
+
var code = grunt.file.read(file);
- if (typeof options.sourceMapPrefix !== 'undefined') {
- file = file.replace(/^\/+/, "").split(/\/+/).slice(options.sourceMapPrefix).join("/");
- }
totalCode += code;
+
+ // The src file name must be relative to the source map for things to work
+ var basename = path.basename(file);
+ var fileDir = path.dirname(file);
+ var sourceMapDir = path.dirname(options.generatedSourceMapName);
+ var relativePath = path.relative(sourceMapDir, fileDir);
+
+ file = relativePath + path.sep + basename;
+
sourcesContent[file] = code;
topLevel = UglifyJS.parse(code, {
filename: file,
@@ -98,8 +107,9 @@ exports.init = function(grunt) {
var min = output.get();
- if (options.sourceMappingURL || options.sourceMap) {
- min += "\n//# sourceMappingURL=" + (options.sourceMappingURL || options.sourceMap);
+ // Add the source map reference to the end of the file
+ if (options.sourceMap) {
+ min += "\n//# sourceMappingURL="+options.destToSourceMap;
}
var result = {
@@ -150,15 +160,18 @@ exports.init = function(grunt) {
if (options.sourceMap) {
+
+ var destBasename = path.basename(dest);
+ var destPath = path.dirname(dest);
var sourceMapIn;
if (options.sourceMapIn) {
sourceMapIn = grunt.file.readJSON(options.sourceMapIn);
}
outputOptions.source_map = UglifyJS.SourceMap({
- file: dest,
- root: options.sourceMapRoot,
+ file: destBasename,
orig: sourceMapIn
});
+
}
if (options.indentLevel !== undefined) {
Oops, something went wrong.

0 comments on commit d85ba2c

Please sign in to comment.