MCPcopy
hub / github.com/babel/babel-loader / handleCache

Function handleCache

src/cache.js:147–225  ·  view source on GitHub ↗
(directory, params)

Source from the content-addressed store, hash-verified

145 * @returns {Promise<null | import("./transform").TransformResult>}
146 */
147const handleCache = async function (directory, params) {
148 const {
149 source,
150 options = {},
151 cacheIdentifier,
152 cacheDirectory,
153 cacheCompression,
154 hash,
155 getFileTimestamp,
156 logger,
157 } = params;
158
159 const file = path.join(
160 directory,
161 filename(source, cacheIdentifier, options, hash),
162 );
163
164 try {
165 // No errors mean that the file was previously cached
166 // we just need to return it
167 logger.debug(`reading cache file '${file}'`);
168 const result = await read(file, cacheCompression);
169 if (
170 !(await areExternalDependenciesModified(
171 result.externalDependencies,
172 getFileTimestamp,
173 ))
174 ) {
175 logger.debug(`validated cache file '${file}'`);
176 return result;
177 }
178 logger.debug(
179 `discarded cache file '${file}' due to changes in external dependencies`,
180 );
181 } catch {
182 // continue if cache can't be read
183 logger.debug(`discarded cache as it can not be read`);
184 }
185
186 const fallback =
187 typeof cacheDirectory !== "string" && directory !== os.tmpdir();
188
189 // Make sure the directory exists.
190 try {
191 // overwrite directory if exists
192 logger.debug(`creating cache folder '${directory}'`);
193 await mkdir(directory, { recursive: true });
194 } catch (err) {
195 if (fallback) {
196 return handleCache(os.tmpdir(), params);
197 }
198
199 throw err;
200 }
201
202 // Otherwise just transform the file
203 // return it to the user asap and write it in cache
204 logger.debug(`applying Babel transform`);

Callers 1

cache.jsFile · 0.85

Calls 5

filenameFunction · 0.85
readFunction · 0.85
addTimestampsFunction · 0.85
writeFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…