Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #19 - Add support for source-map generation #51

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions bin/ngmin
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,52 @@ var program = require('commander'),
program
.version(require('../package.json').version)
.usage('<infile> <outfile>')
.option('|--source-map <path>', "Specify an output file where to generate source map.", null)
.option('|--source-map-root <path>', "The path to the original source to be included in the source map.", null)
.option('|--in-source-map <path>', "Input source map, useful if you're compressing JS that was generated from some other original code.", null)
.parse(process.argv);

if (program.args.length !== 0 && program.args.length !== 2) {
console.error('ngmin should be called with an input and output file, or no arguments if using stdio');
process.exit(1);
}

var options = {
sourceMap: program.sourceMap,
sourceMapRoot: program.sourceMapRoot,
sourceMapIn: program.inSourceMap
};

if (program.args.length === 2) {
var infile = program.args[0];
var outfile = program.args[1];
options.sourceFile = infile;

try {
var content = fs.readFileSync(infile, 'utf-8');
} catch (e) {
console.error('Error opening: ' + infile);
process.exit(1);
}
var generatedCode = ngmin.annotate(content);
var generated = ngmin.annotate(content, options);

try {
fs.writeFileSync(outfile, generatedCode);
if (generated.map) {
generated.code += '\n\n//# sourceMappingURL='+options.sourceMap+'\n';
}
fs.writeFileSync(outfile, generated.code);
} catch (e) {
console.error('Error writing to: ' + outfile);
process.exit(1);
}

if (generated.map) {
try {
fs.writeFileSync(options.sourceMap, generated.map);
} catch (e) {
console.error('Error writing to: ' + options.sourceMap);
process.exit(1);
}
}
} else {
// else use stdio
var buffer = '';
Expand All @@ -45,6 +65,18 @@ if (program.args.length === 2) {
});

process.stdin.on('end', function() {
process.stdout.write(ngmin.annotate(buffer));
var generated = ngmin.annotate(buffer, options);
if (generated.map) {
generated.code += '\n\n//# sourceMappingURL='+options.sourceMap+'\n';
}
process.stdout.write(generated.code);
if (generated.map) {
try {
fs.writeFileSync(options.sourceMap, generated.map);
} catch (e) {
console.error('Error writing to: ' + options.sourceMap);
process.exit(1);
}
}
});
}
35 changes: 31 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@

var esprima = require('esprima'),
escodegen = require('escodegen'),
astral = require('astral')();
astral = require('astral')(),
sourceMap = require('source-map'),
fs = require('fs');

// register angular annotator in astral
require('astral-angular-annotate')(astral);

var annotate = exports.annotate = function (inputCode) {
var annotate = exports.annotate = function (inputCode, options) {
options = options || {};
options.sourceMap = options.sourceMap || null;
options.sourceMapRoot = options.sourceMapRoot || null;
options.sourceMapIn = options.sourceMapIn || null;
options.sourceFile = options.sourceFile || null;

var mapIn = null;

if (options.sourceMapIn) {
mapIn = new sourceMap.SourceMapConsumer(JSON.parse(fs.readFileSync(options.sourceMapIn)));
}

var ast = esprima.parse(inputCode, {
tolerant: true
tolerant: true,
loc: true
});

astral.run(ast);
Expand All @@ -19,8 +33,21 @@ var annotate = exports.annotate = function (inputCode) {
indent: {
style: ' '
}
}
},
sourceMap: options.sourceMap && options.sourceFile ? options.sourceFile : undefined,
sourceMapRoot: options.sourceMapRoot,
sourceMapWithCode: true
});

if ('string' === typeof generatedCode) {
generatedCode = { code: generatedCode };
}

if (generatedCode.map) {
if (mapIn) {
generatedCode.map.applySourceMap(mapIn);
}
}

