UNPKG

4.12 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/fold
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
11 if (k2 === undefined) k2 = k;
12 var desc = Object.getOwnPropertyDescriptor(m, k);
13 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
14 desc = { enumerable: true, get: function() { return m[k]; } };
15 }
16 Object.defineProperty(o, k2, desc);
17}) : (function(o, m, k, k2) {
18 if (k2 === undefined) k2 = k;
19 o[k2] = m[k];
20}));
21var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
22 Object.defineProperty(o, "default", { enumerable: true, value: v });
23}) : function(o, v) {
24 o["default"] = v;
25});
26var __importStar = (this && this.__importStar) || function (mod) {
27 if (mod && mod.__esModule) return mod;
28 var result = {};
29 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
30 __setModuleDefault(result, mod);
31 return result;
32};
33Object.defineProperty(exports, "__esModule", { value: true });
34exports.ImportAliases = void 0;
35const path_1 = require("path");
36const IocLookupException_1 = require("../Exceptions/IocLookupException");
37/**
38 * Manages the import aliases
39 */
40class ImportAliases {
41 constructor(container) {
42 this.container = container;
43 /**
44 * Registered aliases
45 */
46 this.list = {};
47 /**
48 * In-memory require cache to speed up lookup calls. Yes, "require"
49 * is slow. Check "perf/require.js"
50 */
51 this.requireCache = new Map();
52 }
53 /**
54 * Returns the matching alias for the given namespace
55 */
56 getPathAlias(namespace) {
57 return Object.keys(this.list).find((alias) => {
58 return namespace.startsWith(`${alias}/`);
59 });
60 }
61 /**
62 * Returns path for a given alias
63 */
64 makeAliasPath(namespace, alias) {
65 return (0, path_1.normalize)(namespace.replace(alias, this.list[alias]));
66 }
67 /**
68 * Register an import alias
69 */
70 register(absolutePath, alias) {
71 this.list[alias] = absolutePath;
72 return this;
73 }
74 /**
75 * Find if a namespace is part of the import aliases
76 */
77 has(namespace) {
78 return !!this.getPathAlias(namespace);
79 }
80 /**
81 * Import the namespace from the registered import aliases.
82 */
83 resolve(namespace) {
84 const alias = this.getPathAlias(namespace);
85 if (!alias) {
86 throw IocLookupException_1.IocLookupException.lookupFailed(namespace);
87 }
88 const cacheItem = this.requireCache.get(namespace);
89 if (cacheItem) {
90 return cacheItem.value;
91 }
92 /**
93 * Absolute path to the module
94 */
95 const diskPath = this.makeAliasPath(namespace, alias);
96 /**
97 * Require the module
98 */
99 const value = require(diskPath);
100 /**
101 * Cache the output
102 */
103 this.requireCache.set(namespace, { diskPath, value });
104 /**
105 * Return the value
106 */
107 return value;
108 }
109 /**
110 * Same as [[resolve]] but uses ES modules
111 */
112 async resolveAsync(namespace) {
113 /**
114 * Piggy back on resolve when using cjs module system
115 */
116 if (this.container.module === 'cjs') {
117 return this.resolve(namespace);
118 }
119 const alias = this.getPathAlias(namespace);
120 if (!alias) {
121 throw IocLookupException_1.IocLookupException.lookupFailed(namespace);
122 }
123 /**
124 * Import the module. The following code will only compile to esm
125 * when the output of this build is esm
126 */
127 return Promise.resolve().then(() => __importStar(require(this.makeAliasPath(namespace, alias))));
128 }
129}
130exports.ImportAliases = ImportAliases;