@@ -25,7 +25,7 @@ import type {ConfigRequest} from './requests/ConfigRequest';
25
25
import type { DevDepSpecifier } from './requests/DevDepRequest' ;
26
26
27
27
import invariant from 'assert' ;
28
- import { blobToStream , TapStream } from '@parcel/utils' ;
28
+ import { blobToStream , bufferStream , TapStream } from '@parcel/utils' ;
29
29
import { PluginLogger } from '@parcel/logger' ;
30
30
import ThrowableDiagnostic , { errorToDiagnostic } from '@parcel/diagnostic' ;
31
31
import { Readable } from 'stream' ;
@@ -89,6 +89,12 @@ export type BundleInfo = {|
89
89
+ isLargeBlob : boolean ,
90
90
| } ;
91
91
92
+ type BundleContent = { |
93
+ type : string ,
94
+ contents : Blob ,
95
+ map : ?string ,
96
+ | } ;
97
+
92
98
type CacheKeyMap = { |
93
99
content : string ,
94
100
map : string ,
@@ -336,18 +342,55 @@ export default class PackagerRunner {
336
342
return this . writeToCache ( cacheKey , results ) ;
337
343
}
338
344
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
+
339
388
async getBundleResult (
340
389
bundle : InternalBundle ,
341
390
bundleGraph : InternalBundleGraph ,
342
391
configs : Map < string , Config > ,
343
392
bundleConfigs : Map < string , Config > ,
344
- ) : Promise <
345
- { |
346
- type : string ,
347
- contents : Blob ,
348
- map : ?string ,
349
- | } [ ] ,
350
- > {
393
+ ) : Promise < BundleContent [ ] > {
351
394
let packagedResults = await this . package (
352
395
bundle ,
353
396
bundleGraph ,
@@ -440,15 +483,14 @@ export default class PackagerRunner {
440
483
) ;
441
484
}
442
485
443
- let res = await this . getBundleResult (
486
+ let contents = await this . getInlineBundleContents (
444
487
bundleToInternalBundle ( bundle ) ,
445
488
// $FlowFixMe
446
489
bundleGraphToInternalBundleGraph ( bundleGraph ) ,
447
490
configs ,
448
491
bundleConfigs ,
449
492
) ;
450
-
451
- return { contents : res [ 0 ] . contents } ;
493
+ return { contents} ;
452
494
} ,
453
495
} ) ;
454
496
if ( Array . isArray ( res ) ) {
0 commit comments