ScuttleBot

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

Keyboard Shortcuts

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