/
node-haste.js
170 lines (142 loc) · 4.79 KB
/
node-haste.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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
* @format
*/
"use strict";
const AssetResolutionCache = require("../../node-haste/AssetResolutionCache");
const DependencyGraphHelpers = require("../../node-haste/DependencyGraph/DependencyGraphHelpers");
const FilesByDirNameIndex = require("../../node-haste/FilesByDirNameIndex");
const HasteFS = require("./HasteFS");
const Module = require("./Module");
const ModuleCache = require("./ModuleCache");
const defaults = require("metro-config/src/defaults/defaults");
const parsePlatformFilePath = require("../../node-haste/lib/parsePlatformFilePath");
const path = require("path");
const _require = require("../../node-haste/DependencyGraph/ModuleResolution"),
ModuleResolver = _require.ModuleResolver;
const _require2 = require("jest-haste-map"),
ModuleMap = _require2.ModuleMap;
const platforms = new Set(defaults.platforms);
const GENERIC_PLATFORM = "g";
const PACKAGE_JSON = path.sep + "package.json";
const NULL_MODULE = {
path: "/",
getPackage() {},
isHaste() {
throw new Error("not implemented");
},
getName() {
throw new Error("not implemented");
}
}; // This function maps the ModuleGraph data structure to jest-haste-map's ModuleMap
const createModuleMap = _ref => {
let files = _ref.files,
helpers = _ref.helpers,
moduleCache = _ref.moduleCache,
sourceExts = _ref.sourceExts;
const map = new Map();
files.forEach(filePath => {
if (helpers.isNodeModulesDir(filePath)) {
return;
}
let id;
let module;
if (filePath.endsWith(PACKAGE_JSON)) {
module = moduleCache.getPackage(filePath);
id = module.data.name;
} else if (sourceExts.indexOf(path.extname(filePath).substr(1)) !== -1) {
module = moduleCache.getModule(filePath);
id = module.name;
}
if (!(id && module && module.isHaste())) {
return;
}
const mapModule = map.get(id) || Object.create(null);
const platform =
parsePlatformFilePath(filePath, platforms).platform || GENERIC_PLATFORM;
const existingModule = mapModule[platform]; // 0 = Module, 1 = Package in jest-haste-map
mapModule[platform] = [filePath, module.type === "Package" ? 1 : 0];
if (existingModule && existingModule[0] !== filePath) {
throw new Error(
[
"@providesModule naming collision:",
` Duplicate module name: \`${id}\``,
` Paths: \`${filePath}\` collides with \`${existingModule[0]}\``,
"",
"This error is caused by a @providesModule declaration " +
"with the same name across two different files."
].join("\n")
);
}
map.set(id, mapModule);
});
return map;
};
exports.createResolveFn = function(options) {
const assetExts = options.assetExts,
extraNodeModules = options.extraNodeModules,
transformedFiles = options.transformedFiles,
sourceExts = options.sourceExts,
platform = options.platform;
const files = Object.keys(transformedFiles);
function getTransformedFile(path) {
const result = transformedFiles[path];
if (!result) {
throw new Error(`"${path} does not exist`);
}
return result;
}
const helpers = new DependencyGraphHelpers({
assetExts
});
const hasteFS = new HasteFS(files);
const moduleCache = new ModuleCache(
filePath => hasteFS.closest(filePath, "package.json"),
getTransformedFile
);
const filesByDirNameIndex = new FilesByDirNameIndex(files);
const assetResolutionCache = new AssetResolutionCache({
assetExtensions: new Set(assetExts),
getDirFiles: dirPath => filesByDirNameIndex.getAllFiles(dirPath),
platforms
});
const moduleResolver = new ModuleResolver({
dirExists: filePath => hasteFS.dirExists(filePath),
doesFileExist: filePath => hasteFS.exists(filePath),
extraNodeModules,
isAssetFile: filePath => helpers.isAssetFile(filePath),
mainFields: options.mainFields,
moduleCache,
moduleMap: new ModuleMap({
duplicates: new Map(),
map: createModuleMap({
files,
helpers,
moduleCache,
sourceExts
}),
mocks: new Map(),
rootDir: ""
}),
preferNativePlatform: true,
projectRoot: "",
resolveAsset: (dirPath, assetName, platform) =>
assetResolutionCache.resolve(dirPath, assetName, platform),
resolveRequest: options.resolveRequest,
sourceExts
});
return (id, sourcePath) => {
const from =
sourcePath != null
? new Module(sourcePath, moduleCache, getTransformedFile(sourcePath))
: NULL_MODULE;
const allowHaste = !helpers.isNodeModulesDir(from.path);
return moduleResolver.resolveDependency(from, id, allowHaste, platform)
.path;
};
};