core.js 10 KB

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