1 | "use strict";
|
2 |
|
3 | 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); } }
|
4 |
|
5 | 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); }); }; }
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const fs = require("fs");
|
17 |
|
18 | const os = require("os");
|
19 |
|
20 | const path = require("path");
|
21 |
|
22 | const zlib = require("zlib");
|
23 |
|
24 | const crypto = require("crypto");
|
25 |
|
26 | const findCacheDir = require("find-cache-dir");
|
27 |
|
28 | const {
|
29 | promisify
|
30 | } = require("util");
|
31 |
|
32 | const transform = require("./transform");
|
33 |
|
34 |
|
35 | let defaultCacheDirectory = null;
|
36 | let hashType = "md4";
|
37 |
|
38 | try {
|
39 | crypto.createHash(hashType);
|
40 | } catch (err) {
|
41 | hashType = "md5";
|
42 | }
|
43 |
|
44 | const readFile = promisify(fs.readFile);
|
45 | const writeFile = promisify(fs.writeFile);
|
46 | const gunzip = promisify(zlib.gunzip);
|
47 | const gzip = promisify(zlib.gzip);
|
48 |
|
49 | const makeDir = require("make-dir");
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | const read = function () {
|
60 | var _ref = _asyncToGenerator(function* (filename, compress) {
|
61 | const data = yield readFile(filename + (compress ? ".gz" : ""));
|
62 | const content = compress ? yield gunzip(data) : data;
|
63 | return JSON.parse(content.toString());
|
64 | });
|
65 |
|
66 | return function read(_x, _x2) {
|
67 | return _ref.apply(this, arguments);
|
68 | };
|
69 | }();
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | const write = function () {
|
81 | var _ref2 = _asyncToGenerator(function* (filename, compress, result) {
|
82 | const content = JSON.stringify(result);
|
83 | const data = compress ? yield gzip(content) : content;
|
84 | return yield writeFile(filename + (compress ? ".gz" : ""), data);
|
85 | });
|
86 |
|
87 | return function write(_x3, _x4, _x5) {
|
88 | return _ref2.apply(this, arguments);
|
89 | };
|
90 | }();
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | const filename = function (source, identifier, options) {
|
102 | const hash = crypto.createHash(hashType);
|
103 | const contents = JSON.stringify({
|
104 | source,
|
105 | options,
|
106 | identifier
|
107 | });
|
108 | hash.update(contents);
|
109 | return hash.digest("hex") + ".json";
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | const handleCache = function () {
|
120 | var _ref3 = _asyncToGenerator(function* (directory, params) {
|
121 | const {
|
122 | source,
|
123 | options = {},
|
124 | cacheIdentifier,
|
125 | cacheDirectory,
|
126 | cacheCompression
|
127 | } = params;
|
128 | const file = path.join(directory, filename(source, cacheIdentifier, options));
|
129 |
|
130 | try {
|
131 |
|
132 |
|
133 | return yield read(file, cacheCompression);
|
134 | } catch (err) {}
|
135 |
|
136 | const fallback = typeof cacheDirectory !== "string" && directory !== os.tmpdir();
|
137 |
|
138 | try {
|
139 | yield makeDir(directory);
|
140 | } catch (err) {
|
141 | if (fallback) {
|
142 | return handleCache(os.tmpdir(), params);
|
143 | }
|
144 |
|
145 | throw err;
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 | const result = yield transform(source, options);
|
151 |
|
152 | try {
|
153 | yield write(file, cacheCompression, result);
|
154 | } catch (err) {
|
155 | if (fallback) {
|
156 |
|
157 | return handleCache(os.tmpdir(), params);
|
158 | }
|
159 |
|
160 | throw err;
|
161 | }
|
162 |
|
163 | return result;
|
164 | });
|
165 |
|
166 | return function handleCache(_x6, _x7) {
|
167 | return _ref3.apply(this, arguments);
|
168 | };
|
169 | }();
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | module.exports = function () {
|
197 | var _ref4 = _asyncToGenerator(function* (params) {
|
198 | let directory;
|
199 |
|
200 | if (typeof params.cacheDirectory === "string") {
|
201 | directory = params.cacheDirectory;
|
202 | } else {
|
203 | if (defaultCacheDirectory === null) {
|
204 | defaultCacheDirectory = findCacheDir({
|
205 | name: "babel-loader"
|
206 | }) || os.tmpdir();
|
207 | }
|
208 |
|
209 | directory = defaultCacheDirectory;
|
210 | }
|
211 |
|
212 | return yield handleCache(directory, params);
|
213 | });
|
214 |
|
215 | return function (_x8) {
|
216 | return _ref4.apply(this, arguments);
|
217 | };
|
218 | }(); |
\ | No newline at end of file |