-
Notifications
You must be signed in to change notification settings - Fork 18
/
lookup.js
310 lines (247 loc) · 7.54 KB
/
lookup.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
var path = require('path');
var inited = false;
var root, baseUrl, pkgs, paths, opts;
var moitJsExtension;
function getJsExtensionMoited() {
if (typeof moitJsExtension === 'undefined') {
var testFile = fis.file(fis.project.getProjectPath() + '/test.js');
// console.log(testFile);
moitJsExtension = testFile.id !== testFile.moduleId;
}
return moitJsExtension;
}
// find package base on folder name
// 根据目录位置查找是否属于某个 package 配置
function findPkgByFolder(folder, list) {
var ret = null;
if (list && list.length) {
list.every(function(item) {
if (item.folder === folder || path.resolve(baseUrl, item.folder) === folder) {
ret = item;
return false;
}
return true;
});
}
return ret;
}
// 判读是否为 fis id 格式的路径
function isFISID(filepath) {
var nsConnector = fis.media().get('namespaceConnector', ':');
return !!~filepath.indexOf(nsConnector);
}
// 当 require 没有指定后缀时,用来根据后缀查找模块定义。
function findResource(name, filepath, extList) {
var candidates = [name, path.join(name, 'index')];
var baseName = path.basename(name);
if (baseName && baseName !== '.' && baseName !== '..') {
candidates.push(path.join(name, baseName));
}
var info = null;
candidates.every(function(candidate) {
info = fis.uri(candidate, filepath);
for (var i = 0, len = extList.length; i < len && !info.file; i++) {
info = fis.uri(candidate + extList[i], filepath);
}
if (info && info.file) {
return false;
}
return true;
});
return info;
}
// -------------------
// 各种查找
// -------------------
// 无后缀查找
function tryNoExtLookUp(info, file, opts) {
// 支持没有指定后缀的 require 查找。
return findResource(info.rest, file ? file.dirname : fis.project.getProjectPath(), opts.extList);
}
// fis id 查找
function tryFisIdLookUp(info, file, opts) {
// 如果是 fis id 路径且属于当前 namespace,也同样做一次查找,从根目录查起
var nsConnector = fis.media().get('namespaceConnector', ':');
var idx = info.rest.indexOf(nsConnector);
if (~idx) {
info.isFisId = true;
var ns = info.rest.substring(0, idx);
var subpath = info.rest.substring(idx + 1);
if (ns === fis.media().get('namespace')) {
return findResource(subpath, root, opts.extList);
}
}
}
// 基于 BaseUrl 查找
function tryBaseUrlLookUp(info, file, opts) {
if (root !== baseUrl && info.rest) {
var ret = findResource(info.rest, baseUrl, opts.extList);
if (ret && ret.file && info.rest[0] === '.') {
fis.log.warn('Should use `%s` instead of `%s` in [`%s`] since `BaseUrl` should not be used in relative path resolving.', ret.file.subpath, info.rest, file.subpath);
}
return ret;
}
}
// 基于 Root 查找
function tryRootLookUp(info, file, opts) {
if (info.rest && info.rest[0] === '.') {
return;
}
return findResource(info.rest, root, opts.extList);
}
// 在 Paths 中查找
function tryPathsLookUp(info, file, opts) {
var id = info.rest;
var test;
if (/^([^\/]+)(?:\/(.*))?$/.test(id)) {
var parts = id.split('/');
var idx = parts.length;
while (idx > 0) {
var prefix = parts.slice(0, idx).join('/');
var subpath = parts.slice(idx).join('/');
var dirs;
if ((dirs = paths[prefix])) {
for (var i = 0, len = dirs.length;
(!test || !test.file) && i < len; i++) {
test = subpath ? findResource(subpath, path.join(baseUrl, dirs[i]), opts.extList) : findResource(dirs[i], baseUrl, opts.extList);
}
}
idx--;
}
}
return test;
}
function tryFolderLookUp(info, file, opts) {
var id = info.rest;
if (id === '.' || opts.packages) {
// 真麻烦,还得去查找当前目录是不是 match 一个 packages。
// 如果是,得找到 main 的设置。
var folderName = id[0] === '/' ? path.join(baseUrl, id) : path.join(file ? file.dirname : baseUrl, id);
var pkg = findPkgByFolder(folderName, opts.packages);
if (pkg) {
return findResource(pkg.main || 'main', pkg.folder, opts.extList);
}
}
}
// 在 Pacakges 里面查找
function tryPackagesLookUp(info, file, opts) {
var id = info.rest;
if (/^([^\/]+)(?:\/(.*))?$/.test(id)) {
var prefix = RegExp.$1;
var subpath = RegExp.$2;
// 如果是 scoped package
if (prefix && prefix[0] === '@') {
var arr = subpath.split('/');
prefix += '/' + arr.shift();
subpath = arr.join('/');
}
var pkg = pkgs[prefix];
if (pkg) {
if (pkg.isFISID) {
info.isFISID = true;
info.id = path.join(pkg.folder, subpath || pkg.main || 'main').replace(/\\/g, '/');
if (!/\.[^\.]+$/.test(info.id)) {
info.id += '.js';
}
info.moduleId = info.id.replace(/\.[^\.]+$/, '');
} else {
return findResource(subpath || pkg.main || 'main', pkg.folder, opts.extList);
}
}
}
}
var lookup = module.exports = function(info, file, silent) {
if (!inited) {
throw new Error('Please make sure init is called before this.');
}
var originPath = info.rest;
var lookupList = lookup.lookupList || [];
lookupList.every(function(finder) {
if (info.file) {
return false;
}
var ret = finder(info, file, opts);
if (ret && ret.file) {
info.id = ret.file.getId();
info.file = ret.file;
return false;
} else if (ret === false) {
return false;
}
return true;
});
// if (!silent && (!info.file || !info.moduleId)) {
// fis.log.warn('Can\'t find resource %s', info.rest.red);
// }
//
// 跨模块引用 js
if (info.isFISID && !info.file && (/\.js$/.test(info.rest) || !/\.\w+$/.test(info.rest))) {
info.id = info.id || info.rest;
var mod = getJsExtensionMoited();
// 只有在省略后缀的模式下才启用。
if (mod && !/^(https?\:)?\/\//.test(info.id)) {
info.moduleId = info.id.replace(/\.js$/, '');
info.id = info.moduleId + '.js';
}
}
return info;
}
lookup.lookupList = [
tryFisIdLookUp,
tryPathsLookUp,
tryPackagesLookUp,
tryFolderLookUp,
tryNoExtLookUp,
tryBaseUrlLookUp,
tryRootLookUp
];
lookup.tryFisIdLookUp = tryFisIdLookUp;
lookup.tryPathsLookUp = tryPathsLookUp;
lookup.tryPackagesLookUp = tryPackagesLookUp;
lookup.tryFolderLookUp = tryFolderLookUp;
lookup.tryNoExtLookUp = tryNoExtLookUp;
lookup.tryBaseUrlLookUp = tryBaseUrlLookUp;
lookup.tryRootLookUp = tryRootLookUp;
lookup.findResource = findResource;
/**
* 初始化 lookup
*/
lookup.init = function(fis, conf) {
inited = true;
opts = conf;
root = fis.project.getProjectPath();
baseUrl = path.join(root, opts.baseUrl || '.');
pkgs = {};
paths = {};
// normalize packages.
// 规整,方便后续查找
function normalizePackage(parent) {
parent.packages = parent.packages && parent.packages.map(function (item) {
if (typeof item === 'string') {
item = {
name: item
};
}
var folder = item.location || item.name;
item.isFISID = isFISID(folder);
item.folder = item.isFISID ? folder : path.join(baseUrl, item.location || item.name);
pkgs[item.name] = item;
if (item.packages) {
normalizePackage(item)
}
return item;
});
}
normalizePackage(opts)
// normalize paths.
// 规整,方便后续查找
opts.paths && (function(obj) {
Object.keys(obj).forEach(function(key) {
var val = obj[key];
if (!Array.isArray(val)) {
val = [val];
}
paths[key] = val;
});
})(opts.paths);
}