diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 5ab96ea05e..dabcf7adb8 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -1,5 +1,7 @@ module.exports = File; +var SHEBANG_REGEX = /^\#\!.*/; + var transform = require("./transform"); var util = require("./util"); var _ = require("lodash"); @@ -32,12 +34,23 @@ File.normaliseOptions = function (opts) { return opts; }; -File.prototype.parse = function (code) { - // remove shebang - code = code.replace(/^\#\!.*/, ""); +File.prototype.parseShebang = function (code) { + var shebangMatch = code.match(SHEBANG_REGEX); + if (shebangMatch) { + this.shebang = shebangMatch[0]; + // remove shebang + code = code.replace(SHEBANG_REGEX, ""); + } + + return code; +}; + +File.prototype.parse = function (code) { var self = this; + code = this.parseShebang(code); + return util.parse(this.opts, code, function (tree) { return self.transform(tree); }); @@ -55,6 +68,10 @@ File.prototype.transform = function (ast) { var result = util.generate(ast, opts); + if (this.shebang) { + result.code = this.shebang + result.code; + } + if (opts.sourceMap === "inline") { result.code += "\n" + util.sourceMapToComment(result.map); }