Skip to content

Commit

Permalink
Initial support for gulp-sourcemap
Browse files Browse the repository at this point in the history
  • Loading branch information
terinjokes committed May 9, 2014
1 parent 26cce5f commit de7d138
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
26 changes: 10 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
var through = require('through2'),
uglify = require('uglify-js'),
merge = require('deepmerge'),
Vinyl = require('vinyl'),
uglifyError = require('./lib/error.js');

module.exports = function(opt) {
Expand All @@ -25,11 +24,11 @@ module.exports = function(opt) {
});

var mangled,
map,
sourceMap;
originalSourceMap;

if (options.outSourceMap === true) {
options.outSourceMap = file.relative + '.map';
if (file.sourceMap) {
options.outSourceMap = file.relative;
originalSourceMap = file.sourceMap;
}

if (options.preserveComments === 'all') {
Expand All @@ -44,25 +43,20 @@ module.exports = function(opt) {
try {
mangled = uglify.minify(String(file.contents), options);
file.contents = new Buffer(mangled.code);
this.push(file);
} catch (e) {
console.warn('Error caught from uglify: ' + e.message + ' in ' + file.path + '. Returning unminifed code');
this.push(file);
return callback();
}

if (options.outSourceMap) {
sourceMap = JSON.parse(mangled.map);
sourceMap.sources = [ file.relative ];
map = new Vinyl({
cwd: file.cwd,
base: file.base,
path: file.path + '.map',
contents: new Buffer(JSON.stringify(sourceMap))
});
this.push(map);
if (file.sourceMap) {
file.sourceMap = JSON.parse(mangled.map);
file.sourceMap.sourcesContent = originalSourceMap.sourcesContent;
file.sourceMap.sources = originalSourceMap.sources;
}

this.push(file);

callback();
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"vinyl": "~0.2.3"
},
"devDependencies": {
"inline-source-map": "^0.3.0",
"tape": "~2.4.2"
},
"engines": {
Expand Down
45 changes: 45 additions & 0 deletions test/sourcemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';
var test = require('tape'),
Vinyl = require('vinyl'),
gulpUglify = require('../'),
uglifyjs = require('uglify-js'),
inlineSourceMap = require('inline-source-map');

var testContentsInput = '"use strict"; (function(console, first, second) { console.log(first + second) }(5, 10))';
var testContentsExpected = uglifyjs.minify(testContentsInput, {fromString: true}).code;
var testSourceMap = inlineSourceMap().addGeneratedMappings('test1.js', testContentsInput, {line:0, column:0}).addSourceContent('test1.js', testContentsInput).toJSON();

var testFile1 = new Vinyl({
cwd: "/home/terin/broken-promises/",
base: "/home/terin/broken-promises/test",
path: "/home/terin/broken-promises/test/test1.js",
contents: new Buffer(testContentsInput)
});
testFile1.sourceMap = testSourceMap;

test('should minify files', function(t) {
t.plan(13);

var stream = gulpUglify();

stream.on('data', function(newFile) {
t.ok(newFile, 'emits a file');
t.ok(newFile.path, 'file has a path');
t.ok(newFile.relative, 'file has relative path information');
t.ok(newFile.contents, 'file has contents');

t.ok(newFile instanceof Vinyl, 'file is Vinyl');
t.ok(newFile.contents instanceof Buffer, 'file contents are a buffer');

t.equals(String(newFile.contents), testContentsExpected);

t.ok(newFile.sourceMap, 'has a source map');
t.equals(newFile.sourceMap.version, 3, 'source map has expected version');
t.ok(Array.isArray(newFile.sourceMap.sources), 'source map has sources array');
t.ok(Array.isArray(newFile.sourceMap.names), 'source maps has names array');
t.ok(Array.isArray(newFile.sourceMap.sourcesContent), 'source map has sourceContent');
t.ok(newFile.sourceMap.mappings, 'source map has mappings');
});

stream.write(testFile1);
});

0 comments on commit de7d138

Please sign in to comment.