cache.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * Filesystem Cache
  3. *
  4. * Given a file and a transform function, cache the result into files
  5. * or retrieve the previously cached files if the given file is already known.
  6. *
  7. * @see https://github.com/babel/babel-loader/issues/34
  8. * @see https://github.com/babel/babel-loader/pull/41
  9. */
  10. const os = require("os");
  11. const path = require("path");
  12. const zlib = require("zlib");
  13. const {
  14. promisify
  15. } = require("util");
  16. const {
  17. readFile,
  18. writeFile,
  19. mkdir
  20. } = require("fs/promises");
  21. const {
  22. sync: findUpSync
  23. } = require("find-up");
  24. const {
  25. env
  26. } = process;
  27. const transform = require("./transform");
  28. const serialize = require("./serialize");
  29. let defaultCacheDirectory = null;
  30. const gunzip = promisify(zlib.gunzip);
  31. const gzip = promisify(zlib.gzip);
  32. /**
  33. * Read the contents from the compressed file.
  34. *
  35. * @async
  36. * @params {String} filename
  37. * @params {Boolean} compress
  38. */
  39. const read = async function (filename, compress) {
  40. const data = await readFile(filename + (compress ? ".gz" : ""));
  41. const content = compress ? await gunzip(data) : data;
  42. return JSON.parse(content.toString());
  43. };
  44. /**
  45. * Write contents into a compressed file.
  46. *
  47. * @async
  48. * @params {String} filename
  49. * @params {Boolean} compress
  50. * @params {String} result
  51. */
  52. const write = async function (filename, compress, result) {
  53. const content = JSON.stringify(result);
  54. const data = compress ? await gzip(content) : content;
  55. return await writeFile(filename + (compress ? ".gz" : ""), data);
  56. };
  57. /**
  58. * Build the filename for the cached file
  59. *
  60. * @params {String} source File source code
  61. * @params {Object} options Options used
  62. *
  63. * @return {String}
  64. */
  65. const filename = function (source, identifier, options, hash) {
  66. hash.update(serialize([options, source, identifier]));
  67. return hash.digest("hex") + ".json";
  68. };
  69. const addTimestamps = async function (externalDependencies, getFileTimestamp) {
  70. for (const depAndEmptyTimestamp of externalDependencies) {
  71. try {
  72. const [dep] = depAndEmptyTimestamp;
  73. const {
  74. timestamp
  75. } = await getFileTimestamp(dep);
  76. depAndEmptyTimestamp.push(timestamp);
  77. } catch {
  78. // ignore errors if timestamp is not available
  79. }
  80. }
  81. };
  82. const areExternalDependenciesModified = async function (externalDepsWithTimestamp, getFileTimestamp) {
  83. for (const depAndTimestamp of externalDepsWithTimestamp) {
  84. const [dep, timestamp] = depAndTimestamp;
  85. let newTimestamp;
  86. try {
  87. newTimestamp = (await getFileTimestamp(dep)).timestamp;
  88. } catch {
  89. return true;
  90. }
  91. if (timestamp !== newTimestamp) {
  92. return true;
  93. }
  94. }
  95. return false;
  96. };
  97. /**
  98. * Handle the cache
  99. *
  100. * @params {String} directory
  101. * @params {Object} params
  102. */
  103. const handleCache = async function (directory, params) {
  104. const {
  105. source,
  106. options = {},
  107. cacheIdentifier,
  108. cacheDirectory,
  109. cacheCompression,
  110. hash,
  111. getFileTimestamp,
  112. logger
  113. } = params;
  114. const file = path.join(directory, filename(source, cacheIdentifier, options, hash));
  115. try {
  116. // No errors mean that the file was previously cached
  117. // we just need to return it
  118. logger.debug(`reading cache file '${file}'`);
  119. const result = await read(file, cacheCompression);
  120. if (!(await areExternalDependenciesModified(result.externalDependencies, getFileTimestamp))) {
  121. logger.debug(`validated cache file '${file}'`);
  122. return result;
  123. }
  124. logger.debug(`discarded cache file '${file}' due to changes in external dependencies`);
  125. } catch {
  126. // conitnue if cache can't be read
  127. logger.debug(`discarded cache as it can not be read`);
  128. }
  129. const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir();
  130. // Make sure the directory exists.
  131. try {
  132. // overwrite directory if exists
  133. logger.debug(`creating cache folder '${directory}'`);
  134. await mkdir(directory, {
  135. recursive: true
  136. });
  137. } catch (err) {
  138. if (fallback) {
  139. return handleCache(os.tmpdir(), params);
  140. }
  141. throw err;
  142. }
  143. // Otherwise just transform the file
  144. // return it to the user asap and write it in cache
  145. logger.debug(`applying Babel transform`);
  146. const result = await transform(source, options);
  147. await addTimestamps(result.externalDependencies, getFileTimestamp);
  148. try {
  149. logger.debug(`writing result to cache file '${file}'`);
  150. await write(file, cacheCompression, result);
  151. } catch (err) {
  152. if (fallback) {
  153. // Fallback to tmpdir if node_modules folder not writable
  154. return handleCache(os.tmpdir(), params);
  155. }
  156. throw err;
  157. }
  158. return result;
  159. };
  160. /**
  161. * Retrieve file from cache, or create a new one for future reads
  162. *
  163. * @async
  164. * @param {Object} params
  165. * @param {String} params.cacheDirectory Directory to store cached files
  166. * @param {String} params.cacheIdentifier Unique identifier to bust cache
  167. * @param {Boolean} params.cacheCompression Whether compressing cached files
  168. * @param {String} params.source Original contents of the file to be cached
  169. * @param {Object} params.options Options to be given to the transform fn
  170. *
  171. * @example
  172. *
  173. * const result = await cache({
  174. * cacheDirectory: '.tmp/cache',
  175. * cacheIdentifier: 'babel-loader-cachefile',
  176. * cacheCompression: false,
  177. * source: *source code from file*,
  178. * options: {
  179. * experimental: true,
  180. * runtime: true
  181. * },
  182. * });
  183. */
  184. module.exports = async function (params) {
  185. let directory;
  186. if (typeof params.cacheDirectory === "string") {
  187. directory = params.cacheDirectory;
  188. } else {
  189. defaultCacheDirectory ??= findCacheDir("babel-loader");
  190. directory = defaultCacheDirectory;
  191. }
  192. return await handleCache(directory, params);
  193. };
  194. function findCacheDir(name) {
  195. if (env.CACHE_DIR && !["true", "false", "1", "0"].includes(env.CACHE_DIR)) {
  196. return path.join(env.CACHE_DIR, name);
  197. }
  198. const rootPkgJSONPath = path.dirname(findUpSync("package.json"));
  199. if (rootPkgJSONPath) {
  200. return path.join(rootPkgJSONPath, "node_modules", ".cache", name);
  201. }
  202. return os.tmpdir();
  203. }