UNPKG

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