UNPKG

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