1 | const browserify = require('browserify');
|
2 | const fse = require('fs-extra');
|
3 | const path = require('path');
|
4 |
|
5 | const utils = require('./lib/utils.js');
|
6 | const renderer = require('./lib/renderer.js');
|
7 | const transform = require('./lib/transforms');
|
8 | const getOpts = require('./lib/getopts.js');
|
9 | const log = require('./lib/utils/log.js');
|
10 | const Less = require('./lib/utils/less.js');
|
11 |
|
12 |
|
13 | let Libs = {
|
14 | 'react-dom' : '',
|
15 | 'react' : ''
|
16 | };
|
17 | const bundleEntryPoint = async (entryPoint, Opts)=>{
|
18 | let opts = Object.assign({
|
19 | entry : {
|
20 | name : path.basename(entryPoint).split('.')[0],
|
21 | dir : path.dirname(entryPoint)
|
22 | }
|
23 | }, Opts);
|
24 | const endLog = log.buildEntryPoint(opts.entry, opts.prod);
|
25 |
|
26 |
|
27 | const paths = utils.paths(opts.paths, opts.entry.name);
|
28 | const bundler = browserify({
|
29 | standalone : opts.entry.name,
|
30 | paths : opts.shared,
|
31 | ignoreMissing : true,
|
32 | postFilter : (id, filepath, pkg)=>{
|
33 | if(utils.shouldBundle(filepath, id, opts)) return true;
|
34 | Libs[id] = filepath;
|
35 | return false;
|
36 | }
|
37 | })
|
38 | .require(entryPoint)
|
39 | .transform((file)=>transform(file, opts), {global: true});
|
40 |
|
41 | if(opts.prod) bundler.transform('uglifyify', {global : true});
|
42 |
|
43 | await fse.ensureDir(`${opts.paths.build}/${opts.entry.name}`);
|
44 | await utils.bundle(bundler)
|
45 | .then((code)=>fse.writeFile(paths.code, code))
|
46 | .catch((err)=>{
|
47 | console.log('BUNDLE ERR', err);
|
48 | })
|
49 | await Less.compile(opts).then((css)=>fse.writeFile(paths.style, css));
|
50 | await renderer(opts);
|
51 |
|
52 | endLog();
|
53 | };
|
54 |
|
55 |
|
56 | const bundleLibs = async (opts)=>{
|
57 | const logEnd = log.libs(Libs, opts.prod);
|
58 | const libBundler = browserify().require(Object.keys(Libs));
|
59 | if(opts.prod){
|
60 | libBundler.transform('uglifyify', {global : true});
|
61 | }
|
62 | return utils.bundle(libBundler)
|
63 | .then((code)=>fse.writeFile(`${opts.paths.build}/${opts.paths.libs}`, code))
|
64 | .then(logEnd);
|
65 | };
|
66 |
|
67 | module.exports = async (entryPoints, opts)=>{
|
68 | opts = getOpts(opts, entryPoints);
|
69 | log.beginBuild(opts);
|
70 |
|
71 | await fse.emptyDir(opts.paths.build);
|
72 | await opts.targets.reduce((flow, ep)=>flow.then(()=>bundleEntryPoint(ep, opts)), Promise.resolve());
|
73 | await bundleLibs(opts);
|
74 | }; |
\ | No newline at end of file |