1 | import fs from 'fs';
|
2 | import { platform } from 'os';
|
3 | import path, { posix } from 'path';
|
4 | import slash from 'slash';
|
5 |
|
6 | const VOLUME = /^([A-Z]:)/i;
|
7 | const IS_WINDOWS = platform() === 'win32';
|
8 |
|
9 |
|
10 | const noop = () => null;
|
11 | const matches = (pattern, importee) => {
|
12 | if (pattern instanceof RegExp) {
|
13 | return pattern.test(importee);
|
14 | }
|
15 | if (importee.length < pattern.length) {
|
16 | return false;
|
17 | }
|
18 | if (importee === pattern) {
|
19 | return true;
|
20 | }
|
21 | const importeeStartsWithKey = importee.indexOf(pattern) === 0;
|
22 | const importeeHasSlashAfterKey = importee.substring(pattern.length)[0] === '/';
|
23 | return importeeStartsWithKey && importeeHasSlashAfterKey;
|
24 | };
|
25 | const endsWith = (needle, haystack) => haystack.slice(-needle.length) === needle;
|
26 | const isFilePath = (id) => /^\.?\//.test(id);
|
27 | const exists = (uri) => {
|
28 | try {
|
29 | return fs.statSync(uri).isFile();
|
30 | } catch (e) {
|
31 | return false;
|
32 | }
|
33 | };
|
34 |
|
35 | const normalizeId = (id) => {
|
36 | if ((IS_WINDOWS && typeof id === 'string') || VOLUME.test(id)) {
|
37 | return slash(id.replace(VOLUME, ''));
|
38 | }
|
39 | return id;
|
40 | };
|
41 |
|
42 | const getEntries = ({ entries }) => {
|
43 | if (!entries) {
|
44 | return [];
|
45 | }
|
46 |
|
47 | if (Array.isArray(entries)) {
|
48 | return entries;
|
49 | }
|
50 |
|
51 | return Object.keys(entries).map((key) => {
|
52 | return { find: key, replacement: entries[key] };
|
53 | });
|
54 | };
|
55 |
|
56 | function alias(options = {}) {
|
57 | const resolve = Array.isArray(options.resolve) ? options.resolve : ['.js'];
|
58 | const entries = getEntries(options);
|
59 |
|
60 |
|
61 | if (entries.length === 0) {
|
62 | return {
|
63 | resolveId: noop
|
64 | };
|
65 | }
|
66 |
|
67 | return {
|
68 | resolveId(importee, importer) {
|
69 | const importeeId = normalizeId(importee);
|
70 | const importerId = normalizeId(importer);
|
71 |
|
72 |
|
73 | const matchedEntry = entries.find((entry) => matches(entry.find, importeeId));
|
74 | if (!matchedEntry || !importerId) {
|
75 | return null;
|
76 | }
|
77 |
|
78 | let updatedId = normalizeId(importeeId.replace(matchedEntry.find, matchedEntry.replacement));
|
79 |
|
80 | if (isFilePath(updatedId)) {
|
81 | const directory = posix.dirname(importerId);
|
82 |
|
83 |
|
84 | const filePath = posix.resolve(directory, updatedId);
|
85 | const match = resolve
|
86 | .map((ext) => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`))
|
87 | .find(exists);
|
88 |
|
89 | if (match) {
|
90 | updatedId = match;
|
91 |
|
92 |
|
93 | } else if (endsWith('.js', filePath)) {
|
94 | updatedId = filePath;
|
95 | } else {
|
96 | const indexFilePath = posix.resolve(directory, `${updatedId}/index`);
|
97 | const defaultMatch = resolve.map((ext) => `${indexFilePath}${ext}`).find(exists);
|
98 | if (defaultMatch) {
|
99 | updatedId = defaultMatch;
|
100 | } else {
|
101 | updatedId = `${filePath}.js`;
|
102 | }
|
103 | }
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | if (VOLUME.test(matchedEntry.replacement)) {
|
110 | return path.resolve(updatedId);
|
111 | }
|
112 | return updatedId;
|
113 | }
|
114 | };
|
115 | }
|
116 |
|
117 | export default alias;
|