forked from CoderLine/alphaTab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.resolve.js
53 lines (48 loc) · 1.76 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
const join = require('path').join;
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 (importee.startsWith('**')) {
return importee;
} else {
const match = Object.entries(mappings).filter(m => importee.startsWith(m[0]));
if (!match || match.length === 0) {
return null;
}
if (match[0][1].endsWith(extension)) {
return join(process.cwd(), match[0][1]);
}
let resolved = join(process.cwd(), match[0][1], importee.substring(match[0][0].length));
if (fs.existsSync(join(resolved, 'index' + extension))) {
return join(resolved, 'index' + extension);
}
return resolved + extension;
}
},
load(id) {
if (id.startsWith('**')) {
const files = glob(id, {
cwd: process.cwd()
});
const source = files
.map(
(file, i) =>
`import _${i} from ${JSON.stringify(join(process.cwd(), file))};
export { _${i} };`
)
.join('\r\n');
return source;
}
return null;
}
};
};