123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- const os = require("os");
- const path = require("path");
- const zlib = require("zlib");
- const {
- promisify
- } = require("util");
- const {
- readFile,
- writeFile,
- mkdir
- } = require("fs/promises");
- const {
- sync: findUpSync
- } = require("find-up");
- const {
- env
- } = process;
- const transform = require("./transform");
- const serialize = require("./serialize");
- let defaultCacheDirectory = null;
- const gunzip = promisify(zlib.gunzip);
- const gzip = promisify(zlib.gzip);
- const read = async function (filename, compress) {
- const data = await readFile(filename + (compress ? ".gz" : ""));
- const content = compress ? await gunzip(data) : data;
- return JSON.parse(content.toString());
- };
- const write = async function (filename, compress, result) {
- const content = JSON.stringify(result);
- const data = compress ? await gzip(content) : content;
- return await writeFile(filename + (compress ? ".gz" : ""), data);
- };
- const filename = function (source, identifier, options, hash) {
- hash.update(serialize([options, source, identifier]));
- return hash.digest("hex") + ".json";
- };
- const addTimestamps = async function (externalDependencies, getFileTimestamp) {
- for (const depAndEmptyTimestamp of externalDependencies) {
- try {
- const [dep] = depAndEmptyTimestamp;
- const {
- timestamp
- } = await getFileTimestamp(dep);
- depAndEmptyTimestamp.push(timestamp);
- } catch {
-
- }
- }
- };
- const areExternalDependenciesModified = async function (externalDepsWithTimestamp, getFileTimestamp) {
- for (const depAndTimestamp of externalDepsWithTimestamp) {
- const [dep, timestamp] = depAndTimestamp;
- let newTimestamp;
- try {
- newTimestamp = (await getFileTimestamp(dep)).timestamp;
- } catch {
- return true;
- }
- if (timestamp !== newTimestamp) {
- return true;
- }
- }
- return false;
- };
- const handleCache = async function (directory, params) {
- const {
- source,
- options = {},
- cacheIdentifier,
- cacheDirectory,
- cacheCompression,
- hash,
- getFileTimestamp,
- logger
- } = params;
- const file = path.join(directory, filename(source, cacheIdentifier, options, hash));
- try {
-
-
- logger.debug(`reading cache file '${file}'`);
- const result = await read(file, cacheCompression);
- if (!(await areExternalDependenciesModified(result.externalDependencies, getFileTimestamp))) {
- logger.debug(`validated cache file '${file}'`);
- return result;
- }
- logger.debug(`discarded cache file '${file}' due to changes in external dependencies`);
- } catch {
-
- logger.debug(`discarded cache as it can not be read`);
- }
- const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir();
-
- try {
-
- logger.debug(`creating cache folder '${directory}'`);
- await mkdir(directory, {
- recursive: true
- });
- } catch (err) {
- if (fallback) {
- return handleCache(os.tmpdir(), params);
- }
- throw err;
- }
-
-
- logger.debug(`applying Babel transform`);
- const result = await transform(source, options);
- await addTimestamps(result.externalDependencies, getFileTimestamp);
- try {
- logger.debug(`writing result to cache file '${file}'`);
- await write(file, cacheCompression, result);
- } catch (err) {
- if (fallback) {
-
- return handleCache(os.tmpdir(), params);
- }
- throw err;
- }
- return result;
- };
- module.exports = async function (params) {
- let directory;
- if (typeof params.cacheDirectory === "string") {
- directory = params.cacheDirectory;
- } else {
- defaultCacheDirectory ??= findCacheDir("babel-loader");
- directory = defaultCacheDirectory;
- }
- return await handleCache(directory, params);
- };
- function findCacheDir(name) {
- if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) {
- return path.join(env.CACHE_DIR, name);
- }
- const rootPkgJSONPath = path.dirname(findUpSync("package.json"));
- if (rootPkgJSONPath) {
- return path.join(rootPkgJSONPath, "node_modules", ".cache", name);
- }
- return os.tmpdir();
- }
|