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