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