88 */
99'use strict' ;
1010
11- const fs = require ( 'fs' ) ;
1211const log = require ( '../util/log' ) . out ( 'bundle' ) ;
12+ const outputBundle = require ( './output/bundle' ) ;
1313const Promise = require ( 'promise' ) ;
1414const ReactPackager = require ( '../../packager/react-packager' ) ;
1515const saveAssets = require ( './saveAssets' ) ;
1616
17- const sign = require ( './sign' ) ;
18-
19- function saveBundleAndMap (
20- bundle ,
21- bundleOutput ,
22- encoding ,
23- sourcemapOutput ,
24- dev
25- ) {
26- log ( 'start' ) ;
27- let codeWithMap ;
28- if ( ! dev ) {
29- codeWithMap = bundle . getMinifiedSourceAndMap ( dev ) ;
30- } else {
31- codeWithMap = {
32- code : bundle . getSource ( { dev } ) ,
33- map : JSON . stringify ( bundle . getSourceMap ( { dev } ) ) ,
34- } ;
35- }
36- log ( 'finish' ) ;
37-
38- log ( 'Writing bundle output to:' , bundleOutput ) ;
39- fs . writeFileSync ( bundleOutput , sign ( codeWithMap . code ) , encoding ) ;
40- log ( 'Done writing bundle output' ) ;
41-
42- if ( sourcemapOutput ) {
43- log ( 'Writing sourcemap output to:' , sourcemapOutput ) ;
44- fs . writeFileSync ( sourcemapOutput , codeWithMap . map ) ;
45- log ( 'Done writing sourcemap output' ) ;
46- }
47- }
48-
49- function savePrepackBundleAndMap (
50- bundle ,
51- bundleOutput ,
52- sourcemapOutput ,
53- bridgeConfig
54- ) {
55- log ( 'Writing prepack bundle output to:' , bundleOutput ) ;
56- const result = bundle . build ( {
57- batchedBridgeConfig : bridgeConfig
58- } ) ;
59- fs . writeFileSync ( bundleOutput , result , 'ucs-2' ) ;
60- log ( 'Done writing prepack bundle output' ) ;
61- }
62-
63- function buildBundle ( args , config ) {
17+ function buildBundle ( args , config , output = outputBundle ) {
6418 return new Promise ( ( resolve , reject ) => {
6519
6620 // This is used by a bazillion of npm modules we don't control so we don't
@@ -82,55 +36,36 @@ function buildBundle(args, config) {
8236 platform : args . platform ,
8337 } ;
8438
85- const prepack = args . prepack ;
86-
87- const client = ReactPackager . createClientFor ( options ) ;
88-
89- client . then ( ( ) => log ( 'Created ReactPackager' ) ) ;
39+ const clientPromise = ReactPackager . createClientFor ( options ) ;
9040
9141 // Build and save the bundle
92- let bundle ;
93- if ( prepack ) {
94- bundle = client . then ( c => c . buildPrepackBundle ( requestOpts ) )
95- . then ( outputBundle => {
96- savePrepackBundleAndMap (
97- outputBundle ,
98- args [ 'bundle-output' ] ,
99- args [ 'sourcemap-output' ] ,
100- args [ 'bridge-config' ]
101- ) ;
102- return outputBundle ;
103- } ) ;
104- } else {
105- bundle = client . then ( c => c . buildBundle ( requestOpts ) )
106- . then ( outputBundle => {
107- saveBundleAndMap (
108- outputBundle ,
109- args [ 'bundle-output' ] ,
110- args [ 'bundle-encoding' ] ,
111- args [ 'sourcemap-output' ] ,
112- args . dev
113- ) ;
114- return outputBundle ;
115- } ) ;
116- }
42+ const bundlePromise = clientPromise
43+ . then ( client => {
44+ log ( 'Created ReactPackager' ) ;
45+ return output . build ( client , requestOpts ) ;
46+ } )
47+ . then ( bundle => {
48+ output . save ( bundle , args , log ) ;
49+ return bundle ;
50+ } ) ;
11751
11852 // When we're done bundling, close the client
119- bundle . then ( ( ) => client . then ( c => {
120- log ( 'Closing client' ) ;
121- c . close ( ) ;
122- } ) ) ;
53+ Promise . all ( [ clientPromise , bundlePromise ] )
54+ . then ( ( [ client ] ) => {
55+ log ( 'Closing client' ) ;
56+ client . close ( ) ;
57+ } ) ;
12358
12459 // Save the assets of the bundle
125- const assets = bundle
126- . then ( outputBundle => outputBundle . getAssets ( ) )
127- . then ( outputAssets => saveAssets (
128- outputAssets ,
129- args . platform ,
130- args [ 'assets-dest' ]
131- ) ) ;
132-
133- // When we're done saving the assets, we're done.
60+ const assets = bundlePromise
61+ . then ( bundle => bundle . getAssets ( ) )
62+ . then ( outputAssets => saveAssets (
63+ outputAssets ,
64+ args . platform ,
65+ args [ 'assets-dest' ]
66+ ) ) ;
67+
68+ // When we're done saving bundle output and the assets, we're done.
13469 resolve ( assets ) ;
13570 } ) ;
13671}
0 commit comments