UNPKG

3.02 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _babelGenerator = require('babel-generator');
8
9var _babelGenerator2 = _interopRequireDefault(_babelGenerator);
10
11var _babelTraverse = require('babel-traverse');
12
13var _babelTraverse2 = _interopRequireDefault(_babelTraverse);
14
15var _babelTemplate = require('babel-template');
16
17var _babelTemplate2 = _interopRequireDefault(_babelTemplate);
18
19var _babelTypes = require('babel-types');
20
21var _injectionTemplate = require('./injection-template');
22
23var _injectionTemplate2 = _interopRequireDefault(_injectionTemplate);
24
25function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
27function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
28
29/**
30 * Inject global variables into a react component,
31 * making them available via this[globalKey] (if possible, deprecated)
32 * and on node's global object
33 * @param {Object} ast generated by babel
34 * @param {Object} globals global variables to inject
35 * @return {Object} ast with injected globals
36 */
37exports.default = function (ast) {
38 let globals = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
39
40 ast.deprecations = [];
41
42 if (Object.keys(globals).length === 0) {
43 return ast;
44 }
45
46 // This is some seriously twisted legacy burden
47 // Until recently we injected globals into the scope of react components -
48 // to do this in interop with all react component cases we have to
49 // - find references to this.<globalKey>
50 // - replace it with global.<globalKey>
51 // - deprecate this stuff!
52 (0, _babelTraverse2.default)(ast, {
53 MemberExpression: function MemberExpression(path) {
54 const isGlobalLegacyReference = Object.keys(globals).some(key => path.matchesPattern(`this.${ key }`));
55 if (isGlobalLegacyReference) {
56 const previous = (0, _babelGenerator2.default)(path.node).code.split('.').slice(1);
57 const replaced = ['global'].concat(_toConsumableArray(previous)).join('.');
58
59 const pos = path.node.loc.start;
60 ast.deprecations.push({
61 key: previous[0],
62 type: 'globalContextAccess',
63 line: pos.line,
64 column: pos.column,
65 alternative: replaced
66 });
67 path.replaceWith((0, _babelTypes.identifier)(replaced));
68 }
69 }
70 });
71
72 if (ast.hasGlobals) {
73 return ast;
74 }
75
76 ast.hasGlobals = true;
77
78 // Inject access to global config
79 // as global.<key>
80 (0, _babelTraverse2.default)(ast, {
81 Program: {
82 exit: function exit(path) {
83 const IDENTIFIER = path.scope.generateUidIdentifier('injection');
84
85 const INJECTION = (0, _babelTemplate2.default)(`var ${ IDENTIFIER.name } = ${ JSON.stringify(globals) };`)();
86
87 const injection = (0, _injectionTemplate2.default)({
88 IDENTIFIER: IDENTIFIER,
89 INJECTION: INJECTION
90 });
91
92 path.unshiftContainer('body', injection);
93 }
94 }
95 });
96
97 return ast;
98};
99
100module.exports = exports['default'];
\No newline at end of file