1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createAsset = createAsset;
|
7 | exports.default = void 0;
|
8 |
|
9 | var _stream = require("stream");
|
10 |
|
11 | var _crypto = _interopRequireDefault(require("crypto"));
|
12 |
|
13 | var _sourceMap = _interopRequireDefault(require("@parcel/source-map"));
|
14 |
|
15 | var _utils = require("@parcel/utils");
|
16 |
|
17 | var _Dependency = require("./Dependency");
|
18 |
|
19 | var _Environment = require("./Environment");
|
20 |
|
21 | var _constants = require("./constants");
|
22 |
|
23 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
24 |
|
25 | function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
26 |
|
27 | function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
28 |
|
29 | function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
30 |
|
31 | function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
32 |
|
33 | function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
34 |
|
35 | function createAsset(options) {
|
36 | let idBase = options.idBase != null ? options.idBase : options.filePath;
|
37 | let uniqueKey = options.uniqueKey || '';
|
38 | return {
|
39 | id: options.id != null ? options.id : (0, _utils.md5FromString)(idBase + options.type + (0, _Environment.getEnvironmentHash)(options.env) + uniqueKey),
|
40 | hash: options.hash,
|
41 | filePath: options.filePath,
|
42 | isIsolated: options.isIsolated == null ? false : options.isIsolated,
|
43 | isInline: options.isInline == null ? false : options.isInline,
|
44 | isSplittable: options.isSplittable,
|
45 | type: options.type,
|
46 | contentKey: options.contentKey,
|
47 | mapKey: options.mapKey,
|
48 | dependencies: options.dependencies || new Map(),
|
49 | includedFiles: options.includedFiles || new Map(),
|
50 | isSource: options.isSource,
|
51 | outputHash: options.outputHash || '',
|
52 | pipeline: options.pipeline,
|
53 | env: options.env,
|
54 | meta: options.meta || {},
|
55 | stats: options.stats,
|
56 | symbols: options.symbols || new Map(),
|
57 | sideEffects: options.sideEffects != null ? options.sideEffects : true,
|
58 | uniqueKey: uniqueKey
|
59 | };
|
60 | }
|
61 |
|
62 | class InternalAsset {
|
63 | constructor({
|
64 | value,
|
65 | options,
|
66 | content,
|
67 | map,
|
68 | ast,
|
69 | idBase
|
70 | }) {
|
71 | _defineProperty(this, "value", void 0);
|
72 |
|
73 | _defineProperty(this, "options", void 0);
|
74 |
|
75 | _defineProperty(this, "content", void 0);
|
76 |
|
77 | _defineProperty(this, "map", void 0);
|
78 |
|
79 | _defineProperty(this, "ast", void 0);
|
80 |
|
81 | _defineProperty(this, "idBase", void 0);
|
82 |
|
83 | this.value = value;
|
84 | this.options = options;
|
85 | this.content = content || '';
|
86 | this.map = map;
|
87 | this.ast = ast;
|
88 | this.idBase = idBase;
|
89 | }
|
90 | |
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | async commit(pipelineKey) {
|
97 | this.ast = null;
|
98 | let contentStream = this.getStream();
|
99 |
|
100 | if (
|
101 | typeof contentStream.bytesRead === 'number' &&
|
102 |
|
103 | contentStream.bytesRead !== contentStream.readableLength) {
|
104 | throw new Error('Stream has already been read. This may happen if a plugin reads from a stream and does not replace it.');
|
105 | }
|
106 |
|
107 | let size = 0;
|
108 |
|
109 | let hash = _crypto.default.createHash('md5');
|
110 |
|
111 |
|
112 |
|
113 | let [contentKey, mapKey] = await Promise.all([this.options.cache.setStream(this.getCacheKey('content' + pipelineKey), contentStream.pipe(new _utils.TapStream(buf => {
|
114 | size += buf.length;
|
115 | hash.update(buf);
|
116 | }))), this.map == null ? Promise.resolve() : this.options.cache.set(this.getCacheKey('map' + pipelineKey), this.map)]);
|
117 | this.value.contentKey = contentKey;
|
118 | this.value.mapKey = mapKey;
|
119 | this.value.stats.size = size;
|
120 | this.value.outputHash = hash.digest('hex');
|
121 | }
|
122 |
|
123 | async getCode() {
|
124 | if (this.value.contentKey != null) {
|
125 | this.content = this.options.cache.getStream(this.value.contentKey);
|
126 | }
|
127 |
|
128 | if (typeof this.content === 'string' || this.content instanceof Buffer) {
|
129 | this.content = this.content.toString();
|
130 | } else {
|
131 | this.content = (await (0, _utils.bufferStream)(this.content)).toString();
|
132 | }
|
133 |
|
134 | return this.content;
|
135 | }
|
136 |
|
137 | async getBuffer() {
|
138 | if (this.value.contentKey != null) {
|
139 | this.content = this.options.cache.getStream(this.value.contentKey);
|
140 | }
|
141 |
|
142 | if (typeof this.content === 'string' || this.content instanceof Buffer) {
|
143 | return Buffer.from(this.content);
|
144 | }
|
145 |
|
146 | this.content = await (0, _utils.bufferStream)(this.content);
|
147 | return this.content;
|
148 | }
|
149 |
|
150 | getStream() {
|
151 | if (this.value.contentKey != null) {
|
152 | this.content = this.options.cache.getStream(this.value.contentKey);
|
153 | }
|
154 |
|
155 | return (0, _utils.blobToStream)(this.content);
|
156 | }
|
157 |
|
158 | setCode(code) {
|
159 | this.content = code;
|
160 | }
|
161 |
|
162 | setBuffer(buffer) {
|
163 | this.content = buffer;
|
164 | }
|
165 |
|
166 | setStream(stream) {
|
167 | this.content = stream;
|
168 | }
|
169 |
|
170 | async getMap() {
|
171 | if (this.value.mapKey != null) {
|
172 | this.map = await this.options.cache.get(this.value.mapKey);
|
173 | }
|
174 |
|
175 | return this.map;
|
176 | }
|
177 |
|
178 | setMap(map) {
|
179 | this.map = map;
|
180 | }
|
181 |
|
182 | getCacheKey(key) {
|
183 | return (0, _utils.md5FromString)(_constants.PARCEL_VERSION + key + this.value.id + (this.value.hash || ''));
|
184 | }
|
185 |
|
186 | addDependency(opts) {
|
187 |
|
188 | let {
|
189 | env,
|
190 | target
|
191 | } = opts,
|
192 | rest = _objectWithoutProperties(opts, ["env", "target"]);
|
193 |
|
194 | let dep = (0, _Dependency.createDependency)(_objectSpread({}, rest, {
|
195 | env: (0, _Environment.mergeEnvironments)(this.value.env, env),
|
196 | sourceAssetId: this.value.id,
|
197 | sourcePath: this.value.filePath
|
198 | }));
|
199 | let existing = this.value.dependencies.get(dep.id);
|
200 |
|
201 | if (existing) {
|
202 | (0, _Dependency.mergeDependencies)(existing, dep);
|
203 | } else {
|
204 | this.value.dependencies.set(dep.id, dep);
|
205 | }
|
206 |
|
207 | return dep.id;
|
208 | }
|
209 |
|
210 | addIncludedFile(file) {
|
211 | this.value.includedFiles.set(file.filePath, file);
|
212 | }
|
213 |
|
214 | getIncludedFiles() {
|
215 | return Array.from(this.value.includedFiles.values());
|
216 | }
|
217 |
|
218 | getDependencies() {
|
219 | return Array.from(this.value.dependencies.values());
|
220 | }
|
221 |
|
222 | createChildAsset(result) {
|
223 | var _ref, _result$content, _result$isIsolated, _result$isInline, _result$isSplittable, _result$isSource, _result$pipeline, _result$sideEffects;
|
224 |
|
225 | let content = (_ref = (_result$content = result.content) !== null && _result$content !== void 0 ? _result$content : result.code) !== null && _ref !== void 0 ? _ref : '';
|
226 | let hash;
|
227 | let size;
|
228 |
|
229 | if (content === this.content) {
|
230 | hash = this.value.hash;
|
231 | size = this.value.stats.size;
|
232 | } else if (typeof content === 'string' || content instanceof Buffer) {
|
233 | hash = (0, _utils.md5FromString)(content);
|
234 | size = content.length;
|
235 | } else {
|
236 | hash = null;
|
237 | size = NaN;
|
238 | }
|
239 |
|
240 | let asset = new InternalAsset({
|
241 | value: createAsset({
|
242 | idBase: this.idBase,
|
243 | hash,
|
244 | filePath: this.value.filePath,
|
245 | type: result.type,
|
246 | isIsolated: (_result$isIsolated = result.isIsolated) !== null && _result$isIsolated !== void 0 ? _result$isIsolated : this.value.isIsolated,
|
247 | isInline: (_result$isInline = result.isInline) !== null && _result$isInline !== void 0 ? _result$isInline : this.value.isInline,
|
248 | isSplittable: (_result$isSplittable = result.isSplittable) !== null && _result$isSplittable !== void 0 ? _result$isSplittable : this.value.isSplittable,
|
249 | isSource: (_result$isSource = result.isSource) !== null && _result$isSource !== void 0 ? _result$isSource : this.value.isSource,
|
250 | env: (0, _Environment.mergeEnvironments)(this.value.env, result.env),
|
251 | dependencies: this.value.type === result.type ? new Map(this.value.dependencies) : new Map(),
|
252 | includedFiles: new Map(this.value.includedFiles),
|
253 | meta: _objectSpread({}, this.value.meta, {}, result.meta),
|
254 | pipeline: (_result$pipeline = result.pipeline) !== null && _result$pipeline !== void 0 ? _result$pipeline : this.value.type === result.type ? this.value.pipeline : null,
|
255 | stats: {
|
256 | time: 0,
|
257 | size
|
258 | },
|
259 | symbols: new Map([...this.value.symbols, ...(result.symbols || [])]),
|
260 | sideEffects: (_result$sideEffects = result.sideEffects) !== null && _result$sideEffects !== void 0 ? _result$sideEffects : this.value.sideEffects,
|
261 | uniqueKey: result.uniqueKey
|
262 | }),
|
263 | options: this.options,
|
264 | content,
|
265 | ast: result.ast,
|
266 | map: result.map,
|
267 | idBase: this.idBase
|
268 | });
|
269 | let dependencies = result.dependencies;
|
270 |
|
271 | if (dependencies) {
|
272 | for (let dep of dependencies) {
|
273 | asset.addDependency(dep);
|
274 | }
|
275 | }
|
276 |
|
277 | let includedFiles = result.includedFiles;
|
278 |
|
279 | if (includedFiles) {
|
280 | for (let file of includedFiles) {
|
281 | asset.addIncludedFile(file);
|
282 | }
|
283 | }
|
284 |
|
285 | return asset;
|
286 | }
|
287 |
|
288 | async getConfig(filePaths, options) {
|
289 | let packageKey = options === null || options === void 0 ? void 0 : options.packageKey;
|
290 | let parse = options && options.parse;
|
291 |
|
292 | if (packageKey != null) {
|
293 | let pkg = await this.getPackage();
|
294 |
|
295 | if (pkg && pkg[packageKey]) {
|
296 | return pkg[packageKey];
|
297 | }
|
298 | }
|
299 |
|
300 | let conf = await (0, _utils.loadConfig)(this.options.inputFS, this.value.filePath, filePaths, parse == null ? null : {
|
301 | parse
|
302 | });
|
303 |
|
304 | if (!conf) {
|
305 | return null;
|
306 | }
|
307 |
|
308 | for (let file of conf.files) {
|
309 | this.addIncludedFile(file);
|
310 | }
|
311 |
|
312 | return conf.config;
|
313 | }
|
314 |
|
315 | getPackage() {
|
316 | return this.getConfig(['package.json']);
|
317 | }
|
318 |
|
319 | }
|
320 |
|
321 | exports.default = InternalAsset; |
\ | No newline at end of file |