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