always return { map, code } object on transform, update apis, change sourceMap comment adding, closes #6, #7 and #8

This commit is contained in:
Sebastian McKenzie 2014-10-08 23:49:19 +11:00
parent f77e7d71d2
commit 4cb7b786dd
5 changed files with 30 additions and 39 deletions

View File

@ -110,12 +110,12 @@ Compile and run `test.js`.
```javascript ```javascript
var to5 = require("6to5"); var to5 = require("6to5");
to5.transform("code();", options); to5.transform("code();", options).code;
to5.transformFileSync("filename.js", options); to5.transformFileSync("filename.js", options).code;
to5.transformFile("filename.js", options, function (err, data) {
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. // See `blacklist` for naming scheme.
whitelist: [], 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, sourceMap: false,
// Returns an object `{ code: "", map: {} }` instead of an appended string.
sourceMapObject: false,
// Filename for use in errors etc. // Filename for use in errors etc.
filename: "unknown", filename: "unknown",

View File

@ -9,7 +9,7 @@ var to5 = require("../lib/6to5/node");
var fs = require("fs"); var fs = require("fs");
var _ = require("lodash"); 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("-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"); //commander.option("-w, --watch", "Watch, only works with --out-dir");
@ -58,7 +58,7 @@ var readdirFilter = function (filename) {
var mainOpts = { var mainOpts = {
blacklist: commander.blacklist, blacklist: commander.blacklist,
whitelist: commander.whitelist, whitelist: commander.whitelist,
sourceMap: commander.sourceMapsComment || commander.sourceMaps, sourceMap: commander.sourceMapType || commander.sourceMaps,
tolerant: commander.tolerant tolerant: commander.tolerant
}; };
@ -73,10 +73,6 @@ var compile = function (filename) {
}; };
if (commander.outDir) { if (commander.outDir) {
if (commander.sourceMaps) {
mainOpts.sourceMapObject = true;
}
var write = function (src, relative) { var write = function (src, relative) {
var data = compile(src); var data = compile(src);
@ -85,12 +81,11 @@ if (commander.outDir) {
var up = path.normalize(dest + "/.."); var up = path.normalize(dest + "/..");
mkdirp.sync(up); mkdirp.sync(up);
if (mainOpts.sourceMapObject) { if (commander.sourceMaps && !commander.sourceMapType) {
fs.writeFileSync(dest + ".map", data.map.toJSON()); fs.writeFileSync(dest + ".map", data.map.toJSON());
data = data.code;
} }
fs.writeFileSync(dest, data); fs.writeFileSync(dest, data.code);
console.log(src + " -> " + dest); console.log(src + " -> " + dest);
}; };
@ -130,7 +125,7 @@ if (commander.outDir) {
}); });
_.each(filenames, function (filename) { _.each(filenames, function (filename) {
data.push(compile(filename) + "\n"); data.push(compile(filename).code + "\n");
}); });
data = data.join(""); data = data.join("");

View File

@ -26,7 +26,7 @@ var _eval = function (code, filename) {
}; };
if (commander.eval) { 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"); var result = _eval(code, "eval");
if (commander.print) console.log(result); if (commander.print) console.log(result);
@ -58,7 +58,7 @@ function replEval(code, context, filename, callback) {
try { try {
code = code.slice(1, -2); // remove "(" and "\n)" 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); result = vm.runInThisContext(code, filename);
} catch (e) { } catch (e) {

View File

@ -18,18 +18,18 @@ exports.transformFile = function (filename, opts, callback) {
opts.filename = filename; opts.filename = filename;
fs.readFile(filename, function (err, raw) { fs.readFile(filename, function (err, code) {
if (err) return callback(err); if (err) return callback(err);
var code; var result;
try { try {
code = transform(raw, opts); result = transform(code, opts);
} catch (err) { } catch (err) {
return callback(err); return callback(err);
} }
callback(null, code); callback(null, result);
}); });
}; };

View File

@ -9,16 +9,13 @@ var transform = module.exports = function (code, opts) {
code = (code || "") + ""; code = (code || "") + "";
_.defaults(opts, { _.defaults(opts, {
sourceMapObject: false, blacklist: [],
blacklist: [], whitelist: [],
whitelist: [], sourceMap: false,
sourceMap: false, filename: "unknown",
filename: "unknown", format: {}
format: {}
}); });
if (opts.sourceMapObject) opts.sourceMap = true;
return util.parse(opts.filename, code, function (tree) { return util.parse(opts.filename, code, function (tree) {
return transform._run(code, tree, opts); return transform._run(code, tree, opts);
}); });
@ -44,11 +41,12 @@ transform._run = function (code, tree, opts) {
}); });
var genOpts = { var genOpts = {
file: path.basename(opts.filename),
format: opts.format format: opts.format
}; };
if (opts.sourceMap) { if (opts.sourceMap) {
genOpts.sourceMap = path.basename(opts.filename); genOpts.sourceMap = genOpts.file;
genOpts.sourceContent = code; genOpts.sourceContent = code;
genOpts.sourceMapWithCode = true; genOpts.sourceMapWithCode = true;
} }
@ -56,13 +54,13 @@ transform._run = function (code, tree, opts) {
var result = util.generate(tree, genOpts); var result = util.generate(tree, genOpts);
if (opts.sourceMap) { if (opts.sourceMap) {
if (opts.sourceMapObject) { if (opts.sourceMap === "comment") {
return result; result.code += "\n" + util.sourceMapToComment(result.map);
} else {
return result.code + "\n" + util.sourceMapToComment(result.map) + "\n";
} }
return result;
} else { } else {
return result + "\n"; return { code: result, map: null };
} }
}; };
@ -82,7 +80,7 @@ transform.test = function (actual, expect, opts) {
_.defaults(opts, { filename: "test" }); _.defaults(opts, { filename: "test" });
var actualCode = actual.code.trim(); var actualCode = actual.code.trim();
var transformedCode = transform(actualCode, opts); var transformedCode = transform(actualCode, opts).code;
var actualAst = util.parse(actual.filename, transformedCode); var actualAst = util.parse(actual.filename, transformedCode);
actualCode = util.generate(actualAst); actualCode = util.generate(actualAst);