123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- const os = require("os");
- const path = require("path");
- const zlib = require("zlib");
- const crypto = require("crypto");
- const {
- promisify
- } = require("util");
- const {
- readFile,
- writeFile,
- mkdir
- } = require("fs/promises");
- const findCacheDirP = import("find-cache-dir");
- const transform = require("./transform");
- let defaultCacheDirectory = null;
- let hashType = "sha256";
- try {
- crypto.createHash(hashType);
- } catch {
- hashType = "md5";
- }
- 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) {
- const hash = crypto.createHash(hashType);
- const contents = JSON.stringify({
- source,
- options,
- identifier
- });
- hash.update(contents);
- return hash.digest("hex") + ".json";
- };
- const handleCache = async function (directory, params) {
- const {
- source,
- options = {},
- cacheIdentifier,
- cacheDirectory,
- cacheCompression,
- logger
- } = params;
- const file = path.join(directory, filename(source, cacheIdentifier, options));
- try {
-
-
- logger.debug(`reading cache file '${file}'`);
- return await read(file, cacheCompression);
- } 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);
-
-
- if (!result.externalDependencies.length) {
- 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 {
- if (defaultCacheDirectory === null) {
- const {
- default: findCacheDir
- } = await findCacheDirP;
- defaultCacheDirectory = findCacheDir({
- name: "babel-loader"
- }) || os.tmpdir();
- }
- directory = defaultCacheDirectory;
- }
- return await handleCache(directory, params);
- };
|