UNPKG

2.02 kBJavaScriptView Raw
1/**
2 * Created by desmond on 4/13/17.
3 */
4'use strict';
5require('./src/setupBabel');
6
7const fs = require('fs');
8const path = require('path');
9const commander = require('commander');
10const Util = require('./src/utils');
11const Parser = require('./src/parser');
12const bundle = require('./src/bundler');
13
14commander
15 .description('React Native Bundle Spliter')
16 .option('--output <path>', 'Path to store bundle.', 'build')
17 .option('--config <path>', 'Config file for react-native-split.')
18 .option('--platform', 'Specify bundle platform. ', 'android')
19 .option('--dev [boolean]', 'Generate dev module.')
20 .parse(process.argv);
21
22if (!commander.config) {
23 throw new Error('You must enter an config file (by --config).');
24}
25
26function isFileExists(fname) {
27 try {
28 fs.accessSync(fname, fs.F_OK);
29 return true;
30 } catch (e) {
31 return false;
32 }
33}
34
35const configFile = path.resolve(process.cwd(), commander.config);
36const outputDir = path.resolve(process.cwd(), commander.output);
37
38if (!isFileExists(configFile)) {
39 console.log('Config file ' + configFile + ' is not exists!');
40 process.exit(-1);
41}
42
43const rawConfig = JSON.parse(fs.readFileSync(configFile, 'utf-8'));
44const workRoot = path.dirname(configFile);
45const outputRoot = path.join(outputDir, `bundle-output`);
46Util.ensureFolder(outputRoot);
47
48const config = {
49 root: workRoot,
50 dev: commander.dev === 'true',
51 packageName : rawConfig['package'],
52 platform : commander.platform,
53 outputDir : path.join(outputRoot, 'split'),
54 bundleDir : path.join(outputRoot, 'bundle'),
55 baseEntry : {
56 index: rawConfig.base.index,
57 includes: rawConfig.base.includes
58 },
59 customEntries : rawConfig.custom
60};
61if (!isFileExists(config.baseEntry.index)) {
62 console.log('Index of base does not exists!');
63}
64
65console.log('Work on root: ' + config.root);
66console.log('Dev mode: ' + config.dev);
67bundle(config, (err, data) => {
68 if (err) throw err;
69 console.log('===[Bundle] Finish!===');
70 const parser = new Parser(data, config);
71 parser.splitBundle();
72});
73
74