UNPKG

3.65 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4// istanbul ignore next
5
6function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj["default"] = obj; return newObj; } }
7
8// istanbul ignore next
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
12var _generation = require("../generation");
13
14var _generation2 = _interopRequireDefault(_generation);
15
16var _messages = require("../messages");
17
18var messages = _interopRequireWildcard(_messages);
19
20var _util = require("../util");
21
22var util = _interopRequireWildcard(_util);
23
24var _transformationFile = require("../transformation/file");
25
26var _transformationFile2 = _interopRequireDefault(_transformationFile);
27
28var _lodashCollectionEach = require("lodash/collection/each");
29
30var _lodashCollectionEach2 = _interopRequireDefault(_lodashCollectionEach);
31
32var _types = require("../types");
33
34var t = _interopRequireWildcard(_types);
35
36/**
37 * [Please add a description.]
38 */
39
40function buildGlobal(namespace, builder) {
41 var body = [];
42 var container = t.functionExpression(null, [t.identifier("global")], t.blockStatement(body));
43 var tree = t.program([t.expressionStatement(t.callExpression(container, [util.template("helper-self-global")]))]);
44
45 body.push(t.variableDeclaration("var", [t.variableDeclarator(namespace, t.assignmentExpression("=", t.memberExpression(t.identifier("global"), namespace), t.objectExpression([])))]));
46
47 builder(body);
48
49 return tree;
50}
51
52/**
53 * [Please add a description.]
54 */
55
56function buildUmd(namespace, builder) {
57 var body = [];
58 body.push(t.variableDeclaration("var", [t.variableDeclarator(namespace, t.identifier("global"))]));
59
60 builder(body);
61
62 var container = util.template("umd-commonjs-strict", {
63 FACTORY_PARAMETERS: t.identifier("global"),
64 BROWSER_ARGUMENTS: t.assignmentExpression("=", t.memberExpression(t.identifier("root"), namespace), t.objectExpression({})),
65 COMMON_ARGUMENTS: t.identifier("exports"),
66 AMD_ARGUMENTS: t.arrayExpression([t.literal("exports")]),
67 FACTORY_BODY: body,
68 UMD_ROOT: t.identifier("this")
69 });
70 return t.program([container]);
71}
72
73/**
74 * [Please add a description.]
75 */
76
77function buildVar(namespace, builder) {
78 var body = [];
79 body.push(t.variableDeclaration("var", [t.variableDeclarator(namespace, t.objectExpression({}))]));
80 builder(body);
81 return t.program(body);
82}
83
84/**
85 * [Please add a description.]
86 */
87
88function buildHelpers(body, namespace, whitelist) {
89 _lodashCollectionEach2["default"](_transformationFile2["default"].helpers, function (name) {
90 if (whitelist && whitelist.indexOf(name) === -1) return;
91
92 var key = t.identifier(t.toIdentifier(name));
93 body.push(t.expressionStatement(t.assignmentExpression("=", t.memberExpression(namespace, key), util.template("helper-" + name))));
94 });
95}
96
97/**
98 * [Please add a description.]
99 */
100
101exports["default"] = function (whitelist) {
102 var outputType = arguments.length <= 1 || arguments[1] === undefined ? "global" : arguments[1];
103
104 var namespace = t.identifier("babelHelpers");
105
106 var builder = function builder(body) {
107 return buildHelpers(body, namespace, whitelist);
108 };
109
110 var tree;
111
112 var build = ({
113 global: buildGlobal,
114 umd: buildUmd,
115 "var": buildVar
116 })[outputType];
117
118 if (build) {
119 tree = build(namespace, builder);
120 } else {
121 throw new Error(messages.get("unsupportedOutputType", outputType));
122 }
123
124 return _generation2["default"](tree).code;
125};
126
127module.exports = exports["default"];
\No newline at end of file