cache.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  3. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  4. /**
  5. * Filesystem Cache
  6. *
  7. * Given a file and a transform function, cache the result into files
  8. * or retrieve the previously cached files if the given file is already known.
  9. *
  10. * @see https://github.com/babel/babel-loader/issues/34
  11. * @see https://github.com/babel/babel-loader/pull/41
  12. */
  13. const fs = require("fs");
  14. const os = require("os");
  15. const path = require("path");
  16. const zlib = require("zlib");
  17. const crypto = require("crypto");
  18. const findCacheDir = require("find-cache-dir");
  19. const {
  20. promisify
  21. } = require("util");
  22. const transform = require("./transform"); // Lazily instantiated when needed
  23. let defaultCacheDirectory = null;
  24. let hashType = "md4"; // use md5 hashing if md4 is not available
  25. try {
  26. crypto.createHash(hashType);
  27. } catch (err) {
  28. hashType = "md5";
  29. }
  30. const readFile = promisify(fs.readFile);
  31. const writeFile = promisify(fs.writeFile);
  32. const gunzip = promisify(zlib.gunzip);
  33. const gzip = promisify(zlib.gzip);
  34. const makeDir = require("make-dir");
  35. /**
  36. * Read the contents from the compressed file.
  37. *
  38. * @async
  39. * @params {String} filename
  40. * @params {Boolean} compress
  41. */
  42. const read = /*#__PURE__*/function () {
  43. var _ref = _asyncToGenerator(function* (filename, compress) {
  44. const data = yield readFile(filename + (compress ? ".gz" : ""));
  45. const content = compress ? yield gunzip(data) : data;
  46. return JSON.parse(content.toString());
  47. });
  48. return function read(_x, _x2) {
  49. return _ref.apply(this, arguments);
  50. };
  51. }();
  52. /**
  53. * Write contents into a compressed file.
  54. *
  55. * @async
  56. * @params {String} filename
  57. * @params {Boolean} compress
  58. * @params {String} result
  59. */
  60. const write = /*#__PURE__*/function () {
  61. var _ref2 = _asyncToGenerator(function* (filename, compress, result) {
  62. const content = JSON.stringify(result);
  63. const data = compress ? yield gzip(content) : content;
  64. return yield writeFile(filename + (compress ? ".gz" : ""), data);
  65. });
  66. return function write(_x3, _x4, _x5) {
  67. return _ref2.apply(this, arguments);
  68. };
  69. }();
  70. /**
  71. * Build the filename for the cached file
  72. *
  73. * @params {String} source File source code
  74. * @params {Object} options Options used
  75. *
  76. * @return {String}
  77. */
  78. const filename = function (source, identifier, options) {
  79. const hash = crypto.createHash(hashType);
  80. const contents = JSON.stringify({
  81. source,
  82. options,
  83. identifier
  84. });
  85. hash.update(contents);
  86. return hash.digest("hex") + ".json";
  87. };
  88. /**
  89. * Handle the cache
  90. *
  91. * @params {String} directory
  92. * @params {Object} params
  93. */
  94. const handleCache = /*#__PURE__*/function () {
  95. var _ref3 = _asyncToGenerator(function* (directory, params) {
  96. const {
  97. source,
  98. options = {},
  99. cacheIdentifier,
  100. cacheDirectory,
  101. cacheCompression
  102. } = params;
  103. const file = path.join(directory, filename(source, cacheIdentifier, options));
  104. try {
  105. // No errors mean that the file was previously cached
  106. // we just need to return it
  107. return yield read(file, cacheCompression);
  108. } catch (err) {}
  109. const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir(); // Make sure the directory exists.
  110. try {
  111. yield makeDir(directory);
  112. } catch (err) {
  113. if (fallback) {
  114. return handleCache(os.tmpdir(), params);
  115. }
  116. throw err;
  117. } // Otherwise just transform the file
  118. // return it to the user asap and write it in cache
  119. const result = yield transform(source, options);
  120. try {
  121. yield write(file, cacheCompression, result);
  122. } catch (err) {
  123. if (fallback) {
  124. // Fallback to tmpdir if node_modules folder not writable
  125. return handleCache(os.tmpdir(), params);
  126. }
  127. throw err;
  128. }
  129. return result;
  130. });
  131. return function handleCache(_x6, _x7) {
  132. return _ref3.apply(this, arguments);
  133. };
  134. }();
  135. /**
  136. * Retrieve file from cache, or create a new one for future reads
  137. *
  138. * @async
  139. * @param {Object} params
  140. * @param {String} params.cacheDirectory Directory to store cached files
  141. * @param {String} params.cacheIdentifier Unique identifier to bust cache
  142. * @param {Boolean} params.cacheCompression Whether compressing cached files
  143. * @param {String} params.source Original contents of the file to be cached
  144. * @param {Object} params.options Options to be given to the transform fn
  145. *
  146. * @example
  147. *
  148. * const result = await cache({
  149. * cacheDirectory: '.tmp/cache',
  150. * cacheIdentifier: 'babel-loader-cachefile',
  151. * cacheCompression: false,
  152. * source: *source code from file*,
  153. * options: {
  154. * experimental: true,
  155. * runtime: true
  156. * },
  157. * });
  158. */
  159. module.exports = /*#__PURE__*/function () {
  160. var _ref4 = _asyncToGenerator(function* (params) {
  161. let directory;
  162. if (typeof params.cacheDirectory === "string") {
  163. directory = params.cacheDirectory;
  164. } else {
  165. if (defaultCacheDirectory === null) {
  166. defaultCacheDirectory = findCacheDir({
  167. name: "babel-loader"
  168. }) || os.tmpdir();
  169. }
  170. directory = defaultCacheDirectory;
  171. }
  172. return yield handleCache(directory, params);
  173. });
  174. return function (_x8) {
  175. return _ref4.apply(this, arguments);
  176. };
  177. }();