UNPKG

2.77 kBJavaScriptView Raw
1/**
2 * Copyright 2015-present Desmond Yao
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Created by desmond on 4/16/17.
17 * @flow
18 */
19
20
21'use strict';
22require('./setupBabel');
23
24const fs = require('fs');
25const path = require('path');
26const commander = require('commander');
27const Util = require('./utils');
28const Parser = require('./parser');
29const bundle = require('./bundler');
30
31function main() {
32 commander
33 .description('React Native Bundle Spliter')
34 .option('--output <path>', 'Path to store bundle.', 'build')
35 .option('--config <path>', 'Config file for react-native-split.')
36 .option('--platform', 'Specify bundle platform. ', 'android')
37 .option('--dev [boolean]', 'Generate dev module.')
38 .parse(process.argv);
39
40 if (!commander.config) {
41 throw new Error('You must enter an config file (by --config).');
42 }
43
44 function isFileExists(fname) {
45 try {
46 fs.accessSync(fname, fs.F_OK);
47 return true;
48 } catch (e) {
49 return false;
50 }
51 }
52
53 const configFile = path.resolve(process.cwd(), commander.config);
54 const outputDir = path.resolve(process.cwd(), commander.output);
55
56 if (!isFileExists(configFile)) {
57 console.log('Config file ' + configFile + ' is not exists!');
58 process.exit(-1);
59 }
60
61 const rawConfig = JSON.parse(fs.readFileSync(configFile, 'utf-8'));
62 const workRoot = path.dirname(configFile);
63 const outputRoot = path.join(outputDir, `bundle-output`);
64 Util.ensureFolder(outputRoot);
65
66 const config = {
67 root: workRoot,
68 dev: commander.dev === 'true',
69 packageName : rawConfig['package'],
70 platform : commander.platform,
71 outputDir : path.join(outputRoot, 'split'),
72 bundleDir : path.join(outputRoot, 'bundle'),
73 baseEntry : {
74 index: rawConfig.base.index,
75 includes: rawConfig.base.includes
76 },
77 customEntries : rawConfig.custom
78 };
79 if (!isFileExists(config.baseEntry.index)) {
80 console.log('Index of base does not exists!');
81 }
82
83 console.log('Work on root: ' + config.root);
84 console.log('Dev mode: ' + config.dev);
85 bundle(config, (err, data) => {
86 if (err) throw err;
87 console.log('===[Bundle] Finish!===');
88 const parser = new Parser(data, config);
89 parser.splitBundle();
90 });
91}
92
93exports.default = main;
\No newline at end of file