This repository has been archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.js
113 lines (97 loc) · 3.14 KB
/
index.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
import fs from 'fs';
import { platform } from 'os';
import path, { posix } from 'path';
import slash from 'slash';
const VOLUME = /^([A-Z]:)/i;
const IS_WINDOWS = platform() === 'win32';
// Helper functions
const noop = () => null;
const matches = (pattern, importee) => {
if (pattern instanceof RegExp) {
return pattern.test(importee);
}
if (importee.length < pattern.length) {
return false;
}
if (importee === pattern) {
return true;
}
const importeeStartsWithKey = (importee.indexOf(pattern) === 0);
const importeeHasSlashAfterKey = (importee.substring(pattern.length)[0] === '/');
return importeeStartsWithKey && importeeHasSlashAfterKey;
};
const endsWith = (needle, haystack) => haystack.slice(-needle.length) === needle;
const isFilePath = id => /^\.?\//.test(id);
const exists = (uri) => {
try {
return fs.statSync(uri).isFile();
} catch (e) {
return false;
}
};
const normalizeId = (id) => {
if ((IS_WINDOWS && typeof id === 'string') || VOLUME.test(id)) {
return slash(id.replace(VOLUME, ''));
}
return id;
};
const getEntries = ({ entries }) => {
if (!entries) {
return [];
}
if (Array.isArray(entries)) {
return entries;
}
return Object.keys(entries).map(key => ({ find: key, replacement: entries[key] }));
};
export default function alias(options = {}) {
const resolve = Array.isArray(options.resolve) ? options.resolve : ['.js'];
const entries = getEntries(options);
// No aliases?
if (entries.length === 0) {
return {
resolveId: noop,
};
}
return {
resolveId(importee, importer) {
const importeeId = normalizeId(importee);
const importerId = normalizeId(importer);
// First match is supposed to be the correct one
const matchedEntry = entries.find(entry => matches(entry.find, importeeId));
if (!matchedEntry || !importerId) {
return null;
}
let updatedId = normalizeId(importeeId.replace(matchedEntry.find, matchedEntry.replacement));
if (isFilePath(updatedId)) {
const directory = posix.dirname(importerId);
// Resolve file names
const filePath = posix.resolve(directory, updatedId);
const match = resolve.map(ext => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`))
.find(exists);
if (match) {
updatedId = match;
// To keep the previous behaviour we simply return the file path
// with extension
} else if (endsWith('.js', filePath)) {
updatedId = filePath;
} else {
const indexFilePath = posix.resolve(directory, `${updatedId}/index`);
const defaultMatch = resolve.map(ext => `${indexFilePath}${ext}`).find(exists);
if (defaultMatch) {
updatedId = defaultMatch;
} else {
updatedId = filePath + '.js';
}
}
}
// if alias is windows absoulate path return resolved path or
// rollup on windows will throw:
// [TypeError: Cannot read property 'specifier' of undefined]
if (VOLUME.test(matchedEntry.replacement)) {
return path.resolve(updatedId);
}
return updatedId;
},
};
}