1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 | require('./src/setupBabel');
|
6 |
|
7 | const fs = require('fs');
|
8 | const path = require('path');
|
9 | const commander = require('commander');
|
10 | const Util = require('./src/utils');
|
11 | const Parser = require('./src/parser');
|
12 | const bundle = require('./src/bundler');
|
13 |
|
14 | commander
|
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 |
|
22 | if (!commander.config) {
|
23 | throw new Error('You must enter an config file (by --config).');
|
24 | }
|
25 |
|
26 | function isFileExists(fname) {
|
27 | try {
|
28 | fs.accessSync(fname, fs.F_OK);
|
29 | return true;
|
30 | } catch (e) {
|
31 | return false;
|
32 | }
|
33 | }
|
34 |
|
35 | const configFile = path.resolve(process.cwd(), commander.config);
|
36 | const outputDir = path.resolve(process.cwd(), commander.output);
|
37 |
|
38 | if (!isFileExists(configFile)) {
|
39 | console.log('Config file ' + configFile + ' is not exists!');
|
40 | process.exit(-1);
|
41 | }
|
42 |
|
43 | const rawConfig = JSON.parse(fs.readFileSync(configFile, 'utf-8'));
|
44 | const workRoot = path.dirname(configFile);
|
45 | const outputRoot = path.join(outputDir, `bundle-output`);
|
46 | Util.ensureFolder(outputRoot);
|
47 |
|
48 | const 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 | };
|
61 | if (!isFileExists(config.baseEntry.index)) {
|
62 | console.log('Index of base does not exists!');
|
63 | }
|
64 |
|
65 | console.log('Work on root: ' + config.root);
|
66 | console.log('Dev mode: ' + config.dev);
|
67 | bundle(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 |
|