UNPKG

3.48 kBJavaScriptView Raw
1'use strict';
2
3var os = require('os');
4var slash = require('slash');
5
6function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
7
8var slash__default = /*#__PURE__*/_interopDefaultLegacy(slash);
9
10const VOLUME = /^([A-Z]:)/i;
11const IS_WINDOWS = os.platform() === 'win32';
12const noop = () => null;
13function matches(pattern, importee) {
14 if (pattern instanceof RegExp) {
15 return pattern.test(importee);
16 }
17 if (importee.length < pattern.length) {
18 return false;
19 }
20 if (importee === pattern) {
21 return true;
22 }
23 const importeeStartsWithKey = importee.indexOf(pattern) === 0;
24 const importeeHasSlashAfterKey = importee.substring(pattern.length)[0] === '/';
25 return importeeStartsWithKey && importeeHasSlashAfterKey;
26}
27function normalizeId(id) {
28 if (typeof id === 'string' && (IS_WINDOWS || VOLUME.test(id))) {
29 return slash__default['default'](id.replace(VOLUME, ''));
30 }
31 return id;
32}
33function getEntries({ entries }) {
34 if (!entries) {
35 return [];
36 }
37 if (Array.isArray(entries)) {
38 return entries;
39 }
40 return Object.entries(entries).map(([key, value]) => {
41 return { find: key, replacement: value };
42 });
43}
44function getCustomResolver({ customResolver }, options) {
45 if (typeof customResolver === 'function') {
46 return customResolver;
47 }
48 if (customResolver && typeof customResolver.resolveId === 'function') {
49 return customResolver.resolveId;
50 }
51 if (typeof options.customResolver === 'function') {
52 return options.customResolver;
53 }
54 if (options.customResolver && typeof options.customResolver.resolveId === 'function') {
55 return options.customResolver.resolveId;
56 }
57 return null;
58}
59function alias(options = {}) {
60 const entries = getEntries(options);
61 if (entries.length === 0) {
62 return {
63 name: 'alias',
64 resolveId: noop
65 };
66 }
67 return {
68 name: 'alias',
69 buildStart(inputOptions) {
70 return Promise.all([...entries, options].map(({ customResolver }) => customResolver &&
71 typeof customResolver === 'object' &&
72 typeof customResolver.buildStart === 'function' &&
73 customResolver.buildStart.call(this, inputOptions))).then(() => {
74 // enforce void return value
75 });
76 },
77 resolveId(importee, importer) {
78 const importeeId = normalizeId(importee);
79 const importerId = normalizeId(importer);
80 // First match is supposed to be the correct one
81 const matchedEntry = entries.find((entry) => matches(entry.find, importeeId));
82 if (!matchedEntry || !importerId) {
83 return null;
84 }
85 const updatedId = normalizeId(importeeId.replace(matchedEntry.find, matchedEntry.replacement));
86 const customResolver = getCustomResolver(matchedEntry, options);
87 if (customResolver) {
88 return customResolver.call(this, updatedId, importerId, {});
89 }
90 return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => {
91 let finalResult = resolved;
92 if (!finalResult) {
93 finalResult = { id: updatedId };
94 }
95 return finalResult;
96 });
97 }
98 };
99}
100
101module.exports = alias;