diff --git a/README.md b/README.md index 2cdbd63e4b..65adcf94a4 100644 --- a/README.md +++ b/README.md @@ -110,12 +110,12 @@ Compile and run `test.js`. ```javascript var to5 = require("6to5"); -to5.transform("code();", options); +to5.transform("code();", options).code; -to5.transformFileSync("filename.js", options); - -to5.transformFile("filename.js", options, function (err, data) { +to5.transformFileSync("filename.js", options).code; +to5.transformFile("filename.js", options, function (err, result) { + result.code; }); ``` @@ -132,12 +132,10 @@ to5.transformFile("filename.js", options, function (err, data) { // See `blacklist` for naming scheme. whitelist: [], - // Append source map and comment to bottom of returned output. + // If truthy, adds a `map` property to returned output. + // If set to "comment", the sourceMappingURL directive is added at the bottom sourceMap: false, - // Returns an object `{ code: "", map: {} }` instead of an appended string. - sourceMapObject: false, - // Filename for use in errors etc. filename: "unknown", diff --git a/bin/6to5 b/bin/6to5 index 752959da34..095ce1de96 100755 --- a/bin/6to5 +++ b/bin/6to5 @@ -9,7 +9,7 @@ var to5 = require("../lib/6to5/node"); var fs = require("fs"); var _ = require("lodash"); -commander.option("-c, --source-maps-comment", "Generate source map and append it with a sourceMappingURL comment alongside the compiled output"); +commander.option("-t, --source-map-type [type]", "Specify a custom sourcemap type such as `comment`"); commander.option("-s, --source-maps", "Save source map alongside the compiled code when using --out-file and --out-dir flags"); //commander.option("-w, --watch", "Watch, only works with --out-dir"); @@ -58,7 +58,7 @@ var readdirFilter = function (filename) { var mainOpts = { blacklist: commander.blacklist, whitelist: commander.whitelist, - sourceMap: commander.sourceMapsComment || commander.sourceMaps, + sourceMap: commander.sourceMapType || commander.sourceMaps, tolerant: commander.tolerant }; @@ -73,10 +73,6 @@ var compile = function (filename) { }; if (commander.outDir) { - if (commander.sourceMaps) { - mainOpts.sourceMapObject = true; - } - var write = function (src, relative) { var data = compile(src); @@ -85,12 +81,11 @@ if (commander.outDir) { var up = path.normalize(dest + "/.."); mkdirp.sync(up); - if (mainOpts.sourceMapObject) { + if (commander.sourceMaps && !commander.sourceMapType) { fs.writeFileSync(dest + ".map", data.map.toJSON()); - data = data.code; } - fs.writeFileSync(dest, data); + fs.writeFileSync(dest, data.code); console.log(src + " -> " + dest); }; @@ -130,7 +125,7 @@ if (commander.outDir) { }); _.each(filenames, function (filename) { - data.push(compile(filename) + "\n"); + data.push(compile(filename).code + "\n"); }); data = data.join(""); diff --git a/bin/6to5-node b/bin/6to5-node index e862736934..d1180ae02b 100755 --- a/bin/6to5-node +++ b/bin/6to5-node @@ -26,7 +26,7 @@ var _eval = function (code, filename) { }; if (commander.eval) { - var code = to5.transform(commander.eval, { filename: "eval" }); + var code = to5.transform(commander.eval, { filename: "eval" }).code; var result = _eval(code, "eval"); if (commander.print) console.log(result); @@ -58,7 +58,7 @@ function replEval(code, context, filename, callback) { try { code = code.slice(1, -2); // remove "(" and "\n)" - code = to5.transform(code, { filename: filename }); + code = to5.transform(code, { filename: filename }).code; result = vm.runInThisContext(code, filename); } catch (e) { diff --git a/lib/6to5/node.js b/lib/6to5/node.js index 6818a153c2..6b53d00cd3 100644 --- a/lib/6to5/node.js +++ b/lib/6to5/node.js @@ -18,18 +18,18 @@ exports.transformFile = function (filename, opts, callback) { opts.filename = filename; - fs.readFile(filename, function (err, raw) { + fs.readFile(filename, function (err, code) { if (err) return callback(err); - var code; + var result; try { - code = transform(raw, opts); + result = transform(code, opts); } catch (err) { return callback(err); } - callback(null, code); + callback(null, result); }); }; diff --git a/lib/6to5/transform.js b/lib/6to5/transform.js index 50ce6accb9..b25c48fb00 100644 --- a/lib/6to5/transform.js +++ b/lib/6to5/transform.js @@ -9,16 +9,13 @@ var transform = module.exports = function (code, opts) { code = (code || "") + ""; _.defaults(opts, { - sourceMapObject: false, - blacklist: [], - whitelist: [], - sourceMap: false, - filename: "unknown", - format: {} + blacklist: [], + whitelist: [], + sourceMap: false, + filename: "unknown", + format: {} }); - if (opts.sourceMapObject) opts.sourceMap = true; - return util.parse(opts.filename, code, function (tree) { return transform._run(code, tree, opts); }); @@ -44,11 +41,12 @@ transform._run = function (code, tree, opts) { }); var genOpts = { + file: path.basename(opts.filename), format: opts.format }; if (opts.sourceMap) { - genOpts.sourceMap = path.basename(opts.filename); + genOpts.sourceMap = genOpts.file; genOpts.sourceContent = code; genOpts.sourceMapWithCode = true; } @@ -56,13 +54,13 @@ transform._run = function (code, tree, opts) { var result = util.generate(tree, genOpts); if (opts.sourceMap) { - if (opts.sourceMapObject) { - return result; - } else { - return result.code + "\n" + util.sourceMapToComment(result.map) + "\n"; + if (opts.sourceMap === "comment") { + result.code += "\n" + util.sourceMapToComment(result.map); } + + return result; } else { - return result + "\n"; + return { code: result, map: null }; } }; @@ -82,7 +80,7 @@ transform.test = function (actual, expect, opts) { _.defaults(opts, { filename: "test" }); var actualCode = actual.code.trim(); - var transformedCode = transform(actualCode, opts); + var transformedCode = transform(actualCode, opts).code; var actualAst = util.parse(actual.filename, transformedCode); actualCode = util.generate(actualAst);