return generatedCode;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"escodegen": "~0.0.15",
"esprima": "~1.0.2",
"commander": "~1.1.1",
"clone": "~0.1.6"
"clone": "~0.1.6",
"source-map": "~0.1.29"
},
"devDependencies": {
"should": "~1.2.1",
Expand Down
14 changes: 7 additions & 7 deletions test/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('annotate', function () {
service('MyCtrl', function ($scope) {});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
service('myService', ['dep', function (dep) {}]).
service('MyCtrl', ['$scope', function ($scope) {}]);
Expand All @@ -41,7 +41,7 @@ describe('annotate', function () {
service('MyCtrl', function ($scope) {});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
service('myService', ['dep', function (dep) {}]).
service('myService2', ['dep', function (dep) {}]).
Expand All @@ -59,7 +59,7 @@ describe('annotate', function () {
service('MyCtrl', function ($scope) {});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
constant('myConstant', 'someConstant').
constant('otherConstant', 'otherConstant').
Expand All @@ -77,7 +77,7 @@ describe('annotate', function () {
service('MyCtrl', function ($scope) {});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
value('myConstant', 'someConstant').
value('otherConstant', 'otherConstant').
Expand All @@ -95,7 +95,7 @@ describe('annotate', function () {
service('MyCtrl', function ($scope) {});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
value('myConstant', 'someConstant').
service('myService1', ['dep', function (dep) {}]).
Expand All @@ -111,7 +111,7 @@ describe('annotate', function () {
factory('b', function ($scope){});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
var mod = angular.module('chain', []);
mod.factory('a', ['$scope', function($scope){}]).
factory('b', ['$scope', function($scope){}]);
Expand All @@ -125,7 +125,7 @@ describe('annotate', function () {
mod.factory('b', function ($scope){});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
var mod = angular.module('chain', []).
factory('a', ['$scope', function($scope){}]);
mod.factory('b', ['$scope', function($scope){}]);
Expand Down
4 changes: 2 additions & 2 deletions test/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('annotate', function () {
});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
directive('myDir', function () {
return {
Expand All @@ -54,7 +54,7 @@ describe('annotate', function () {
});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
directive('myDir', ['$window', function ($window) {
return {
Expand Down
4 changes: 2 additions & 2 deletions test/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('annotate', function () {
});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
define(["./thing"], function(thing) {
angular.module('myMod', []).
controller('MyCtrl', ['$scope', function ($scope) {}]);
Expand All @@ -43,7 +43,7 @@ describe('annotate', function () {

});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
define(["./thing"], function(thing) {
var myMod = angular.module('myMod', []);
myMod.controller('MyCtrl', ['$scope', function ($scope) {}]);
Expand Down
10 changes: 5 additions & 5 deletions test/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('annotate', function () {
myMod.controller('MyCtrl', function ($scope) {});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
var myMod = angular.module('myMod', []);
myMod.controller('MyCtrl', [
'$scope',
Expand All @@ -41,7 +41,7 @@ describe('annotate', function () {
myMod.controller('MyCtrl', function ($scope) {});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
var myMod;
myMod = angular.module('myMod', []);
myMod.controller('MyCtrl', [
Expand All @@ -59,7 +59,7 @@ describe('annotate', function () {
myMod.provider('MyService', { $get: function(service) {} });
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
var myMod;
myMod = angular.module('myMod', []);
myMod.provider('MyService', {
Expand All @@ -78,7 +78,7 @@ describe('annotate', function () {
myMod3.controller('MyCtrl', function ($scope) {});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
var myMod = angular.module('myMod', []);
var myMod2 = myMod, myMod3;
myMod3 = myMod2;
Expand All @@ -96,7 +96,7 @@ describe('annotate', function () {
myOtherMod.controller('MyCtrl', function ($scope) {});
};
var annotated = annotate(fn);
annotated.should.equal(stringifyFunctionBody(fn));
annotated.code.should.equal(stringifyFunctionBody(fn));
});


Expand Down
4 changes: 2 additions & 2 deletions test/route-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('annotate', function () {
});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('path', {
Expand Down Expand Up @@ -64,7 +64,7 @@ describe('annotate', function () {
});
});

annotated.should.equal(stringifyFunctionBody(function () {
annotated.code.should.equal(stringifyFunctionBody(function () {
angular.module('myMod', []).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.
Expand Down
Loading