-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathfindJsModulesFor.js
172 lines (156 loc) · 5.14 KB
/
findJsModulesFor.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
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
// @flow
import minimatch from 'minimatch';
import sortBy from 'lodash/sortBy';
import uniqBy from 'lodash/uniqBy';
import Configuration from './Configuration';
import JsModule from './JsModule';
import ModuleFinder from './ModuleFinder';
function findImportsFromEnvironment(
config: Configuration,
variableName: string,
): Array<JsModule> {
return config
.get('coreModules')
.filter((dep: string): boolean => dep.toLowerCase() === variableName.toLowerCase())
.map((dep: string): JsModule => new JsModule({
importPath: dep,
variableName,
}));
}
function findJsModulesFromModuleFinder(
config: Configuration,
normalizedName: string,
variableName: string,
finder: ModuleFinder,
pathToCurrentFile: string,
options: Object = {},
): Promise<Array<JsModule>> {
return new Promise((resolve: Function, reject: Function) => {
let isWantedPackageDependency = Boolean;
if (!config.get('importDevDependencies')) {
const packageDependencies = config.get('packageDependencies');
isWantedPackageDependency = (packageName: string): boolean => packageDependencies.has(
packageName,
);
}
const isSearch = !!options.search;
const method = isSearch ? 'search' : 'find';
finder[method](normalizedName)
.then((exports: Array<Object>) => {
const modules = exports.map(({
name,
path,
isDefault,
isType,
packageName,
}: Object): ?JsModule => { // eslint-disable-line object-curly-newline
// Filter out modules that are in the `excludes` config.
const isExcluded = config.get('excludes')
.some((glob: string): boolean => minimatch(path, glob));
if (isExcluded) {
return undefined;
}
if (packageName) {
if (!isWantedPackageDependency(packageName)) {
return undefined;
}
return new JsModule({
importPath: packageName,
variableName: isSearch ? name : variableName,
hasNamedExports: !isDefault,
isType,
});
}
return JsModule.construct({
hasNamedExports: !isDefault,
isType,
relativeFilePath: path,
stripFileExtensions: config.get('stripFileExtensions', {
pathToImportedModule: path,
}),
makeRelativeTo: config.get('useRelativePaths', {
pathToImportedModule: path,
})
&& config.pathToCurrentFile,
variableName: isSearch ? name : variableName,
workingDirectory: config.workingDirectory,
});
});
resolve(modules.filter(Boolean));
})
.catch(reject);
});
}
export function dedupeAndSort(modules: Array<JsModule>): Array<JsModule> {
// We might end up having duplicate modules here. In order to dedupe
// these, we remove the module with the longest path
const sorted = sortBy(
modules,
(module: JsModule): number => module.importPath.length,
);
const uniques = uniqBy(
sorted,
// Default export and named export with same name from the same module are not considered dupes
(module: JsModule): string => [module.importPath, module.hasNamedExports].join(),
);
// Sorting by path, but with default exports before named exports
return sortBy(uniques, (module: JsModule): string => [
module.importPath,
module.hasNamedExports ? 1 : 0,
].join());
}
const NON_PATH_ALIAS_PATTERN = /^[a-zA-Z0-9-_]+$/;
type FindJsModulesForOptionsType = {
search: boolean,
};
export default function findJsModulesFor(
config: Configuration,
variableName: string,
options?: FindJsModulesForOptionsType,
): Promise<Array<JsModule>> {
return new Promise((resolve: Function, reject: Function) => {
let normalizedName = variableName;
const alias = config.resolveAlias(variableName);
if (alias) {
if (NON_PATH_ALIAS_PATTERN.test(alias)) {
// The alias is likely a package dependency. We can use it in the
// ModuleFinder lookup.
normalizedName = alias;
} else {
// The alias is a path of some sort. Use it directly as the moduleName
// in the import.
resolve([new JsModule({ importPath: alias, variableName })]);
return;
}
}
const namedImportsModule = config.resolveNamedExports(variableName);
if (namedImportsModule) {
resolve([namedImportsModule]);
return;
}
const matchedModules = [];
matchedModules.push(...findImportsFromEnvironment(config, variableName));
const finder = ModuleFinder.getForWorkingDirectory(
config.workingDirectory,
{
excludes: config.get('excludes'),
ignorePackagePrefixes: config.get('ignorePackagePrefixes'),
},
);
findJsModulesFromModuleFinder(
config,
normalizedName,
variableName,
finder,
config.pathToCurrentFile,
options,
)
.then((modules: Array<JsModule>) => {
matchedModules.push(...modules);
resolve(dedupeAndSort(matchedModules));
})
.catch((error: Object) => {
reject(error);
});
});
}