cache.js 5.6 KB

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