add support for custom module formatters - #82
This commit is contained in:
parent
4f15bfcf1d
commit
a1adca6b65
@ -16,19 +16,27 @@ commander.option("-s, --source-maps", "Save source map alongside the compiled co
|
|||||||
commander.option("-f, --filename [filename]", "Filename to use when reading from stdin - this will be used in source-maps, errors etc [stdin]", "stdin");
|
commander.option("-f, --filename [filename]", "Filename to use when reading from stdin - this will be used in source-maps, errors etc [stdin]", "stdin");
|
||||||
commander.option("-w, --watch", "Recompile files on changes");
|
commander.option("-w, --watch", "Recompile files on changes");
|
||||||
|
|
||||||
|
commander.option("-m, --modules [modules]", "Module formatter type to use [common]", "common");
|
||||||
commander.option("-w, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util2.list);
|
commander.option("-w, --whitelist [whitelist]", "Whitelist of transformers to ONLY use", util2.list);
|
||||||
commander.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util2.list);
|
commander.option("-b, --blacklist [blacklist]", "Blacklist of transformers to NOT use", util2.list);
|
||||||
commander.option("-o, --out-file [out]", "Compile all input files into a single file");
|
commander.option("-o, --out-file [out]", "Compile all input files into a single file");
|
||||||
commander.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory");
|
commander.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory");
|
||||||
|
|
||||||
commander.on("--help", function(){
|
commander.on("--help", function(){
|
||||||
console.log(" Transformers:");
|
var outKeys = function (title, obj) {
|
||||||
console.log();
|
console.log(" " + title + ":");
|
||||||
_.each(_.keys(transform.transformers).sort(), function (key) {
|
console.log();
|
||||||
if (key[0] === "_") return;
|
|
||||||
console.log(" - " + key);
|
_.each(_.keys(obj).sort(), function (key) {
|
||||||
});
|
if (key[0] === "_") return;
|
||||||
console.log();
|
console.log(" - " + key);
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log();
|
||||||
|
};
|
||||||
|
|
||||||
|
outKeys("Transformers", transform.transformers);
|
||||||
|
outKeys("Module formatters", transform.moduleFormatters);
|
||||||
});
|
});
|
||||||
|
|
||||||
var pkg = require("../../package.json");
|
var pkg = require("../../package.json");
|
||||||
@ -83,7 +91,8 @@ exports.opts = {
|
|||||||
sourceMapName: commander.outFile,
|
sourceMapName: commander.outFile,
|
||||||
blacklist: commander.blacklist,
|
blacklist: commander.blacklist,
|
||||||
whitelist: commander.whitelist,
|
whitelist: commander.whitelist,
|
||||||
sourceMap: commander.sourceMaps || commander.sourceMapsInline
|
sourceMap: commander.sourceMaps || commander.sourceMapsInline,
|
||||||
|
modules: commander.modules
|
||||||
};
|
};
|
||||||
|
|
||||||
var fn;
|
var fn;
|
||||||
|
|||||||
@ -9,9 +9,11 @@ var b = require("recast").types.builders;
|
|||||||
var _ = require("lodash");
|
var _ = require("lodash");
|
||||||
|
|
||||||
function File(opts) {
|
function File(opts) {
|
||||||
|
this.opts = File.normaliseOptions(opts);
|
||||||
|
this.moduleFormatter = this.getModuleFormatter(opts.modules);
|
||||||
|
|
||||||
this.declarations = {};
|
this.declarations = {};
|
||||||
this.uids = {};
|
this.uids = {};
|
||||||
this.opts = File.normaliseOptions(opts);
|
|
||||||
this.ast = {};
|
this.ast = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,7 +24,8 @@ File.normaliseOptions = function (opts) {
|
|||||||
blacklist: [],
|
blacklist: [],
|
||||||
whitelist: [],
|
whitelist: [],
|
||||||
sourceMap: false,
|
sourceMap: false,
|
||||||
filename: "unknown"
|
filename: "unknown",
|
||||||
|
modules: "common"
|
||||||
});
|
});
|
||||||
|
|
||||||
_.defaults(opts, {
|
_.defaults(opts, {
|
||||||
@ -36,6 +39,12 @@ File.normaliseOptions = function (opts) {
|
|||||||
return opts;
|
return opts;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
File.prototype.getModuleFormatter = function (type) {
|
||||||
|
var ModuleLoader = transform.moduleFormatters[type];
|
||||||
|
if (!ModuleLoader) throw new ReferenceError("unknown module formatter type " + type);
|
||||||
|
return new ModuleLoader(this);
|
||||||
|
};
|
||||||
|
|
||||||
File.prototype.parseShebang = function (code) {
|
File.prototype.parseShebang = function (code) {
|
||||||
var shebangMatch = code.match(SHEBANG_REGEX);
|
var shebangMatch = code.match(SHEBANG_REGEX);
|
||||||
if (shebangMatch) {
|
if (shebangMatch) {
|
||||||
|
|||||||
0
lib/6to5/modules/amd.js
Normal file
0
lib/6to5/modules/amd.js
Normal file
96
lib/6to5/modules/common.js
Normal file
96
lib/6to5/modules/common.js
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
module.exports = CommonJSModuleFormatter;
|
||||||
|
|
||||||
|
var util = require("../util");
|
||||||
|
var b = require("recast").types.builders;
|
||||||
|
|
||||||
|
var getSpecifierName = function (specifier) {
|
||||||
|
return specifier.name || specifier.id;
|
||||||
|
};
|
||||||
|
|
||||||
|
function CommonJSModuleFormatter(file) {
|
||||||
|
this.file = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
CommonJSModuleFormatter.prototype.import = function (node, nodes) {
|
||||||
|
// import "foo";
|
||||||
|
nodes.push(util.template("require", {
|
||||||
|
MODULE_NAME: node.source.raw
|
||||||
|
}, true));
|
||||||
|
};
|
||||||
|
|
||||||
|
CommonJSModuleFormatter.prototype.importSpecifier = function (specifier, node, nodes) {
|
||||||
|
var variableName = getSpecifierName(specifier);
|
||||||
|
var key = specifier.id.name;
|
||||||
|
|
||||||
|
// import foo from "foo";
|
||||||
|
if (specifier.type === "ImportDefaultSpecifier") {
|
||||||
|
key = b.identifier("default");
|
||||||
|
}
|
||||||
|
|
||||||
|
var templateName = "require-assign";
|
||||||
|
|
||||||
|
// import * as bar from "foo";
|
||||||
|
if (specifier.type !== "ImportNamespaceSpecifier") templateName += "-key";
|
||||||
|
|
||||||
|
nodes.push(util.template(templateName, {
|
||||||
|
VARIABLE_NAME: variableName.name,
|
||||||
|
MODULE_NAME: node.source.raw,
|
||||||
|
KEY: key
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
CommonJSModuleFormatter.prototype.export = function (node, nodes) {
|
||||||
|
var declar = node.declaration;
|
||||||
|
|
||||||
|
if (node.default) {
|
||||||
|
util.ensureExpressionType(declar);
|
||||||
|
|
||||||
|
nodes.push(util.template("exports-default", {
|
||||||
|
VALUE: declar
|
||||||
|
}, true));
|
||||||
|
} else {
|
||||||
|
var id = declar.id;
|
||||||
|
if (declar.type === "VariableDeclaration") {
|
||||||
|
id = declar.declarations[0].id;
|
||||||
|
}
|
||||||
|
|
||||||
|
var assign = util.template("exports-assign", {
|
||||||
|
VALUE: id,
|
||||||
|
KEY: id
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
nodes.push(declar);
|
||||||
|
|
||||||
|
if (declar.type === "FunctionDeclaration") {
|
||||||
|
assign._blockHoist = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
nodes.push(assign);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
CommonJSModuleFormatter.prototype.exportSpecifier = function (specifier, node, nodes) {
|
||||||
|
var variableName = getSpecifierName(specifier);
|
||||||
|
|
||||||
|
if (node.source) {
|
||||||
|
if (specifier.type === "ExportBatchSpecifier") {
|
||||||
|
// export * from "foo";
|
||||||
|
nodes.push(util.template("exports-wildcard", {
|
||||||
|
MODULE_NAME: node.source.raw
|
||||||
|
}, true));
|
||||||
|
} else {
|
||||||
|
// export { foo } from "test";
|
||||||
|
nodes.push(util.template("exports-require-assign-key", {
|
||||||
|
VARIABLE_NAME: variableName.name,
|
||||||
|
MODULE_NAME: node.source.raw,
|
||||||
|
KEY: specifier.id
|
||||||
|
}, true));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// export { foo };
|
||||||
|
nodes.push(util.template("exports-assign", {
|
||||||
|
VALUE: specifier.id,
|
||||||
|
KEY: variableName
|
||||||
|
}, true));
|
||||||
|
}
|
||||||
|
};
|
||||||
0
lib/6to5/modules/umd.js
Normal file
0
lib/6to5/modules/umd.js
Normal file
@ -106,6 +106,10 @@ transform.transformers = {
|
|||||||
useStrict: require("./transformers/use-strict")
|
useStrict: require("./transformers/use-strict")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
transform.moduleFormatters = {
|
||||||
|
common: require("./modules/common")
|
||||||
|
};
|
||||||
|
|
||||||
_.each(transform.transformers, function (transformer, key) {
|
_.each(transform.transformers, function (transformer, key) {
|
||||||
transform.transformers[key] = new Transformer(key, transformer);
|
transform.transformers[key] = new Transformer(key, transformer);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -11,13 +11,13 @@ function Transformer(key, transformer) {
|
|||||||
Transformer.normalise = function (transformer) {
|
Transformer.normalise = function (transformer) {
|
||||||
if (_.isFunction(transformer)) {
|
if (_.isFunction(transformer)) {
|
||||||
transformer = { ast: transformer };
|
transformer = { ast: transformer };
|
||||||
} else {
|
|
||||||
_.each(transformer, function (fns, type) {
|
|
||||||
if (type === "ast") return;
|
|
||||||
if (_.isFunction(fns)) fns = { enter: fns };
|
|
||||||
transformer[type] = fns;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_.each(transformer, function (fns, type) {
|
||||||
|
if (_.isFunction(fns)) fns = { enter: fns };
|
||||||
|
transformer[type] = fns;
|
||||||
|
});
|
||||||
|
|
||||||
return transformer;
|
return transformer;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -27,8 +27,8 @@ Transformer.prototype.transform = function (file) {
|
|||||||
var transformer = this.transformer;
|
var transformer = this.transformer;
|
||||||
var ast = file.ast;
|
var ast = file.ast;
|
||||||
|
|
||||||
if (transformer.ast) {
|
if (transformer.ast && transformer.ast.enter) {
|
||||||
transformer.ast(ast, file);
|
transformer.ast.enter(ast, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
var build = function (exit) {
|
var build = function (exit) {
|
||||||
@ -57,6 +57,14 @@ Transformer.prototype.transform = function (file) {
|
|||||||
enter: build(),
|
enter: build(),
|
||||||
exit: build(true)
|
exit: build(true)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (transformer.ast && transformer.ast.exit) {
|
||||||
|
transformer.ast.exit(ast, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file.moduleFormatter.transform) {
|
||||||
|
file.moduleFormatter.transform(ast);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Transformer.prototype.canRun = function (file) {
|
Transformer.prototype.canRun = function (file) {
|
||||||
|
|||||||
@ -1,110 +1,28 @@
|
|||||||
var util = require("../util");
|
var _ = require("lodash");
|
||||||
var b = require("recast").types.builders;
|
|
||||||
var _ = require("lodash");
|
|
||||||
|
|
||||||
exports.ImportDeclaration = function (node) {
|
exports.ImportDeclaration = function (node, parent, file) {
|
||||||
var nodes = [];
|
var nodes = [];
|
||||||
|
|
||||||
if (node.specifiers.length) {
|
if (node.specifiers.length) {
|
||||||
_.each(node.specifiers, function (specifier) {
|
_.each(node.specifiers, function (specifier) {
|
||||||
var variableName = getSpecifierName(specifier);
|
file.moduleFormatter.importSpecifier(specifier, node, nodes);
|
||||||
var key = specifier.id.name;
|
|
||||||
|
|
||||||
// import foo from "foo";
|
|
||||||
if (specifier.type === "ImportDefaultSpecifier") {
|
|
||||||
key = b.identifier("default");
|
|
||||||
}
|
|
||||||
|
|
||||||
var templateName = "require-assign";
|
|
||||||
|
|
||||||
// import * as bar from "foo";
|
|
||||||
if (specifier.type !== "ImportNamespaceSpecifier") templateName += "-key";
|
|
||||||
|
|
||||||
nodes.push(util.template(templateName, {
|
|
||||||
VARIABLE_NAME: variableName.name,
|
|
||||||
MODULE_NAME: node.source.raw,
|
|
||||||
KEY: key
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// import "foo";
|
file.moduleFormatter.import(node, nodes);
|
||||||
nodes.push(util.template("require", {
|
|
||||||
MODULE_NAME: node.source.raw
|
|
||||||
}, true));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nodes;
|
return nodes;
|
||||||
};
|
};
|
||||||
|
|
||||||
var pushExportSpecifiers = function (node, nodes) {
|
exports.ExportDeclaration = function (node, parent, file) {
|
||||||
_.each(node.specifiers, function (specifier) {
|
|
||||||
var variableName = getSpecifierName(specifier);
|
|
||||||
|
|
||||||
if (node.source) {
|
|
||||||
if (specifier.type === "ExportBatchSpecifier") {
|
|
||||||
// export * from "foo";
|
|
||||||
nodes.push(util.template("exports-wildcard", {
|
|
||||||
MODULE_NAME: node.source.raw
|
|
||||||
}, true));
|
|
||||||
} else {
|
|
||||||
// export { foo } from "test";
|
|
||||||
nodes.push(util.template("exports-require-assign-key", {
|
|
||||||
VARIABLE_NAME: variableName.name,
|
|
||||||
MODULE_NAME: node.source.raw,
|
|
||||||
KEY: specifier.id
|
|
||||||
}, true));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// export { foo };
|
|
||||||
nodes.push(util.template("exports-assign", {
|
|
||||||
VALUE: specifier.id,
|
|
||||||
KEY: variableName
|
|
||||||
}, true));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var getSpecifierName = function (specifier) {
|
|
||||||
return specifier.name || specifier.id;
|
|
||||||
};
|
|
||||||
|
|
||||||
var pushExportDeclaration = function (node, parent, nodes) {
|
|
||||||
var declar = node.declaration;
|
|
||||||
|
|
||||||
if (node.default) {
|
|
||||||
util.ensureExpressionType(declar);
|
|
||||||
|
|
||||||
nodes.push(util.template("exports-default", {
|
|
||||||
VALUE: declar
|
|
||||||
}, true));
|
|
||||||
} else {
|
|
||||||
var id = declar.id;
|
|
||||||
if (declar.type === "VariableDeclaration") {
|
|
||||||
id = declar.declarations[0].id;
|
|
||||||
}
|
|
||||||
|
|
||||||
var assign = util.template("exports-assign", {
|
|
||||||
VALUE: id,
|
|
||||||
KEY: id
|
|
||||||
}, true);
|
|
||||||
|
|
||||||
nodes.push(declar);
|
|
||||||
|
|
||||||
if (declar.type === "FunctionDeclaration") {
|
|
||||||
assign._blockHoist = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
nodes.push(assign);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.ExportDeclaration = function (node, parent) {
|
|
||||||
var nodes = [];
|
var nodes = [];
|
||||||
|
|
||||||
if (node.declaration) {
|
if (node.declaration) {
|
||||||
pushExportDeclaration(node, parent, nodes);
|
file.moduleFormatter.export(node, nodes);
|
||||||
} else {
|
} else {
|
||||||
pushExportSpecifiers(node, nodes);
|
_.each(node.specifiers, function (specifier) {
|
||||||
|
file.moduleFormatter.exportSpecifier(specifier, node, nodes);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return nodes;
|
return nodes;
|
||||||
|
|||||||
3
test/fixtures/syntax/modules-common/options.json
vendored
Normal file
3
test/fixtures/syntax/modules-common/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"modules": "common"
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user