Projekt

Obecné

Profil

Stáhnout (11.6 KB) Statistiky
| Větev: | Revize:
1
"use strict";
2

    
3
exports.__esModule = true;
4

    
5
var _objectWithoutProperties2 = require("babel-runtime/helpers/objectWithoutProperties");
6

    
7
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
8

    
9
var _stringify = require("babel-runtime/core-js/json/stringify");
10

    
11
var _stringify2 = _interopRequireDefault(_stringify);
12

    
13
var _assign = require("babel-runtime/core-js/object/assign");
14

    
15
var _assign2 = _interopRequireDefault(_assign);
16

    
17
var _getIterator2 = require("babel-runtime/core-js/get-iterator");
18

    
19
var _getIterator3 = _interopRequireDefault(_getIterator2);
20

    
21
var _typeof2 = require("babel-runtime/helpers/typeof");
22

    
23
var _typeof3 = _interopRequireDefault(_typeof2);
24

    
25
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
26

    
27
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
28

    
29
var _node = require("../../../api/node");
30

    
31
var context = _interopRequireWildcard(_node);
32

    
33
var _plugin2 = require("../../plugin");
34

    
35
var _plugin3 = _interopRequireDefault(_plugin2);
36

    
37
var _babelMessages = require("babel-messages");
38

    
39
var messages = _interopRequireWildcard(_babelMessages);
40

    
41
var _index = require("./index");
42

    
43
var _resolvePlugin = require("../../../helpers/resolve-plugin");
44

    
45
var _resolvePlugin2 = _interopRequireDefault(_resolvePlugin);
46

    
47
var _resolvePreset = require("../../../helpers/resolve-preset");
48

    
49
var _resolvePreset2 = _interopRequireDefault(_resolvePreset);
50

    
51
var _cloneDeepWith = require("lodash/cloneDeepWith");
52

    
53
var _cloneDeepWith2 = _interopRequireDefault(_cloneDeepWith);
54

    
55
var _clone = require("lodash/clone");
56

    
57
var _clone2 = _interopRequireDefault(_clone);
58

    
59
var _merge = require("../../../helpers/merge");
60

    
61
var _merge2 = _interopRequireDefault(_merge);
62

    
63
var _config2 = require("./config");
64

    
65
var _config3 = _interopRequireDefault(_config2);
66

    
67
var _removed = require("./removed");
68

    
69
var _removed2 = _interopRequireDefault(_removed);
70

    
71
var _buildConfigChain = require("./build-config-chain");
72

    
73
var _buildConfigChain2 = _interopRequireDefault(_buildConfigChain);
74

    
75
var _path = require("path");
76

    
77
var _path2 = _interopRequireDefault(_path);
78

    
79
function _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; } }
80

    
81
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
82

    
83
var OptionManager = function () {
84
  function OptionManager(log) {
85
    (0, _classCallCheck3.default)(this, OptionManager);
86

    
87
    this.resolvedConfigs = [];
88
    this.options = OptionManager.createBareOptions();
89
    this.log = log;
90
  }
91

    
92
  OptionManager.memoisePluginContainer = function memoisePluginContainer(fn, loc, i, alias) {
93
    for (var _iterator = OptionManager.memoisedPlugins, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
94
      var _ref;
95

    
96
      if (_isArray) {
97
        if (_i >= _iterator.length) break;
98
        _ref = _iterator[_i++];
99
      } else {
100
        _i = _iterator.next();
101
        if (_i.done) break;
102
        _ref = _i.value;
103
      }
104

    
105
      var cache = _ref;
106

    
107
      if (cache.container === fn) return cache.plugin;
108
    }
109

    
110
    var obj = void 0;
111

    
112
    if (typeof fn === "function") {
113
      obj = fn(context);
114
    } else {
115
      obj = fn;
116
    }
117

    
118
    if ((typeof obj === "undefined" ? "undefined" : (0, _typeof3.default)(obj)) === "object") {
119
      var _plugin = new _plugin3.default(obj, alias);
120
      OptionManager.memoisedPlugins.push({
121
        container: fn,
122
        plugin: _plugin
123
      });
124
      return _plugin;
125
    } else {
126
      throw new TypeError(messages.get("pluginNotObject", loc, i, typeof obj === "undefined" ? "undefined" : (0, _typeof3.default)(obj)) + loc + i);
127
    }
128
  };
129

    
130
  OptionManager.createBareOptions = function createBareOptions() {
131
    var opts = {};
132

    
133
    for (var _key in _config3.default) {
134
      var opt = _config3.default[_key];
135
      opts[_key] = (0, _clone2.default)(opt.default);
136
    }
137

    
138
    return opts;
139
  };
140

    
141
  OptionManager.normalisePlugin = function normalisePlugin(plugin, loc, i, alias) {
142
    plugin = plugin.__esModule ? plugin.default : plugin;
143

    
144
    if (!(plugin instanceof _plugin3.default)) {
145
      if (typeof plugin === "function" || (typeof plugin === "undefined" ? "undefined" : (0, _typeof3.default)(plugin)) === "object") {
146
        plugin = OptionManager.memoisePluginContainer(plugin, loc, i, alias);
147
      } else {
148
        throw new TypeError(messages.get("pluginNotFunction", loc, i, typeof plugin === "undefined" ? "undefined" : (0, _typeof3.default)(plugin)));
149
      }
150
    }
151

    
152
    plugin.init(loc, i);
153

    
154
    return plugin;
155
  };
156

    
157
  OptionManager.normalisePlugins = function normalisePlugins(loc, dirname, plugins) {
158
    return plugins.map(function (val, i) {
159
      var plugin = void 0,
160
          options = void 0;
161

    
162
      if (!val) {
163
        throw new TypeError("Falsy value found in plugins");
164
      }
165

    
166
      if (Array.isArray(val)) {
167
        plugin = val[0];
168
        options = val[1];
169
      } else {
170
        plugin = val;
171
      }
172

    
173
      var alias = typeof plugin === "string" ? plugin : loc + "$" + i;
174

    
175
      if (typeof plugin === "string") {
176
        var pluginLoc = (0, _resolvePlugin2.default)(plugin, dirname);
177
        if (pluginLoc) {
178
          plugin = require(pluginLoc);
179
        } else {
180
          throw new ReferenceError(messages.get("pluginUnknown", plugin, loc, i, dirname));
181
        }
182
      }
183

    
184
      plugin = OptionManager.normalisePlugin(plugin, loc, i, alias);
185

    
186
      return [plugin, options];
187
    });
188
  };
189

    
190
  OptionManager.prototype.mergeOptions = function mergeOptions(_ref2) {
191
    var _this = this;
192

    
193
    var rawOpts = _ref2.options,
194
        extendingOpts = _ref2.extending,
195
        alias = _ref2.alias,
196
        loc = _ref2.loc,
197
        dirname = _ref2.dirname;
198

    
199
    alias = alias || "foreign";
200
    if (!rawOpts) return;
201

    
202
    if ((typeof rawOpts === "undefined" ? "undefined" : (0, _typeof3.default)(rawOpts)) !== "object" || Array.isArray(rawOpts)) {
203
      this.log.error("Invalid options type for " + alias, TypeError);
204
    }
205

    
206
    var opts = (0, _cloneDeepWith2.default)(rawOpts, function (val) {
207
      if (val instanceof _plugin3.default) {
208
        return val;
209
      }
210
    });
211

    
212
    dirname = dirname || process.cwd();
213
    loc = loc || alias;
214

    
215
    for (var _key2 in opts) {
216
      var option = _config3.default[_key2];
217

    
218
      if (!option && this.log) {
219
        if (_removed2.default[_key2]) {
220
          this.log.error("Using removed Babel 5 option: " + alias + "." + _key2 + " - " + _removed2.default[_key2].message, ReferenceError);
221
        } else {
222
          var unknownOptErr = "Unknown option: " + alias + "." + _key2 + ". Check out http://babeljs.io/docs/usage/options/ for more information about options.";
223
          var presetConfigErr = "A common cause of this error is the presence of a configuration options object without the corresponding preset name. Example:\n\nInvalid:\n  `{ presets: [{option: value}] }`\nValid:\n  `{ presets: [['presetName', {option: value}]] }`\n\nFor more detailed information on preset configuration, please see https://babeljs.io/docs/en/plugins#pluginpresets-options.";
224

    
225

    
226
          this.log.error(unknownOptErr + "\n\n" + presetConfigErr, ReferenceError);
227
        }
228
      }
229
    }
230

    
231
    (0, _index.normaliseOptions)(opts);
232

    
233
    if (opts.plugins) {
234
      opts.plugins = OptionManager.normalisePlugins(loc, dirname, opts.plugins);
235
    }
236

    
237
    if (opts.presets) {
238
      if (opts.passPerPreset) {
239
        opts.presets = this.resolvePresets(opts.presets, dirname, function (preset, presetLoc) {
240
          _this.mergeOptions({
241
            options: preset,
242
            extending: preset,
243
            alias: presetLoc,
244
            loc: presetLoc,
245
            dirname: dirname
246
          });
247
        });
248
      } else {
249
        this.mergePresets(opts.presets, dirname);
250
        delete opts.presets;
251
      }
252
    }
253

    
254
    if (rawOpts === extendingOpts) {
255
      (0, _assign2.default)(extendingOpts, opts);
256
    } else {
257
      (0, _merge2.default)(extendingOpts || this.options, opts);
258
    }
259
  };
260

    
261
  OptionManager.prototype.mergePresets = function mergePresets(presets, dirname) {
262
    var _this2 = this;
263

    
264
    this.resolvePresets(presets, dirname, function (presetOpts, presetLoc) {
265
      _this2.mergeOptions({
266
        options: presetOpts,
267
        alias: presetLoc,
268
        loc: presetLoc,
269
        dirname: _path2.default.dirname(presetLoc || "")
270
      });
271
    });
272
  };
273

    
274
  OptionManager.prototype.resolvePresets = function resolvePresets(presets, dirname, onResolve) {
275
    return presets.map(function (val) {
276
      var options = void 0;
277
      if (Array.isArray(val)) {
278
        if (val.length > 2) {
279
          throw new Error("Unexpected extra options " + (0, _stringify2.default)(val.slice(2)) + " passed to preset.");
280
        }
281

    
282
        var _val = val;
283
        val = _val[0];
284
        options = _val[1];
285
      }
286

    
287
      var presetLoc = void 0;
288
      try {
289
        if (typeof val === "string") {
290
          presetLoc = (0, _resolvePreset2.default)(val, dirname);
291

    
292
          if (!presetLoc) {
293
            throw new Error("Couldn't find preset " + (0, _stringify2.default)(val) + " relative to directory " + (0, _stringify2.default)(dirname));
294
          }
295

    
296
          val = require(presetLoc);
297
        }
298

    
299
        if ((typeof val === "undefined" ? "undefined" : (0, _typeof3.default)(val)) === "object" && val.__esModule) {
300
          if (val.default) {
301
            val = val.default;
302
          } else {
303
            var _val2 = val,
304
                __esModule = _val2.__esModule,
305
                rest = (0, _objectWithoutProperties3.default)(_val2, ["__esModule"]);
306

    
307
            val = rest;
308
          }
309
        }
310

    
311
        if ((typeof val === "undefined" ? "undefined" : (0, _typeof3.default)(val)) === "object" && val.buildPreset) val = val.buildPreset;
312

    
313
        if (typeof val !== "function" && options !== undefined) {
314
          throw new Error("Options " + (0, _stringify2.default)(options) + " passed to " + (presetLoc || "a preset") + " which does not accept options.");
315
        }
316

    
317
        if (typeof val === "function") val = val(context, options, { dirname: dirname });
318

    
319
        if ((typeof val === "undefined" ? "undefined" : (0, _typeof3.default)(val)) !== "object") {
320
          throw new Error("Unsupported preset format: " + val + ".");
321
        }
322

    
323
        onResolve && onResolve(val, presetLoc);
324
      } catch (e) {
325
        if (presetLoc) {
326
          e.message += " (While processing preset: " + (0, _stringify2.default)(presetLoc) + ")";
327
        }
328
        throw e;
329
      }
330
      return val;
331
    });
332
  };
333

    
334
  OptionManager.prototype.normaliseOptions = function normaliseOptions() {
335
    var opts = this.options;
336

    
337
    for (var _key3 in _config3.default) {
338
      var option = _config3.default[_key3];
339
      var val = opts[_key3];
340

    
341
      if (!val && option.optional) continue;
342

    
343
      if (option.alias) {
344
        opts[option.alias] = opts[option.alias] || val;
345
      } else {
346
        opts[_key3] = val;
347
      }
348
    }
349
  };
350

    
351
  OptionManager.prototype.init = function init() {
352
    var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
353

    
354
    for (var _iterator2 = (0, _buildConfigChain2.default)(opts, this.log), _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : (0, _getIterator3.default)(_iterator2);;) {
355
      var _ref3;
356

    
357
      if (_isArray2) {
358
        if (_i2 >= _iterator2.length) break;
359
        _ref3 = _iterator2[_i2++];
360
      } else {
361
        _i2 = _iterator2.next();
362
        if (_i2.done) break;
363
        _ref3 = _i2.value;
364
      }
365

    
366
      var _config = _ref3;
367

    
368
      this.mergeOptions(_config);
369
    }
370

    
371
    this.normaliseOptions(opts);
372

    
373
    return this.options;
374
  };
375

    
376
  return OptionManager;
377
}();
378

    
379
exports.default = OptionManager;
380

    
381

    
382
OptionManager.memoisedPlugins = [];
383
module.exports = exports["default"];
(4-4/6)