ScuttleBot

scuttlebot / tests / e2e / node_modules / playwright / lib / transform / compilationCache.js
Blame History Raw 275 lines
1
"use strict";
2
var __create = Object.create;
3
var __defProp = Object.defineProperty;
4
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
var __getOwnPropNames = Object.getOwnPropertyNames;
6
var __getProtoOf = Object.getPrototypeOf;
7
var __hasOwnProp = Object.prototype.hasOwnProperty;
8
var __export = (target, all) => {
9
for (var name in all)
10
__defProp(target, name, { get: all[name], enumerable: true });
11
};
12
var __copyProps = (to, from, except, desc) => {
13
if (from && typeof from === "object" || typeof from === "function") {
14
for (let key of __getOwnPropNames(from))
15
if (!__hasOwnProp.call(to, key) && key !== except)
16
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
}
18
return to;
19
};
20
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
// If the importer is in node compatibility mode or this is not an ESM
22
// file that has been converted to a CommonJS file using a Babel-
23
// compatible transform (i.e. "__esModule" has not been set), then set
24
// "default" to the CommonJS "module.exports" for node compatibility.
25
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
mod
27
));
28
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
var compilationCache_exports = {};
30
__export(compilationCache_exports, {
31
addToCompilationCache: () => addToCompilationCache,
32
affectedTestFiles: () => affectedTestFiles,
33
belongsToNodeModules: () => belongsToNodeModules,
34
cacheDir: () => cacheDir,
35
collectAffectedTestFiles: () => collectAffectedTestFiles,
36
currentFileDepsCollector: () => currentFileDepsCollector,
37
dependenciesForTestFile: () => dependenciesForTestFile,
38
fileDependenciesForTest: () => fileDependenciesForTest,
39
getFromCompilationCache: () => getFromCompilationCache,
40
getUserData: () => getUserData,
41
installSourceMapSupport: () => installSourceMapSupport,
42
internalDependenciesForTestFile: () => internalDependenciesForTestFile,
43
serializeCompilationCache: () => serializeCompilationCache,
44
setExternalDependencies: () => setExternalDependencies,
45
startCollectingFileDeps: () => startCollectingFileDeps,
46
stopCollectingFileDeps: () => stopCollectingFileDeps
47
});
48
module.exports = __toCommonJS(compilationCache_exports);
49
var import_fs = __toESM(require("fs"));
50
var import_os = __toESM(require("os"));
51
var import_path = __toESM(require("path"));
52
var import_utils = require("playwright-core/lib/utils");
53
var import_globals = require("../common/globals");
54
var import_utilsBundle = require("../utilsBundle");
55
const cacheDir = process.env.PWTEST_CACHE_DIR || (() => {
56
if (process.platform === "win32")
57
return import_path.default.join(import_os.default.tmpdir(), `playwright-transform-cache`);
58
return import_path.default.join(import_os.default.tmpdir(), `playwright-transform-cache-` + process.geteuid?.());
59
})();
60
const sourceMaps = /* @__PURE__ */ new Map();
61
const memoryCache = /* @__PURE__ */ new Map();
62
const fileDependencies = /* @__PURE__ */ new Map();
63
const externalDependencies = /* @__PURE__ */ new Map();
64
function installSourceMapSupport() {
65
Error.stackTraceLimit = 200;
66
import_utilsBundle.sourceMapSupport.install({
67
environment: "node",
68
handleUncaughtExceptions: false,
69
retrieveSourceMap(source) {
70
if (source.startsWith("file://") && !sourceMaps.has(source))
71
source = source.substring("file://".length);
72
if (!sourceMaps.has(source))
73
return null;
74
const sourceMapPath = sourceMaps.get(source);
75
try {
76
return {
77
map: JSON.parse(import_fs.default.readFileSync(sourceMapPath, "utf-8")),
78
url: source
79
};
80
} catch {
81
return null;
82
}
83
}
84
});
85
}
86
function _innerAddToCompilationCacheAndSerialize(filename, entry) {
87
sourceMaps.set(entry.moduleUrl || filename, entry.sourceMapPath);
88
memoryCache.set(filename, entry);
89
return {
90
sourceMaps: [[entry.moduleUrl || filename, entry.sourceMapPath]],
91
memoryCache: [[filename, entry]],
92
fileDependencies: [],
93
externalDependencies: []
94
};
95
}
96
function getFromCompilationCache(filename, contentHash, moduleUrl) {
97
const cache = memoryCache.get(filename);
98
if (cache?.codePath) {
99
try {
100
return { cachedCode: import_fs.default.readFileSync(cache.codePath, "utf-8") };
101
} catch {
102
}
103
}
104
const filePathHash = calculateFilePathHash(filename);
105
const hashPrefix = filePathHash + "_" + contentHash.substring(0, 7);
106
const cacheFolderName = filePathHash.substring(0, 2);
107
const cachePath = calculateCachePath(filename, cacheFolderName, hashPrefix);
108
const codePath = cachePath + ".js";
109
const sourceMapPath = cachePath + ".map";
110
const dataPath = cachePath + ".data";
111
try {
112
const cachedCode = import_fs.default.readFileSync(codePath, "utf8");
113
const serializedCache = _innerAddToCompilationCacheAndSerialize(filename, { codePath, sourceMapPath, dataPath, moduleUrl });
114
return { cachedCode, serializedCache };
115
} catch {
116
}
117
return {
118
addToCache: (code, map, data) => {
119
if ((0, import_globals.isWorkerProcess)())
120
return {};
121
clearOldCacheEntries(cacheFolderName, filePathHash);
122
import_fs.default.mkdirSync(import_path.default.dirname(cachePath), { recursive: true });
123
if (map)
124
import_fs.default.writeFileSync(sourceMapPath, JSON.stringify(map), "utf8");
125
if (data.size)
126
import_fs.default.writeFileSync(dataPath, JSON.stringify(Object.fromEntries(data.entries()), void 0, 2), "utf8");
127
import_fs.default.writeFileSync(codePath, code, "utf8");
128
const serializedCache = _innerAddToCompilationCacheAndSerialize(filename, { codePath, sourceMapPath, dataPath, moduleUrl });
129
return { serializedCache };
130
}
131
};
132
}
133
function serializeCompilationCache() {
134
return {
135
sourceMaps: [...sourceMaps.entries()],
136
memoryCache: [...memoryCache.entries()],
137
fileDependencies: [...fileDependencies.entries()].map(([filename, deps]) => [filename, [...deps]]),
138
externalDependencies: [...externalDependencies.entries()].map(([filename, deps]) => [filename, [...deps]])
139
};
140
}
141
function addToCompilationCache(payload) {
142
for (const entry of payload.sourceMaps)
143
sourceMaps.set(entry[0], entry[1]);
144
for (const entry of payload.memoryCache)
145
memoryCache.set(entry[0], entry[1]);
146
for (const entry of payload.fileDependencies) {
147
const existing = fileDependencies.get(entry[0]) || [];
148
fileDependencies.set(entry[0], /* @__PURE__ */ new Set([...entry[1], ...existing]));
149
}
150
for (const entry of payload.externalDependencies) {
151
const existing = externalDependencies.get(entry[0]) || [];
152
externalDependencies.set(entry[0], /* @__PURE__ */ new Set([...entry[1], ...existing]));
153
}
154
}
155
function calculateFilePathHash(filePath) {
156
return (0, import_utils.calculateSha1)(filePath).substring(0, 10);
157
}
158
function calculateCachePath(filePath, cacheFolderName, hashPrefix) {
159
const fileName = hashPrefix + "_" + import_path.default.basename(filePath, import_path.default.extname(filePath)).replace(/\W/g, "");
160
return import_path.default.join(cacheDir, cacheFolderName, fileName);
161
}
162
function clearOldCacheEntries(cacheFolderName, filePathHash) {
163
const cachePath = import_path.default.join(cacheDir, cacheFolderName);
164
try {
165
const cachedRelevantFiles = import_fs.default.readdirSync(cachePath).filter((file) => file.startsWith(filePathHash));
166
for (const file of cachedRelevantFiles)
167
import_fs.default.rmSync(import_path.default.join(cachePath, file), { force: true });
168
} catch {
169
}
170
}
171
let depsCollector;
172
function startCollectingFileDeps() {
173
depsCollector = /* @__PURE__ */ new Set();
174
}
175
function stopCollectingFileDeps(filename) {
176
if (!depsCollector)
177
return;
178
depsCollector.delete(filename);
179
for (const dep of depsCollector) {
180
if (belongsToNodeModules(dep))
181
depsCollector.delete(dep);
182
}
183
fileDependencies.set(filename, depsCollector);
184
depsCollector = void 0;
185
}
186
function currentFileDepsCollector() {
187
return depsCollector;
188
}
189
function setExternalDependencies(filename, deps) {
190
const depsSet = new Set(deps.filter((dep) => !belongsToNodeModules(dep) && dep !== filename));
191
externalDependencies.set(filename, depsSet);
192
}
193
function fileDependenciesForTest() {
194
return fileDependencies;
195
}
196
function collectAffectedTestFiles(changedFile, testFileCollector) {
197
const isTestFile = (file) => fileDependencies.has(file);
198
if (isTestFile(changedFile))
199
testFileCollector.add(changedFile);
200
for (const [testFile, deps] of fileDependencies) {
201
if (deps.has(changedFile))
202
testFileCollector.add(testFile);
203
}
204
for (const [importingFile, depsOfImportingFile] of externalDependencies) {
205
if (depsOfImportingFile.has(changedFile)) {
206
if (isTestFile(importingFile))
207
testFileCollector.add(importingFile);
208
for (const [testFile, depsOfTestFile] of fileDependencies) {
209
if (depsOfTestFile.has(importingFile))
210
testFileCollector.add(testFile);
211
}
212
}
213
}
214
}
215
function affectedTestFiles(changes) {
216
const result = /* @__PURE__ */ new Set();
217
for (const change of changes)
218
collectAffectedTestFiles(change, result);
219
return [...result];
220
}
221
function internalDependenciesForTestFile(filename) {
222
return fileDependencies.get(filename);
223
}
224
function dependenciesForTestFile(filename) {
225
const result = /* @__PURE__ */ new Set();
226
for (const testDependency of fileDependencies.get(filename) || []) {
227
result.add(testDependency);
228
for (const externalDependency of externalDependencies.get(testDependency) || [])
229
result.add(externalDependency);
230
}
231
for (const dep of externalDependencies.get(filename) || [])
232
result.add(dep);
233
return result;
234
}
235
const kPlaywrightInternalPrefix = import_path.default.resolve(__dirname, "../../../playwright");
236
function belongsToNodeModules(file) {
237
if (file.includes(`${import_path.default.sep}node_modules${import_path.default.sep}`))
238
return true;
239
if (file.startsWith(kPlaywrightInternalPrefix) && (file.endsWith(".js") || file.endsWith(".mjs")))
240
return true;
241
return false;
242
}
243
async function getUserData(pluginName) {
244
const result = /* @__PURE__ */ new Map();
245
for (const [fileName, cache] of memoryCache) {
246
if (!cache.dataPath)
247
continue;
248
if (!import_fs.default.existsSync(cache.dataPath))
249
continue;
250
const data = JSON.parse(await import_fs.default.promises.readFile(cache.dataPath, "utf8"));
251
if (data[pluginName])
252
result.set(fileName, data[pluginName]);
253
}
254
return result;
255
}
256
// Annotate the CommonJS export names for ESM import in node:
257
0 && (module.exports = {
258
addToCompilationCache,
259
affectedTestFiles,
260
belongsToNodeModules,
261
cacheDir,
262
collectAffectedTestFiles,
263
currentFileDepsCollector,
264
dependenciesForTestFile,
265
fileDependenciesForTest,
266
getFromCompilationCache,
267
getUserData,
268
installSourceMapSupport,
269
internalDependenciesForTestFile,
270
serializeCompilationCache,
271
setExternalDependencies,
272
startCollectingFileDeps,
273
stopCollectingFileDeps
274
});
275

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button