-
Notifications
You must be signed in to change notification settings - Fork 202
/
rollup.plugin.resolve.ts
70 lines (61 loc) · 2.23 KB
/
rollup.plugin.resolve.ts
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
import path from 'path';
import glob from 'glob';
import fs from 'fs';
import { Plugin } from 'rollup';
export interface ResolvePluginOptions {
mappings: {
[key: string]: string
},
types?: boolean
}
export default function resolve(options: ResolvePluginOptions) {
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;
let 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');
}
resolved += extension;
if (fs.existsSync(resolved)) {
return resolved;
}
return null;
}
},
load(id) {
if (id.startsWith('**')) {
const files = glob.sync(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;
}
} as Plugin;
};