-
Notifications
You must be signed in to change notification settings - Fork 172
/
resolver.js
83 lines (68 loc) · 1.98 KB
/
resolver.js
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
/*
* 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
*/
'use strict';
const fs = require('fs');
const path = require('path');
const lwcResolver = require('@lwc/jest-resolver');
const { PROJECT_ROOT, getModulePaths } = require('./utils/project.js');
const { getInfoFromId } = require('./utils/module.js');
const DEFAULT_NAMESPACE = 'c';
function isFile(file) {
let result;
try {
const stat = fs.statSync(file);
result = stat.isFile() || stat.isFIFO();
} catch (e) {
if (!(e && e.code === 'ENOENT')) {
throw e;
}
result = false;
}
return result;
}
function resolveAsFile(name, extensions) {
if (isFile(name)) {
return name;
}
for (let i = 0; i < extensions.length; i++) {
const file = name + extensions[i];
if (isFile(file)) {
return file;
}
}
return undefined;
}
function getLightningMock(modulePath) {
const p = path.join(__dirname, 'lightning-stubs', modulePath);
if (fs.existsSync(p)) {
return path.join(p, modulePath + '.js');
}
}
function getModule(modulePath, options) {
const { ns, name } = getInfoFromId(modulePath);
if (ns === 'lightning') {
return getLightningMock(name);
}
if (ns === DEFAULT_NAMESPACE) {
const paths = getModulePaths();
for (let i = 0; i < paths.length; i++) {
const file = resolveAsFile(
path.join(PROJECT_ROOT, paths[i], name, name),
options.extensions,
);
if (file) {
return fs.realpathSync(file);
}
}
}
}
module.exports = function (modulePath, options) {
if (modulePath === 'lwc') {
return require.resolve('@lwc/engine-dom');
}
return getModule(modulePath, options) || lwcResolver.apply(null, arguments);
};