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