cache.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 mkdirpOrig = require("mkdirp");
  19. const findCacheDir = require("find-cache-dir");
  20. const promisify = require("pify");
  21. const transform = require("./transform"); // Lazily instantiated when needed
  22. let defaultCacheDirectory = null;
  23. const readFile = promisify(fs.readFile);
  24. const writeFile = promisify(fs.writeFile);
  25. const gunzip = promisify(zlib.gunzip);
  26. const gzip = promisify(zlib.gzip);
  27. const mkdirp = promisify(mkdirpOrig);
  28. /**
  29. * Read the contents from the compressed file.
  30. *
  31. * @async
  32. * @params {String} filename
  33. */
  34. const read =
  35. /*#__PURE__*/
  36. function () {
  37. var _ref = _asyncToGenerator(function* (filename, compress) {
  38. const data = yield readFile(filename + (compress ? ".gz" : ""));
  39. const content = compress ? yield gunzip(data) : data;
  40. return JSON.parse(content.toString());
  41. });
  42. return function read(_x, _x2) {
  43. return _ref.apply(this, arguments);
  44. };
  45. }();
  46. /**
  47. * Write contents into a compressed file.
  48. *
  49. * @async
  50. * @params {String} filename
  51. * @params {String} result
  52. */
  53. const write =
  54. /*#__PURE__*/
  55. function () {
  56. var _ref2 = _asyncToGenerator(function* (filename, compress, result) {
  57. const content = JSON.stringify(result);
  58. const data = compress ? yield gzip(content) : content;
  59. return yield writeFile(filename + (compress ? ".gz" : ""), data);
  60. });
  61. return function write(_x3, _x4, _x5) {
  62. return _ref2.apply(this, arguments);
  63. };
  64. }();
  65. /**
  66. * Build the filename for the cached file
  67. *
  68. * @params {String} source File source code
  69. * @params {Object} options Options used
  70. *
  71. * @return {String}
  72. */
  73. const filename = function (source, identifier, options) {
  74. const hash = crypto.createHash("md4");
  75. const contents = JSON.stringify({
  76. source,
  77. options,
  78. identifier
  79. });
  80. hash.update(contents);
  81. return hash.digest("hex") + ".json";
  82. };
  83. /**
  84. * Handle the cache
  85. *
  86. * @params {String} directory
  87. * @params {Object} params
  88. */
  89. const handleCache =
  90. /*#__PURE__*/
  91. function () {
  92. var _ref3 = _asyncToGenerator(function* (directory, params) {
  93. const {
  94. source,
  95. options = {},
  96. cacheIdentifier,
  97. cacheDirectory,
  98. cacheCompression
  99. } = params;
  100. const file = path.join(directory, filename(source, cacheIdentifier, options));
  101. try {
  102. // No errors mean that the file was previously cached
  103. // we just need to return it
  104. return yield read(file, cacheCompression);
  105. } catch (err) {}
  106. const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir(); // Make sure the directory exists.
  107. try {
  108. yield mkdirp(directory);
  109. } catch (err) {
  110. if (fallback) {
  111. return handleCache(os.tmpdir(), params);
  112. }
  113. throw err;
  114. } // Otherwise just transform the file
  115. // return it to the user asap and write it in cache
  116. const result = yield transform(source, options);
  117. try {
  118. yield write(file, cacheCompression, result);
  119. } catch (err) {
  120. if (fallback) {
  121. // Fallback to tmpdir if node_modules folder not writable
  122. return handleCache(os.tmpdir(), params);
  123. }
  124. throw err;
  125. }
  126. return result;
  127. });
  128. return function handleCache(_x6, _x7) {
  129. return _ref3.apply(this, arguments);
  130. };
  131. }();
  132. /**
  133. * Retrieve file from cache, or create a new one for future reads
  134. *
  135. * @async
  136. * @param {Object} params
  137. * @param {String} params.directory Directory to store cached files
  138. * @param {String} params.identifier Unique identifier to bust cache
  139. * @param {String} params.source Original contents of the file to be cached
  140. * @param {Object} params.options Options to be given to the transform fn
  141. * @param {Function} params.transform Function that will transform the
  142. * original file and whose result will be
  143. * cached
  144. *
  145. * @example
  146. *
  147. * cache({
  148. * directory: '.tmp/cache',
  149. * identifier: 'babel-loader-cachefile',
  150. * cacheCompression: false,
  151. * source: *source code from file*,
  152. * options: {
  153. * experimental: true,
  154. * runtime: true
  155. * },
  156. * transform: function(source, options) {
  157. * var content = *do what you need with the source*
  158. * return content;
  159. * }
  160. * }, function(err, result) {
  161. *
  162. * });
  163. */
  164. module.exports =
  165. /*#__PURE__*/
  166. function () {
  167. var _ref4 = _asyncToGenerator(function* (params) {
  168. let directory;
  169. if (typeof params.cacheDirectory === "string") {
  170. directory = params.cacheDirectory;
  171. } else {
  172. if (defaultCacheDirectory === null) {
  173. defaultCacheDirectory = findCacheDir({
  174. name: "babel-loader"
  175. }) || os.tmpdir();
  176. }
  177. directory = defaultCacheDirectory;
  178. }
  179. return yield handleCache(directory, params);
  180. });
  181. return function (_x8) {
  182. return _ref4.apply(this, arguments);
  183. };
  184. }();