UNPKG

8.47 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _path = require('path');
10
11function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
13function camel2Dash(_str) {
14 var str = _str[0].toLowerCase() + _str.substr(1);
15 return str.replace(/([A-Z])/g, function ($1) {
16 return '-' + $1.toLowerCase();
17 });
18}
19
20function camel2Underline(_str) {
21 var str = _str[0].toLowerCase() + _str.substr(1);
22 return str.replace(/([A-Z])/g, function ($1) {
23 return '_' + $1.toLowerCase();
24 });
25}
26
27function winPath(path) {
28 return path.replace(/\\/g, '/');
29}
30
31var Plugin = function () {
32 function Plugin(libraryName, libraryDirectory, style, camel2DashComponentName, camel2UnderlineComponentName, fileName, customName, types) {
33 _classCallCheck(this, Plugin);
34
35 this.specified = null;
36 this.libraryObjs = null;
37 this.selectedMethods = null;
38 this.libraryName = libraryName;
39 this.libraryDirectory = typeof libraryDirectory === 'undefined' ? 'lib' : libraryDirectory;
40 this.camel2DashComponentName = typeof camel2DashComponentName === 'undefined' ? true : camel2DashComponentName;
41 this.camel2UnderlineComponentName = camel2UnderlineComponentName;
42 this.style = style || false;
43 this.fileName = fileName || '';
44 this.customName = customName;
45 this.types = types;
46 }
47
48 _createClass(Plugin, [{
49 key: 'importMethod',
50 value: function importMethod(methodName, file) {
51 if (!this.selectedMethods[methodName]) {
52 var libraryDirectory = this.libraryDirectory;
53 var style = this.style;
54 var transformedMethodName = this.camel2UnderlineComponentName // eslint-disable-line
55 ? camel2Underline(methodName) : this.camel2DashComponentName ? camel2Dash(methodName) : methodName;
56 var path = winPath(this.customName ? this.customName(transformedMethodName) : (0, _path.join)(this.libraryName, libraryDirectory, transformedMethodName, this.fileName) // eslint-disable-line
57 );
58 this.selectedMethods[methodName] = file.addImport(path, 'default');
59 if (style === true) {
60 file.addImport(path + '/style', 'style');
61 } else if (style === 'css') {
62 file.addImport(path + '/style/css', 'style');
63 }
64 }
65 return this.selectedMethods[methodName];
66 }
67 }, {
68 key: 'buildExpressionHandler',
69 value: function buildExpressionHandler(node, props, path) {
70 var _this = this;
71
72 var file = path.hub.file;
73
74 var types = this.types;
75 props.forEach(function (prop) {
76 if (!types.isIdentifier(node[prop])) return;
77 if (_this.specified[node[prop].name]) {
78 node[prop] = _this.importMethod(_this.specified[node[prop].name], file); // eslint-disable-line
79 }
80 });
81 }
82 }, {
83 key: 'buildDeclaratorHandler',
84 value: function buildDeclaratorHandler(node, prop, path) {
85 var file = path.hub.file;
86
87 var types = this.types;
88 if (!types.isIdentifier(node[prop])) return;
89 if (this.specified[node[prop].name] && path.scope.hasBinding(node[prop].name) && path.scope.getBinding(node[prop].name).path.type === 'ImportSpecifier') {
90 node[prop] = this.importMethod(node[prop].name, file); // eslint-disable-line
91 }
92 }
93 }, {
94 key: 'Program',
95 value: function Program() {
96 this.specified = Object.create(null);
97 this.libraryObjs = Object.create(null);
98 this.selectedMethods = Object.create(null);
99 }
100 }, {
101 key: 'ImportDeclaration',
102 value: function ImportDeclaration(path) {
103 var _this2 = this;
104
105 var node = path.node;
106
107 // path maybe removed by prev instances.
108
109 if (!node) return;
110
111 var value = node.source.value;
112
113 var libraryName = this.libraryName;
114 var types = this.types;
115 if (value === libraryName) {
116 node.specifiers.forEach(function (spec) {
117 if (types.isImportSpecifier(spec)) {
118 _this2.specified[spec.local.name] = spec.imported.name;
119 } else {
120 _this2.libraryObjs[spec.local.name] = true;
121 }
122 });
123 path.remove();
124 }
125 }
126 }, {
127 key: 'CallExpression',
128 value: function CallExpression(path) {
129 var _this3 = this;
130
131 var node = path.node;
132 var file = path.hub.file;
133 var name = node.callee.name;
134
135 var types = this.types;
136
137 if (types.isIdentifier(node.callee)) {
138 if (this.specified[name]) {
139 node.callee = this.importMethod(this.specified[name], file);
140 }
141 }
142
143 node.arguments = node.arguments.map(function (arg) {
144 var argName = arg.name;
145
146 if (_this3.specified[argName] && path.scope.hasBinding(argName) && path.scope.getBinding(argName).path.type === 'ImportSpecifier') {
147 return _this3.importMethod(_this3.specified[argName], file);
148 }
149 return arg;
150 });
151 }
152 }, {
153 key: 'MemberExpression',
154 value: function MemberExpression(path) {
155 var node = path.node;
156 var file = path.hub.file;
157
158 // multiple instance check.
159
160 if (!node.object || !node.object.name) return;
161
162 if (this.libraryObjs[node.object.name]) {
163 // antd.Button -> _Button
164 path.replaceWith(this.importMethod(node.property.name, file));
165 } else if (this.specified[node.object.name]) {
166 node.object = this.importMethod(this.specified[node.object.name], file);
167 }
168 }
169 }, {
170 key: 'Property',
171 value: function Property(path, _ref) {
172 var opts = _ref.opts;
173 var node = path.node;
174
175 this.buildDeclaratorHandler(node, 'value', path, opts);
176 }
177 }, {
178 key: 'VariableDeclarator',
179 value: function VariableDeclarator(path, _ref2) {
180 var opts = _ref2.opts;
181 var node = path.node;
182
183 this.buildDeclaratorHandler(node, 'init', path, opts);
184 }
185 }, {
186 key: 'LogicalExpression',
187 value: function LogicalExpression(path, _ref3) {
188 var opts = _ref3.opts;
189 var node = path.node;
190
191 this.buildExpressionHandler(node, ['left', 'right'], path, opts);
192 }
193 }, {
194 key: 'ConditionalExpression',
195 value: function ConditionalExpression(path, _ref4) {
196 var opts = _ref4.opts;
197 var node = path.node;
198
199 this.buildExpressionHandler(node, ['test', 'consequent', 'alternate'], path, opts);
200 }
201 }, {
202 key: 'IfStatement',
203 value: function IfStatement(path, _ref5) {
204 var opts = _ref5.opts;
205 var node = path.node;
206
207 this.buildExpressionHandler(node, ['test'], path, opts);
208 this.buildExpressionHandler(node.test, ['left', 'right'], path, opts);
209 }
210 }, {
211 key: 'ExpressionStatement',
212 value: function ExpressionStatement(path, _ref6) {
213 var opts = _ref6.opts;
214 var node = path.node;
215 var types = this.types;
216
217 if (types.isAssignmentExpression(node.expression)) {
218 this.buildExpressionHandler(node.expression, ['right'], path, opts);
219 }
220 }
221 }, {
222 key: 'ReturnStatement',
223 value: function ReturnStatement(path) {
224 var types = this.types;
225 var node = path.node,
226 file = path.hub.file;
227
228 if (node.argument && types.isIdentifier(node.argument) && this.specified[node.argument.name]) {
229 node.argument = this.importMethod(node.argument.name, file);
230 }
231 }
232 }, {
233 key: 'ExportDefaultDeclaration',
234 value: function ExportDefaultDeclaration(path, _ref7) {
235 var opts = _ref7.opts;
236 var node = path.node;
237
238 this.buildExpressionHandler(node, ['declaration'], path, opts);
239 }
240 }, {
241 key: 'BinaryExpression',
242 value: function BinaryExpression(path, _ref8) {
243 var opts = _ref8.opts;
244 var node = path.node;
245
246 this.buildExpressionHandler(node, ['left', 'right'], path, opts);
247 }
248 }]);
249
250 return Plugin;
251}();
252
253exports.default = Plugin;
254module.exports = exports['default'];
\No newline at end of file