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