Skip to content

Commit

Permalink
always return { map, code } object on transform, update apis, change …
Browse files Browse the repository at this point in the history
…sourceMap comment adding, closes #6, #7 and #8
  • Loading branch information
sebmck committed Oct 8, 2014
1 parent f77e7d7 commit 4cb7b78
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 39 deletions.
14 changes: 6 additions & 8 deletions README.md
Expand Up @@ -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;
});
```

Expand All @@ -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",

Expand Down
15 changes: 5 additions & 10 deletions bin/6to5
Expand Up @@ -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");

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Oct 8, 2014

Member

I think it would make more sense to have --source-map and --source-map-inline which are mutually exclusive. Also note the singular source-map to be consistent with the API.

This comment has been minimized.

Copy link
@sebmck

sebmck Oct 8, 2014

Author Contributor

Good call. I'm a bit stressed to get these changes pushed out as soon as possible which is probably to my own detriment.

//commander.option("-w, --watch", "Watch, only works with --out-dir");

Expand Down Expand Up @@ -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
};

Expand All @@ -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);

Expand All @@ -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);
};
Expand Down Expand Up @@ -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("");
Expand Down
4 changes: 2 additions & 2 deletions bin/6to5-node
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions lib/6to5/node.js
Expand Up @@ -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);
});
};

Expand Down
28 changes: 13 additions & 15 deletions lib/6to5/transform.js
Expand Up @@ -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);
});
Expand All @@ -44,25 +41,26 @@ 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;
}

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 };
}
};

Expand All @@ -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);

Expand Down

0 comments on commit 4cb7b78

Please sign in to comment.