Merge pull request #945 from neVERberleRfellerER/helper-globals-fix

Add choice of output format to babel-external-helpers
This commit is contained in:
Sebastian McKenzie 2015-03-08 02:56:30 +11:00
commit 7eb169a894
4 changed files with 74 additions and 6 deletions

View File

@ -1,4 +1,13 @@
#!/usr/bin/env node
var commander = require("commander");
var util = require("../lib/babel/util");
var runtime = require("../lib/babel/build-external-helpers");
console.log(runtime());
commander.option("-l, --whitelist [whitelist]", "Whitelist of helpers to ONLY include", util.list);
commander.option("-t, --output-type [type]", "Type of output (global|umd|var)", "global");
commander.usage("[options]");
commander.parse(process.argv);
console.log(runtime(commander.whitelist, commander.outputType));

View File

@ -1,11 +1,10 @@
import buildHelpers from "./build-helpers";
import generator from "./generation";
import * as messages from "./messages";
import * as util from "./util";
import t from "./types";
export default function (whitelist) {
var namespace = t.identifier("babelHelpers");
function buildGlobal(namespace, builder) {
var body = [];
var container = t.functionExpression(null, [t.identifier("global")], t.blockStatement(body));
var tree = t.program([t.expressionStatement(t.callExpression(container, [util.template("self-global")]))]);
@ -17,7 +16,55 @@ export default function (whitelist) {
)
]));
buildHelpers(body, namespace, whitelist);
builder(body);
return tree;
}
function buildUmd(namespace, builder) {
var body = [];
body.push(t.variableDeclaration("var", [
t.variableDeclarator(namespace, t.identifier("global"))
]));
builder(body);
var container = util.template("umd-commonjs-strict", {
FACTORY_PARAMETERS: t.identifier("global"),
BROWSER_ARGUMENTS: t.assignmentExpression("=", t.memberExpression(t.identifier("root"), namespace), t.objectExpression({})),
COMMON_ARGUMENTS: t.identifier("exports"),
AMD_ARGUMENTS: t.arrayExpression([t.literal("exports")]),
FACTORY_BODY: body,
UMD_ROOT: t.identifier("this")
});
return t.program([container]);
}
function buildVar(namespace, builder) {
var body = [];
body.push(t.variableDeclaration("var", [
t.variableDeclarator(namespace, t.objectExpression({}))
]));
builder(body);
return t.program(body);
}
export default function (whitelist, outputType = "global") {
var namespace = t.identifier("babelHelpers");
var builder = function (body) {
return buildHelpers(body, namespace, whitelist);
};
var tree;
if (outputType === "global") {
tree = buildGlobal(namespace, builder);
} else if (outputType === "umd") {
tree = buildUmd(namespace, builder);
} else if (outputType === "var") {
tree = buildVar(namespace, builder);
} else {
throw new Error(messages.get("unsupportedOutputType", outputType));
}
return generator(tree).code;
};

View File

@ -20,7 +20,8 @@ export var messages = {
didYouMean: "Did you mean $1?",
evalInStrictMode: "eval is not allowed in strict mode",
codeGeneratorDeopt: "Note: The code generator has deoptimised the styling of $1 as it exceeds the max of $2.",
missingTemplatesDirectory: "no templates directory - this is most likely the result of a broken `npm publish`. Please report to https://github.com/babel/babel/issues"
missingTemplatesDirectory: "no templates directory - this is most likely the result of a broken `npm publish`. Please report to https://github.com/babel/babel/issues",
unsupportedOutputType: "Unsupported output type $1"
};
export function get(key) {

View File

@ -0,0 +1,11 @@
(function (root, factory) {
if (typeof define === "function" && define.amd) {
define(AMD_ARGUMENTS, factory);
} else if (typeof exports === 'object') {
factory(COMMON_ARGUMENTS);
} else {
factory(BROWSER_ARGUMENTS);
}
})(UMD_ROOT, function (FACTORY_PARAMETERS) {
FACTORY_BODY
});