UNPKG

2.7 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 *
18 */
19
20'use strict';
21
22require('./setupBabel');
23
24var fs = require('fs');
25var path = require('path');
26var commander = require('commander');
27var Util = require('./utils');
28var Parser = require('./parser');
29var bundle = require('./bundler');
30
31function main() {
32 commander.description('React Native Bundle Spliter').option('--output <path>', 'Path to store bundle.', 'build').option('--config <path>', 'Config file for react-native-split.').option('--platform', 'Specify bundle platform. ', 'android').option('--dev [boolean]', 'Generate dev module.').parse(process.argv);
33
34 if (!commander.config) {
35 throw new Error('You must enter an config file (by --config).');
36 }
37
38 function isFileExists(fname) {
39 try {
40 fs.accessSync(fname, fs.F_OK);
41 return true;
42 } catch (e) {
43 return false;
44 }
45 }
46
47 var configFile = path.resolve(process.cwd(), commander.config);
48 var outputDir = path.resolve(process.cwd(), commander.output);
49
50 if (!isFileExists(configFile)) {
51 console.log('Config file ' + configFile + ' is not exists!');
52 process.exit(-1);
53 }
54
55 var rawConfig = JSON.parse(fs.readFileSync(configFile, 'utf-8'));
56 var workRoot = path.dirname(configFile);
57 var outputRoot = path.join(outputDir, 'bundle-output');
58 Util.ensureFolder(outputRoot);
59
60 var config = {
61 root: workRoot,
62 dev: commander.dev === 'true',
63 packageName: rawConfig['package'],
64 platform: commander.platform,
65 outputDir: path.join(outputRoot, 'split'),
66 bundleDir: path.join(outputRoot, 'bundle'),
67 baseEntry: {
68 index: rawConfig.base.index,
69 includes: rawConfig.base.includes
70 },
71 customEntries: rawConfig.custom
72 };
73 if (!isFileExists(config.baseEntry.index)) {
74 console.log('Index of base does not exists!');
75 }
76
77 console.log('Work on root: ' + config.root);
78 console.log('Dev mode: ' + config.dev);
79 bundle(config, function (err, data) {
80 if (err) throw err;
81 console.log('===[Bundle] Finish!===');
82 var parser = new Parser(data, config);
83 parser.splitBundle();
84 });
85}
86
87exports.default = main;
\No newline at end of file