UNPKG

7.68 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7exports.default = function (defaultLibraryName) {
8 return function (_ref) {
9 var types = _ref.types;
10
11 var specified = void 0;
12 var libraryObjs = void 0;
13 var selectedMethods = void 0;
14 var moduleArr = void 0;
15
16 function camel2Dash(_str) {
17 var str = _str[0].toLowerCase() + _str.substr(1);
18 return str.replace(/([A-Z])/g, function ($1) {
19 return '-' + $1.toLowerCase();
20 });
21 }
22
23 function importMethod(methodName, file, opts) {
24 if (!selectedMethods[methodName]) {
25 var options = void 0;
26 var path = void 0;
27
28 if (Array.isArray(opts)) {
29 options = opts.find(function (option) {
30 return moduleArr[methodName] === option.libraryName || libraryObjs[methodName] === option.libraryName;
31 }); // eslint-disable-line
32 }
33 options = options || opts;
34
35 var _options = options;
36 var _options$libDir = _options.libDir;
37 var libDir = _options$libDir === undefined ? 'lib' : _options$libDir;
38 var _options$libraryName = _options.libraryName;
39 var libraryName = _options$libraryName === undefined ? defaultLibraryName : _options$libraryName;
40 var styleLibraryName = _options.styleLibraryName;
41 var style = _options.style;
42 var _options$root = _options.root;
43 var root = _options$root === undefined ? '' : _options$root;
44
45 var _root = root;
46
47 if (root) {
48 _root = '/' + root;
49 }
50
51 if (libraryObjs[methodName]) {
52 path = libraryName + '/' + libDir + _root;
53 } else {
54 path = libraryName + '/' + libDir + '/' + camel2Dash(methodName);
55 }
56
57 selectedMethods[methodName] = file.addImport(path, 'default');
58
59 if (styleLibraryName) {
60 if (!cachePath[libraryName]) {
61 var themeName = styleLibraryName.replace(/^~/, '');
62 cachePath[libraryName] = styleLibraryName.indexOf('~') === 0 ? themeName : libraryName + '/' + libDir + '/' + themeName;
63 }
64
65 if (libraryObjs[methodName]) {
66 /* istanbul ingore next */
67 if (cache[libraryName] === 2) {
68 throw Error('[babel-plugin-component] If you are using ' + 'on-demand loading and importing all, make sure to invoke the' + ' importing all first.');
69 }
70 path = '' + cachePath[libraryName] + (_root || '/index') + '.css';
71 cache[libraryName] = 1;
72 } else {
73 if (cache[libraryName] !== 1) {
74 path = cachePath[libraryName] + '/' + camel2Dash(methodName) + '.css';
75 file.addImport(cachePath[libraryName] + '/base.css', 'default');
76 cache[libraryName] = 2;
77 }
78 }
79
80 file.addImport(path, 'default');
81 } else {
82 if (style === true) {
83 file.addImport(path + '/style.css', 'default');
84 } else if (style) {
85 file.addImport(path + '/' + style, 'default');
86 }
87 }
88 }
89 return selectedMethods[methodName];
90 }
91
92 function buildExpressionHandler(node, props, path, opts) {
93 var file = path.hub.file;
94
95 props.forEach(function (prop) {
96 if (!types.isIdentifier(node[prop])) return;
97 if (specified[node[prop].name]) {
98 node[prop] = importMethod(node[prop].name, file, opts); // eslint-disable-line
99 }
100 });
101 }
102
103 function buildDeclaratorHandler(node, prop, path, opts) {
104 var file = path.hub.file;
105
106 if (!types.isIdentifier(node[prop])) return;
107 if (specified[node[prop].name]) {
108 node[prop] = importMethod(node[prop].name, file, opts); // eslint-disable-line
109 }
110 }
111
112 return {
113 visitor: {
114 Program: function Program() {
115 specified = Object.create(null);
116 libraryObjs = Object.create(null);
117 selectedMethods = Object.create(null);
118 moduleArr = Object.create(null);
119 },
120 ImportDeclaration: function ImportDeclaration(path, _ref2) {
121 var opts = _ref2.opts;
122 var node = path.node;
123 var value = node.source.value;
124
125 var result = {};
126
127 if (Array.isArray(opts)) {
128 result = opts.find(function (option) {
129 return option.libraryName === value;
130 }) || {};
131 }
132 var libraryName = result.libraryName || opts.libraryName || defaultLibraryName;
133
134 if (value === libraryName) {
135 node.specifiers.forEach(function (spec) {
136 if (types.isImportSpecifier(spec)) {
137 specified[spec.local.name] = spec.imported.name;
138 moduleArr[spec.imported.name] = value;
139 } else {
140 libraryObjs[spec.local.name] = value;
141 }
142 });
143
144 path.remove();
145 }
146 },
147 CallExpression: function CallExpression(path, _ref3) {
148 var opts = _ref3.opts;
149 var node = path.node;
150 var file = path.hub.file;
151 var name = node.callee.name;
152
153
154 if (types.isIdentifier(node.callee)) {
155 if (specified[name]) {
156 node.callee = importMethod(specified[name], file, opts);
157 }
158 } else {
159 node.arguments = node.arguments.map(function (arg) {
160 var argName = arg.name;
161
162 if (specified[argName]) {
163 return importMethod(specified[argName], file, opts);
164 } else if (libraryObjs[argName]) {
165 return importMethod(argName, file, opts);
166 }
167 return arg;
168 });
169 }
170 },
171 MemberExpression: function MemberExpression(path, _ref4) {
172 var opts = _ref4.opts;
173 var node = path.node;
174 var file = path.hub.file;
175
176
177 if (libraryObjs[node.object.name] || specified[node.object.name]) {
178 node.object = importMethod(node.object.name, file, opts);
179 }
180 },
181 ArrayExpression: function ArrayExpression(path, _ref5) {
182 var opts = _ref5.opts;
183 var elements = path.node.elements;
184 var file = path.hub.file;
185
186
187 elements.forEach(function (item, key) {
188 if (libraryObjs[item.name] || specified[item.name]) {
189 elements[key] = importMethod(item.name, file, opts);
190 }
191 });
192 },
193 Property: function Property(path, _ref6) {
194 var opts = _ref6.opts;
195 var node = path.node;
196
197 buildDeclaratorHandler(node, 'value', path, opts);
198 },
199 VariableDeclarator: function VariableDeclarator(path, _ref7) {
200 var opts = _ref7.opts;
201 var node = path.node;
202
203 buildDeclaratorHandler(node, 'init', path, opts);
204 },
205 LogicalExpression: function LogicalExpression(path, _ref8) {
206 var opts = _ref8.opts;
207 var node = path.node;
208
209 buildExpressionHandler(node, ['left', 'right'], path, opts);
210 },
211 ConditionalExpression: function ConditionalExpression(path, _ref9) {
212 var opts = _ref9.opts;
213 var node = path.node;
214
215 buildExpressionHandler(node, ['test', 'consequent', 'alternate'], path, opts);
216 },
217 IfStatement: function IfStatement(path, _ref10) {
218 var opts = _ref10.opts;
219 var node = path.node;
220
221 buildExpressionHandler(node, ['test'], path, opts);
222 buildExpressionHandler(node.test, ['left', 'right'], path, opts);
223 }
224 }
225 };
226 };
227};
228
229var cache = {};
230var cachePath = {};
231
232module.exports = exports['default'];
\No newline at end of file