-
Notifications
You must be signed in to change notification settings - Fork 1
/
openeddi-build.js
170 lines (146 loc) · 4.55 KB
/
openeddi-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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Created by jfagan on 3/8/15.
* This file builds the application.
* openeddi-build.js
*/
var fs = require('fs');
var Vulcanize = require('vulcanize');
var log = require('util').log;
/**
* Create the Browserify bundle
* @param callback
*/
var browserifyOpenEddi = function (callback) {
var browserify = require('browserify');
// Every key js file on the client branches from oe.js
var b = browserify('./oe/oe.js');
// jstify will allow browserify to use external template files
log('[openeddi-build] Compiling templates into the bundle');
b.transform('jstify', {engine: 'lodash', templateOpts: {variable: 'rc'}});
// export the file here
var builtFile = fs.createWriteStream('./oe/built.js');
// and bundle and export
log('[openeddi-build] Bundling all javascript files into build.js');
b.bundle().pipe(builtFile);
var cleanup = function () {
builtFile.removeListener('finish', finish);
builtFile.removeListener('error', error);
};
var finish = function () {
cleanup();
callback(null);
};
var error = function (err) {
cleanup();
callback(err);
};
builtFile.addListener('finish', finish);
builtFile.addListener('error', error);
};
/**
* Render the index.ejs file, if we are using vulcanize
* @param callback
*/
var renderOEEJS = function (callback) {
var ejs = require('ejs');
// function to write the file
var writeRenderedEJS = function (err, str) {
if (err) {
return console.log(err);
} else {
fs.writeFile("./oe/compiled-app.html", str, function (err) {
if (err) {
callback(err);
} else {
callback()
}
});
}
};
// render the file
log('[openeddi-build] Rendering EJS');
ejs.renderFile(appRoot + '/oe/index.ejs', {
csrfToken: '',
OEConfig: OEConfig,
oe: {test: 'the test'},
oeModules: oeModules
}, null, writeRenderedEJS);
};
/**
* Vulcanize the html. This process takes all the html imports, along with all the CSS and JS, and inserts them into a single html file.
* @param callback
* @returns {void|*}
*/
var vulcanizeOpenEddi = function (callback) {
var vulcanExcludes = ["/bower_components/iron-flex-layout/iron-flex-layout-classes.html",
"/bower_components/iron-flex-layout/iron-flex-layout.html"];
// Excluding these things lets chrome work, but then firefox doesn't work
// if they are not excluded firefox works, but the classes don't register for Chrome
var vulcan = new Vulcanize({
abspath: appRoot + '/oe/',
excludes: [],
stripExcludes: [],
inlineScripts: true,
inlineCss: true,
addedImports: [],
redirects: [],
implicitStrip: true,
stripComments: false,
inputUrl: ''
});
log('[openeddi-build] Vulcanizing application');
vulcan.process('./compiled-app.html', function (err, inlinedHtml) {
if (err) {
callback(err);
}
fs.writeFile("./oe/vulcanized-app.html", inlinedHtml, function (err) {
if (err) {
return log(err);
}
log('[openeddi-build] Vulcanized to file');
callback(null);
});
});
};
/**
* Uglify the bundled javascript. Minify it so that it's a smaller file size (and in some cases, runs faster)
* @param callback
*/
var uglifyBuiltJS = function (callback) {
log('[openeddi-build] Minifying built.js');
var uglify = require('uglify-js');
var minBuilt = uglify.minify(appRoot + '/oe/built.js');
fs.writeFile("./oe/built-min.js", minBuilt.code, function (err) {
if (err) {
callback(err);
} else {
callback(null)
}
});
};
/**
* The access point for this file.
* @param callback
*/
var buildAppFile = function (callback) {
var async = require('async');
var appBuildQueue = [browserifyOpenEddi];
if (OEConfig.build.minify) {
appBuildQueue.push(uglifyBuiltJS);
}
if (OEConfig.build.vulcanize) {
appBuildQueue.push(renderOEEJS);
appBuildQueue.push(vulcanizeOpenEddi);
}
async.series(appBuildQueue, function (err) {
if (err) {
log('[openeddi-build] Error building the application file');
log(err);
callback(err);
} else {
log('[openeddi-build] Finished building app file');
callback();
}
});
};
module.exports = buildAppFile;