Skip to content

Commit a993f98

Browse files
committed
Cache inline bundle packaging
1 parent 638b12d commit a993f98

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

packages/core/core/src/PackagerRunner.js

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type {ConfigRequest} from './requests/ConfigRequest';
2525
import type {DevDepSpecifier} from './requests/DevDepRequest';
2626

2727
import invariant from 'assert';
28-
import {blobToStream, TapStream} from '@parcel/utils';
28+
import {blobToStream, bufferStream, TapStream} from '@parcel/utils';
2929
import {PluginLogger} from '@parcel/logger';
3030
import ThrowableDiagnostic, {errorToDiagnostic} from '@parcel/diagnostic';
3131
import {Readable} from 'stream';
@@ -89,6 +89,12 @@ export type BundleInfo = {|
8989
+isLargeBlob: boolean,
9090
|};
9191

92+
type BundleContent = {|
93+
type: string,
94+
contents: Blob,
95+
map: ?string,
96+
|};
97+
9298
type CacheKeyMap = {|
9399
content: string,
94100
map: string,
@@ -336,18 +342,55 @@ export default class PackagerRunner {
336342
return this.writeToCache(cacheKey, results);
337343
}
338344

345+
async getInlineBundleContents(
346+
bundle: InternalBundle,
347+
bundleGraph: InternalBundleGraph,
348+
configs: Map<string, Config>,
349+
bundleConfigs: Map<string, Config>,
350+
): Promise<Blob> {
351+
let bundleInfo = await this.getBundleInfoFromCache(
352+
bundleGraph,
353+
bundle,
354+
configs,
355+
bundleConfigs,
356+
);
357+
358+
if (bundleInfo.length > 0) {
359+
return this.options.cache.getBlob(bundleInfo[0].cacheKeys.content);
360+
}
361+
362+
let results = await this.getBundleResult(
363+
bundle,
364+
bundleGraph,
365+
configs,
366+
bundleConfigs,
367+
);
368+
369+
// Writing to the cache consumes the stream, but we need to also return it to the calling packager.
370+
// Buffer the stream into memory first.
371+
if (results[0].contents instanceof Readable) {
372+
results[0].contents = await bufferStream(results[0].contents);
373+
}
374+
375+
// Recompute cache keys as they may have changed due to dev dependencies.
376+
let cacheKey = await this.getCacheKey(
377+
bundle,
378+
bundleGraph,
379+
configs,
380+
bundleConfigs,
381+
[...this.invalidations.values()],
382+
);
383+
384+
await this.writeToCache(cacheKey, results);
385+
return results[0].contents;
386+
}
387+
339388
async getBundleResult(
340389
bundle: InternalBundle,
341390
bundleGraph: InternalBundleGraph,
342391
configs: Map<string, Config>,
343392
bundleConfigs: Map<string, Config>,
344-
): Promise<
345-
{|
346-
type: string,
347-
contents: Blob,
348-
map: ?string,
349-
|}[],
350-
> {
393+
): Promise<BundleContent[]> {
351394
let packagedResults = await this.package(
352395
bundle,
353396
bundleGraph,
@@ -440,15 +483,14 @@ export default class PackagerRunner {
440483
);
441484
}
442485

443-
let res = await this.getBundleResult(
486+
let contents = await this.getInlineBundleContents(
444487
bundleToInternalBundle(bundle),
445488
// $FlowFixMe
446489
bundleGraphToInternalBundleGraph(bundleGraph),
447490
configs,
448491
bundleConfigs,
449492
);
450-
451-
return {contents: res[0].contents};
493+
return {contents};
452494
},
453495
});
454496
if (Array.isArray(res)) {

0 commit comments

Comments
 (0)