Skip to content

Commit 32fa0b7

Browse files
davidaureliofacebook-github-bot-4
authored andcommitted
Move bundle output formats to their own files
Reviewed By: tadeuzagallo Differential Revision: D2702834 fb-gh-sync-id: fca308c5078a11924e071077822fb77d762bd564
1 parent 3c382a7 commit 32fa0b7

File tree

5 files changed

+149
-92
lines changed

5 files changed

+149
-92
lines changed

local-cli/bundle/buildBundle.js

Lines changed: 26 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,13 @@
88
*/
99
'use strict';
1010

11-
const fs = require('fs');
1211
const log = require('../util/log').out('bundle');
12+
const outputBundle = require('./output/bundle');
1313
const Promise = require('promise');
1414
const ReactPackager = require('../../packager/react-packager');
1515
const 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
}

local-cli/bundle/bundle.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
const buildBundle = require('./buildBundle');
1212
const bundleCommandLineArgs = require('./bundleCommandLineArgs');
1313
const parseCommandLine = require('../util/parseCommandLine');
14+
const outputBundle = require('./output/bundle');
15+
const outputPrepack = require('./output/prepack');
1416

1517
/**
1618
* Builds the bundle starting to look for dependencies at the given entry path.
1719
*/
1820
function bundle(argv, config) {
19-
return buildBundle(parseCommandLine(bundleCommandLineArgs, argv), config);
21+
const args = parseCommandLine(bundleCommandLineArgs, argv);
22+
const output = args.prepack ? outputPrepack : outputBundle;
23+
return buildBundle(args, config, output);
2024
}
2125

2226
module.exports = bundle;

local-cli/bundle/output/bundle.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
const Promise = require('promise');
12+
const sign = require('../sign');
13+
const writeFile = require('./writeFile');
14+
15+
function buildBundle(packagerClient, requestOptions) {
16+
return packagerClient.buildBundle(requestOptions);
17+
}
18+
19+
function createCodeWithMap(bundle, dev) {
20+
if (!dev) {
21+
return bundle.getMinifiedSourceAndMap(dev);
22+
} else {
23+
return {
24+
code: bundle.getSource({dev}),
25+
map: JSON.stringify(bundle.getSourceMap({dev})),
26+
};
27+
}
28+
}
29+
30+
function saveBundleAndMap(bundle, options, log) {
31+
const {
32+
'bundle-output': bundleOutput,
33+
'bundle-encoding': encoding,
34+
dev,
35+
'sourcemap-output': sourcemapOutput,
36+
} = options;
37+
38+
log('start');
39+
const codeWithMap = createCodeWithMap(bundle, dev);
40+
log('finish');
41+
42+
log('Writing bundle output to:', bundleOutput);
43+
const writeBundle = writeFile(bundleOutput, sign(codeWithMap.code), encoding);
44+
writeBundle.then(() => log('Done writing bundle output'));
45+
46+
if (sourcemapOutput) {
47+
log('Writing sourcemap output to:', sourcemapOutput);
48+
const writeMap = writeFile(sourcemapOutput, codeWithMap.map, null);
49+
writeMap.then(() => log('Done writing sourcemap output'));
50+
return Promise.all([writeBundle, writeMap]);
51+
} else {
52+
return writeBundle;
53+
}
54+
}
55+
56+
57+
exports.build = buildBundle;
58+
exports.save = saveBundleAndMap;
59+
exports.formatName = 'bundle';

local-cli/bundle/output/prepack.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
const writeFile = require('./writeFile');
12+
13+
function buildPrepackBundle(packagerClient, requestOptions) {
14+
return packagerClient.buildPrepackBundle(requestOptions);
15+
}
16+
17+
function savePrepackBundle(bundle, options, log) {
18+
const {
19+
'bundle-output': bundleOutput,
20+
'bridge-config': bridgeConfig,
21+
} = options;
22+
23+
const result = bundle.build({
24+
batchedBridgeConfig: bridgeConfig
25+
});
26+
27+
log('Writing prepack bundle output to:', bundleOutput);
28+
const writePrepackBundle = writeFile(bundleOutput, result, 'ucs-2');
29+
writePrepackBundle.then(() => log('Done writing prepack bundle output'));
30+
return writePrepackBundle;
31+
}
32+
33+
exports.build = buildPrepackBundle;
34+
exports.save = savePrepackBundle;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
const fs = require('fs');
12+
const Promise = require('promise');
13+
14+
function writeFile(file, data, encoding) {
15+
return new Promise((resolve, reject) => {
16+
fs.writeFile(
17+
file,
18+
data,
19+
encoding,
20+
error => error ? reject(error) : resolve()
21+
);
22+
});
23+
}
24+
25+
module.exports = writeFile;

0 commit comments

Comments
 (0)