index.js
4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.path2Component = void 0;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _utils() {
const data = require("@umijs/utils");
_utils = function _utils() {
return data;
};
return data;
}
function _path() {
const data = require("path");
_path = function _path() {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]); if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/**
* convert path into componentName
* like:
* - src/index.tsx => SrcIndex
* - components/Header.tsx => ComponentHeader
*
* @param filePath
* @returns {string} componentName
*/
const path2Component = filePath => {
const _parse = (0, _path().parse)(filePath),
ext = _parse.ext;
const filePathWithoutExt = filePath // remove extension
.replace(ext, '').split(_path().sep) // upperFirst
.map(item => _utils().lodash.upperFirst(item.replace(/\W/g, ''))).join('');
return filePathWithoutExt;
}; // @ts-ignore
exports.path2Component = path2Component;
var _default = babel => {
const t = babel.types;
return {
visitor: {
// @ts-ignore
ExportDefaultDeclaration: {
enter(path, state) {
const def = path.node.declaration;
const _state$file$opts = state.file.opts,
cwd = _state$file$opts.cwd,
filename = _state$file$opts.filename;
const relativePath = (0, _path().relative)(cwd, filename);
if (/^\.(tsx|jsx)$/.test((0, _path().extname)(relativePath)) && // hidden relativePath
!/(^|\/)\.[^\/\.]/g.test(relativePath) && !relativePath.includes('node_modules')) {
let componentName = path2Component(relativePath);
if (!componentName) {
return;
} // solve identifier conflict
const identifiers = Object.keys(path.scope.bindings || {}); // add index if conflict
let idx = 0; // loop util componentName conflict
while (identifiers.includes(componentName)) {
componentName = `${componentName}${idx}`;
idx += 1;
} // generate component name identifier
const named = t.identifier(componentName);
if (t.isArrowFunctionExpression(def)) {
const varDec = t.variableDeclaration('const', [t.variableDeclarator(named, def)]);
const _path$insertBefore = path.insertBefore(varDec),
_path$insertBefore2 = _slicedToArray(_path$insertBefore, 1),
varDeclPath = _path$insertBefore2[0];
path.scope.registerDeclaration(varDeclPath);
path.replaceWith(t.exportDefaultDeclaration(named));
} else if (t.isFunctionDeclaration(def) && !def.id) {
def.id = named;
}
}
}
}
}
};
};
exports.default = _default;