UNPKG

3.76 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7exports.default = function (_ref) {
8 var types = _ref.types;
9
10 var Programer = {
11 visitor: {
12 ImportDeclaration: function ImportDeclaration(path, _ref2) {
13 var opts = _ref2.opts;
14 var node = path.node,
15 source = node.source,
16 specifiers = node.specifiers;
17
18
19 var currentConfigInstance = opts; //plugin 配置
20
21 if (Array.isArray(opts)) {
22 currentConfigInstance = opts.find(function (option) {
23 return option.libraryName === source.value;
24 }) || {};
25 }
26
27 (0, _assert2.default)(currentConfigInstance.libraryName, '\n\n libraryName should be provided! \n\n');
28
29 //plugin 配置的依赖库libraryName名称
30 var libraryName = currentConfigInstance.libraryName || opts.libraryName || 'k-view';
31
32 //下划线
33 currentConfigInstance.camel2UnderlineComponentName = typeof currentConfigInstance.camel2UnderlineComponentName === 'undefined' ? false : currentConfigInstance.camel2UnderlineComponentName;
34 //横杆连接
35 currentConfigInstance.camel2DashComponentName = typeof currentConfigInstance.camel2DashComponentName === 'undefined' ? false : currentConfigInstance.camel2DashComponentName;
36
37 // 确认导入库 是否是 .babelrc library属性指定库 以及 如果不是默认导入 才进行按需导入加载
38 if (libraryName === source.value && !types.isImportDefaultSpecifier(specifiers[0]) && !types.isImportNamespaceSpecifier(specifiers[0])) {
39
40 var newImports = specifiers.map(function (specifier) {
41 // 遍历 出导入的每个包的说明描述符
42 //转换后的文件名
43 var transformedSourceName = currentConfigInstance.camel2UnderlineComponentName ? winPath(transCamel(specifier.imported.name, '_')) : currentConfigInstance.camel2DashComponentName ? winPath(transCamel(specifier.imported.name, '-')) : specifier.imported.name;
44 var libraryDirectory = typeof currentConfigInstance.libraryDirectory === 'undefined' ? 'lib' : currentConfigInstance.libraryDirectory;
45
46 //当前组件路径
47 var compDirPath = winPath((0, _path.join)(libraryName, libraryDirectory, transformedSourceName));
48
49 var compInstancePath = currentConfigInstance.customName ? currentConfigInstance.customName('' + transformedSourceName) : compDirPath + '/index.js';
50 var compInstanceStylePath = currentConfigInstance.customStyleName ? currentConfigInstance.customStyleName('' + transformedSourceName) : compDirPath + '/style.css';
51
52 return [types.importDeclaration([types.importDefaultSpecifier(specifier.local)], types.stringLiteral(compInstancePath)), types.callExpression(types.import(), [types.stringLiteral(compInstanceStylePath)])];
53 });
54 newImports = flatArray(newImports, 1);
55 path.replaceWithMultiple(newImports);
56 }
57 }
58 }
59 };
60 return Programer;
61};
62
63var _assert = require('assert');
64
65var _assert2 = _interopRequireDefault(_assert);
66
67var _path = require('path');
68
69function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
70
71function transCamel(_str, symbol) {
72 var str = _str[0].toLowerCase() + _str.substr(1);
73 return str.replace(/([A-Z])/g, function ($1) {
74 return '' + symbol + $1.toLowerCase();
75 });
76}
77
78function winPath(path) {
79 return path.replace(/\\/g, '/');
80}
81
82function flatArray(arrParam, depArg) {
83 var res = [],
84 depth = depArg || 1,
85 depNum = 0,
86 flatMap = function flatMap(arr) {
87 arr.map(function (item, index, array) {
88 if (Array.isArray(item)) {
89 if (depNum < depth) {
90 depNum++;
91 flatMap(item);
92 } else {
93 res.push(item);
94 }
95 } else {
96 res.push(item);
97 if (array.length === index + 1) depNum = 0;
98 }
99 });
100 };
101 flatMap(arrParam);
102 return res;
103}
\No newline at end of file