Webpack4Cache.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _os = _interopRequireDefault(require("os"));
  7. var _cacache = _interopRequireDefault(require("cacache"));
  8. var _findCacheDir = _interopRequireDefault(require("find-cache-dir"));
  9. var _serializeJavascript = _interopRequireDefault(require("serialize-javascript"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. class Webpack4Cache {
  12. constructor(compilation, options, weakCache) {
  13. this.cache = options.cache === true ? Webpack4Cache.getCacheDirectory() : options.cache;
  14. this.weakCache = weakCache;
  15. }
  16. static getCacheDirectory() {
  17. return (0, _findCacheDir.default)({
  18. name: 'terser-webpack-plugin'
  19. }) || _os.default.tmpdir();
  20. }
  21. async get(cacheData, {
  22. RawSource,
  23. ConcatSource,
  24. SourceMapSource
  25. }) {
  26. if (!this.cache) {
  27. // eslint-disable-next-line no-undefined
  28. return undefined;
  29. }
  30. const weakOutput = this.weakCache.get(cacheData.inputSource);
  31. if (weakOutput) {
  32. return weakOutput;
  33. } // eslint-disable-next-line no-param-reassign
  34. cacheData.cacheIdent = cacheData.cacheIdent || (0, _serializeJavascript.default)(cacheData.cacheKeys);
  35. let cachedResult;
  36. try {
  37. cachedResult = await _cacache.default.get(this.cache, cacheData.cacheIdent);
  38. } catch (ignoreError) {
  39. // eslint-disable-next-line no-undefined
  40. return undefined;
  41. }
  42. cachedResult = JSON.parse(cachedResult.data);
  43. if (cachedResult.target === 'comments') {
  44. return new ConcatSource(cachedResult.value);
  45. }
  46. const {
  47. code,
  48. name,
  49. map,
  50. input,
  51. inputSourceMap,
  52. extractedComments,
  53. banner,
  54. shebang
  55. } = cachedResult;
  56. if (map) {
  57. cachedResult.source = new SourceMapSource(code, name, map, input, inputSourceMap, true);
  58. } else {
  59. cachedResult.source = new RawSource(code);
  60. }
  61. if (banner) {
  62. cachedResult.source = new ConcatSource(shebang ? `${shebang}\n` : '', `/*! ${banner} */\n`, cachedResult.source);
  63. }
  64. if (extractedComments) {
  65. cachedResult.extractedCommentsSource = new RawSource(extractedComments);
  66. }
  67. return cachedResult;
  68. }
  69. async store(cacheData) {
  70. if (!this.cache) {
  71. // eslint-disable-next-line no-undefined
  72. return undefined;
  73. }
  74. if (!this.weakCache.has(cacheData.inputSource)) {
  75. if (cacheData.target === 'comments') {
  76. this.weakCache.set(cacheData.inputSource, cacheData.output);
  77. } else {
  78. this.weakCache.set(cacheData.inputSource, cacheData);
  79. }
  80. }
  81. let data;
  82. if (cacheData.target === 'comments') {
  83. data = {
  84. target: cacheData.target,
  85. value: cacheData.output.source()
  86. };
  87. } else {
  88. data = {
  89. code: cacheData.code,
  90. name: cacheData.name,
  91. map: cacheData.map,
  92. input: cacheData.input,
  93. inputSourceMap: cacheData.inputSourceMap,
  94. banner: cacheData.banner,
  95. shebang: cacheData.shebang
  96. };
  97. if (cacheData.extractedCommentsSource) {
  98. data.extractedComments = cacheData.extractedCommentsSource.source();
  99. data.commentsFilename = cacheData.commentsFilename;
  100. }
  101. }
  102. return _cacache.default.put(this.cache, cacheData.cacheIdent, JSON.stringify(data));
  103. }
  104. }
  105. exports.default = Webpack4Cache;