-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
69 lines (60 loc) · 1.7 KB
/
bundle.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
/**
* Module dependencies
*/
var fs = require('fs')
, path = require('path')
, build = require('component-builder')
, resolve = require('component-resolver')
, bundler = require('component-bundler')
, jade = require('jade')
var opts = {root: __dirname, build: path.join(__dirname, 'out/component')};
var fwrite = fs.writeFileSync;
var json = require(path.join(opts.root, 'component.json'));
var bundle = bundler.pages(json);
/**
* Resolve and build
*/
resolve(opts.root, {install: true}, function (err, tree) {
var bundles = null;
if (err) { throw err; }
// create
bundles = bundle(tree);
// build
Object.keys(bundles).forEach(function (name) {
build.styles(bundles[name])
.use('styles', build.plugins.css())
.end(function (err, string) {
if (err) throw err;
if (string) {
fs.writeFileSync(path.join(opts.build, name +'.css'), string);
}
});
build.scripts(bundles[name])
.use('scripts', build.plugins.js())
.use('templates', function (file, next) {
file.read(function (err, buf) {
if (err) { return next(err); }
switch (file.extension) {
case 'jade':
buf = jade.render(String(buf));
break;
}
file.define = true;
file.string = JSON.stringify(buf);
next();
});
})
.build(function (err, js) {
var file = null;
if (err) { throw err; }
else if (!js) { return; }
// require code
if (name === json.locals[0] && js) {
//js = build.scripts.require + js;
js = build.scripts.umd('./component/boot', 'spin', js);
}
file = path.join(opts.build, name + '.js');
fwrite(file, js, 'utf8');
});
});
})