forked from CoderLine/alphaTab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.resolve.js
56 lines (50 loc) · 1.94 KB
/
rollup.resolve.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
const path = require('path');
const glob = require('glob').sync;
const fs = require('fs');
module.exports = function (options) {
const mappings = options.mappings;
const types = options.types;
return {
name: 'resolve-typescript-paths',
resolveId: function (importee, importer) {
if (typeof importer === 'undefined' || importee.startsWith('\0')) {
return null;
}
const extension = types ? '.d.ts' : '.js';
if (fs.existsSync(importee)) {
return importee;
} else if (importee.startsWith('**')) {
return importee;
} else {
const importerDir = path.dirname(importer);
let resolved = importee;
const match = Object.entries(mappings).filter(m => importee.startsWith(m[0]));
if (match && match.length > 0) {
if (match[0][1].endsWith(extension)) {
resolved = path.join(process.cwd(), match[0][1]);
} else {
resolved = path.join(process.cwd(), match[0][1], importee.substring(match[0][0].length));
}
} else {
resolved = path.join(importerDir, importee);
}
if (fs.existsSync(path.join(resolved, 'index' + extension))) {
resolved = path.join(resolved, 'index');
}
return resolved + extension;
}
},
load(id) {
if (id.startsWith('**')) {
const files = glob(id, {
cwd: process.cwd()
});
const source = files
.map((file, i) => `export * as _${i} from ${JSON.stringify(path.join(process.cwd(), file))};`)
.join('\r\n');
return source;
}
return null;
}
};
};