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 => ({ find: key, replacement: entries[key] }));
|
52 | };
|
53 |
|
54 | function alias(options = {}) {
|
55 | const resolve = Array.isArray(options.resolve) ? options.resolve : ['.js'];
|
56 | const entries = getEntries(options);
|
57 |
|
58 |
|
59 | if (entries.length === 0) {
|
60 | return {
|
61 | resolveId: noop,
|
62 | };
|
63 | }
|
64 |
|
65 | return {
|
66 | resolveId(importee, importer) {
|
67 | const importeeId = normalizeId(importee);
|
68 | const importerId = normalizeId(importer);
|
69 |
|
70 |
|
71 | const matchedEntry = entries.find(entry => matches(entry.find, importeeId));
|
72 | if (!matchedEntry || !importerId) {
|
73 | return null;
|
74 | }
|
75 |
|
76 | let updatedId = normalizeId(importeeId.replace(matchedEntry.find, matchedEntry.replacement));
|
77 |
|
78 | if (isFilePath(updatedId)) {
|
79 | const directory = posix.dirname(importerId);
|
80 |
|
81 |
|
82 | const filePath = posix.resolve(directory, updatedId);
|
83 | const match = resolve.map(ext => (endsWith(ext, filePath) ? filePath : `${filePath}${ext}`))
|
84 | .find(exists);
|
85 |
|
86 | if (match) {
|
87 | updatedId = match;
|
88 |
|
89 |
|
90 | } else if (endsWith('.js', filePath)) {
|
91 | updatedId = filePath;
|
92 | } else {
|
93 | const indexFilePath = posix.resolve(directory, `${updatedId}/index`);
|
94 | const defaultMatch = resolve.map(ext => `${indexFilePath}${ext}`).find(exists);
|
95 | if (defaultMatch) {
|
96 | updatedId = defaultMatch;
|
97 | } else {
|
98 | updatedId = filePath + '.js';
|
99 | }
|
100 | }
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | if (VOLUME.test(matchedEntry.replacement)) {
|
107 | return path.resolve(updatedId);
|
108 | }
|
109 | return updatedId;
|
110 | },
|
111 | };
|
112 | }
|
113 |
|
114 | export default alias;
|