Skip to content

Commit

Permalink
Simplified source map configuration
Browse files Browse the repository at this point in the history
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 gruntjsgh-122.
  • Loading branch information
jamesplease authored and Tyler Kellen committed Jan 16, 2014
1 parent 4f2a094 commit d85ba2c
Show file tree
Hide file tree
Showing 33 changed files with 194 additions and 182 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
npm-debug.log
tmp
.idea
.DS_Store
144 changes: 70 additions & 74 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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.
Expand Down Expand Up @@ -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'
]);

Expand Down
28 changes: 7 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
Expand Down Expand Up @@ -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.*
26 changes: 6 additions & 20 deletions docs/uglify-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
Expand Down
27 changes: 20 additions & 7 deletions tasks/lib/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

'use strict';

var path = require('path');

// External libs.
var UglifyJS = require('uglify-js');
var fs = require('fs');
Expand All @@ -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,
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit d85ba2c

Please sign in to comment.