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