-
Notifications
You must be signed in to change notification settings - Fork 382
/
resolve-module.ts
227 lines (194 loc) · 6.55 KB
/
resolve-module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import fs from 'fs';
import path from 'path';
import resolve from 'resolve';
import {
createRegistryEntry,
findFirstUpwardConfigPath,
isAliasModuleRecord,
isDirModuleRecord,
isNpmModuleRecord,
getLwcConfig,
getModuleEntry,
normalizeConfig,
validateNpmConfig,
mergeModules,
remapList,
transposeObject,
validateNpmAlias,
} from './utils';
import { NoLwcModuleFound, LwcConfigError } from './errors';
import {
RegistryEntry,
AliasModuleRecord,
InnerResolverOptions,
ModuleRecord,
DirModuleRecord,
ModuleResolverConfig,
NpmModuleRecord,
} from './types';
function resolveModuleFromAlias(
specifier: string,
moduleRecord: AliasModuleRecord,
opts: InnerResolverOptions
): RegistryEntry | undefined {
const { name, path: modulePath } = moduleRecord;
if (specifier !== name) {
return;
}
const entry = path.resolve(opts.rootDir, modulePath);
if (!fs.existsSync(entry)) {
throw new LwcConfigError(
`Invalid alias module record "${JSON.stringify(
moduleRecord
)}", file "${entry}" does not exist`,
{ scope: opts.rootDir }
);
}
return createRegistryEntry(entry, specifier, opts);
}
function resolveModuleFromDir(
specifier: string,
moduleRecord: DirModuleRecord,
opts: InnerResolverOptions
): RegistryEntry | undefined {
const { dir } = moduleRecord;
const absModuleDir = path.isAbsolute(dir) ? dir : path.join(opts.rootDir, dir);
if (!fs.existsSync(absModuleDir)) {
throw new LwcConfigError(
`Invalid dir module record "${JSON.stringify(
moduleRecord
)}", directory ${absModuleDir} doesn't exists`,
{ scope: opts.rootDir }
);
}
// A module dir record can only resolve module specifier with the following form "[ns]/[name]".
// We can early exit if the required specifier doesn't match.
const parts = specifier.split('/');
if (parts.length !== 2) {
return;
}
const [ns, name] = parts;
const moduleDir = path.join(absModuleDir, ns, name);
// Exit if the expected module directory doesn't exists.
if (!fs.existsSync(moduleDir)) {
return;
}
const entry = getModuleEntry(moduleDir, name, opts);
return createRegistryEntry(entry, specifier, opts);
}
function resolveModuleFromNpm(
specifier: string,
npmModuleRecord: NpmModuleRecord,
opts: InnerResolverOptions
): RegistryEntry | undefined {
const { npm, map: aliasMapping } = npmModuleRecord;
let pkgJsonPath;
try {
pkgJsonPath = resolve.sync(`${npm}/package.json`, {
basedir: opts.rootDir,
preserveSymlinks: true,
});
} catch (error: any) {
// If the module "package.json" can't be found, throw an an invalid config error. Otherwise
// rethrow the original error.
if (error.code === 'MODULE_NOT_FOUND') {
throw new LwcConfigError(
`Invalid npm module record "${JSON.stringify(
npmModuleRecord
)}", "${npm}" npm module can't be resolved`,
{ scope: opts.rootDir }
);
}
throw error;
}
const packageDir = path.dirname(pkgJsonPath);
const lwcConfig = getLwcConfig(packageDir);
validateNpmConfig(lwcConfig, { rootDir: packageDir });
let exposedModules = lwcConfig.expose;
let reverseMapping;
if (aliasMapping) {
validateNpmAlias(lwcConfig.expose, aliasMapping, { rootDir: packageDir });
exposedModules = remapList(lwcConfig.expose, aliasMapping);
reverseMapping = transposeObject(aliasMapping);
}
if (exposedModules.includes(specifier)) {
for (const moduleRecord of lwcConfig.modules) {
const aliasedSpecifier = reverseMapping && reverseMapping[specifier];
const registryEntry = resolveModuleRecordType(
aliasedSpecifier || specifier,
moduleRecord,
{
rootDir: packageDir,
}
);
if (registryEntry) {
if (aliasedSpecifier) {
registryEntry.specifier = specifier;
}
return registryEntry;
}
}
throw new LwcConfigError(
`Unable to find "${specifier}" under npm package "${npmModuleRecord.npm}"`,
{ scope: packageDir }
);
}
}
function resolveModuleRecordType(
specifier: string,
moduleRecord: ModuleRecord,
opts: InnerResolverOptions
): RegistryEntry | undefined {
const { rootDir } = opts;
if (isAliasModuleRecord(moduleRecord)) {
return resolveModuleFromAlias(specifier, moduleRecord, { rootDir });
} else if (isDirModuleRecord(moduleRecord)) {
return resolveModuleFromDir(specifier, moduleRecord, { rootDir });
} else if (isNpmModuleRecord(moduleRecord)) {
return resolveModuleFromNpm(specifier, moduleRecord, opts);
}
throw new LwcConfigError(`Unknown module record "${JSON.stringify(moduleRecord)}"`, {
scope: rootDir,
});
}
export function resolveModule(
importee: string,
dirname: string,
config?: Partial<ModuleResolverConfig>
): RegistryEntry {
if (typeof importee !== 'string') {
throw new TypeError(
`The importee argument must be a string. Received type ${typeof importee}`
);
}
if (typeof dirname !== 'string') {
throw new TypeError(
`The dirname argument must be a string. Received type ${typeof dirname}`
);
}
if (importee.startsWith('.') || importee.startsWith('/')) {
throw new TypeError(
`The importee argument must be a valid LWC module name. Received "${importee}"`
);
}
const rootDir = findFirstUpwardConfigPath(path.resolve(dirname));
const lwcConfig = getLwcConfig(rootDir);
let modules = lwcConfig.modules || [];
if (config) {
const userConfig = normalizeConfig(config, rootDir);
modules = mergeModules(userConfig.modules, modules);
}
for (const moduleRecord of modules) {
const registryEntry = resolveModuleRecordType(importee, moduleRecord, { rootDir });
if (registryEntry) {
return registryEntry;
}
}
throw new NoLwcModuleFound(importee, dirname);
}