getFilePath.js
4.8 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getFilePath = getFilePath;
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 _fs() {
const data = require("fs");
_fs = function _fs() {
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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
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; }
// similar with resolve.extensions in webpack config
const EXT_NAMES = ['.mjs', '.js', '.jsx', '.ts', '.tsx'];
function getPathWithExt(path) {
if ((0, _fs().existsSync)(path) && (0, _fs().statSync)(path).isFile()) {
return path;
}
var _iterator = _createForOfIteratorHelper(EXT_NAMES),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
const extName = _step.value;
const newPath = `${path}${extName}`;
if ((0, _fs().existsSync)(newPath) && (0, _fs().statSync)(newPath).isFile()) {
return newPath;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return null;
}
function getPathWithPkgJSON(path) {
// TODO: 这里是否会有 symlink 问题?
if ((0, _fs().statSync)(path).isDirectory()) {
const pkgPath = (0, _path().join)(path, 'package.json');
if ((0, _fs().existsSync)(pkgPath)) {
const pkg = JSON.parse((0, _fs().readFileSync)(pkgPath, 'utf-8')); // ref: https://webpack.js.org/configuration/resolve/#resolvemainfields
// TODO: support browser object
// ref: https://unpkg.alibaba-inc.com/browse/react-dom@17.0.2/package.json
const entry =
/*pkg.browser ||*/
pkg.module || pkg.main || 'index.js';
const target = (0, _path().join)(path, entry);
return getPathWithExt(target) || getPathWithIndexFile(target);
}
}
return null;
}
function getPathWithIndexFile(path) {
if ((0, _fs().existsSync)(path) && (0, _fs().statSync)(path).isDirectory()) {
var _iterator2 = _createForOfIteratorHelper(EXT_NAMES),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
const extName = _step2.value;
const indexFilePath = (0, _path().join)(path, `index${extName}`);
if ((0, _fs().existsSync)(indexFilePath) && (0, _fs().statSync)(indexFilePath).isFile()) {
return indexFilePath;
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
}
return null;
}
function getFilePath(path) {
// 1. 文件存在
// 2. 加后缀
const pathWithExt = getPathWithExt(path);
if (pathWithExt) {
return (0, _utils().winPath)(pathWithExt);
} // 3. path + package.json
const pathWithPkgJSON = getPathWithPkgJSON(path);
if (pathWithPkgJSON) {
return (0, _utils().winPath)(pathWithPkgJSON);
} // 4. path + index.js
const pathWithIndexFile = getPathWithIndexFile(path);
if (pathWithIndexFile) {
return (0, _utils().winPath)(pathWithIndexFile);
}
return null;
}