-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrocfile.js
269 lines (235 loc) · 6.01 KB
/
Brocfile.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// dependencies
var assetRev = require("broccoli-asset-rev");
var chalk = require("chalk");
var cleanCSS = require("broccoli-clean-css");
var concatCSS = require("broccoli-concat");
var env = require("broccoli-env").getEnv();
var exportTree = require("broccoli-export-tree");
var filterReact = require("broccoli-react");
var jscs = require("broccoli-jscs");
var jsHintTree = require("broccoli-jshint");
var less = require("broccoli-less");
var mergeTrees = require("broccoli-merge-trees");
var pickFiles = require("broccoli-static-compiler");
var replace = require("broccoli-replace");
var uglifyJavaScript = require("broccoli-uglify-js");
var webpackify = require("broccoli-webpack");
var dirs = {
distDir: "target",
data: "data",
dataDist: "app/data",
src: "src",
styles: "styles",
stylesDist: "app",
img: "img",
imgDist: "app/img"
};
var fileNames = {
mainJs: "main", // without extension
mainStyles: "main" // without extension
};
/*
* Task definitions
*/
var tasks = {
jsHint: function (jsTree) {
// run jscs on compiled js
var jscsTree = jscs(jsTree, {
logError: function (message) {
switch (env) {
case "production":
console.log(message + "\n");
// fail build in production
throw new Error("jscs failed, see messages above");
default:
// use pretty colors in test and development mode
console.log(chalk.red(message) + "\n");
break;
}
},
disableTestGenerator: false,
enabled: true
});
// run jshint on compiled js
var hintTree = jsHintTree(jsTree , {
logError: function (message) {
switch (env) {
case "production":
this._errors.push(message + "\n");
// fail build in production
throw new Error("jshint failed, see messages above");
default:
// use pretty colors in test and development mode
this._errors.push(chalk.red(message) + "\n");
break;
}
}
});
// hack to strip test files from jshint tree
hintTree = pickFiles(hintTree, {
srcDir: "/",
files: []
});
return mergeTrees(
[jscsTree, hintTree, jsTree],
{ overwrite: true }
);
},
webpack: function (masterTree) {
// transform merge module dependencies into one file
return webpackify(masterTree, {
entry: "./" + fileNames.mainJs + ".js",
output: {
filename: "app/application.js"
}
});
},
minifyJs: function (masterTree) {
return uglifyJavaScript(masterTree, {
mangle: true,
compress: true
});
},
css: function (masterTree) {
// create tree for less
var cssTree = pickFiles(dirs.styles, {
srcDir: "/",
files: ["**/" + fileNames.mainStyles + ".less", "**/*.css"],
destDir: dirs.stylesDist
});
// compile less to css
cssTree = less(cssTree, {});
// concatenate css
cssTree = concatCSS(cssTree, {
inputFiles: ["**/*.css"],
outputFile: "/" + dirs.stylesDist + "/application.css",
});
return mergeTrees(
[masterTree, cssTree],
{ overwrite: true }
);
},
minifyCSS: function (masterTree) {
return cleanCSS(masterTree);
},
index: function (masterTree) {
// create tree for index
var indexTree = pickFiles(dirs.src, {
srcDir: "/",
destDir: "",
files: ["*.html"],
});
return mergeTrees(
[masterTree, indexTree],
{ overwrite: true }
);
},
img: function (masterTree) {
// create tree for image files
var imgTree = pickFiles(dirs.img, {
srcDir: "/",
destDir: dirs.imgDist,
});
return mergeTrees(
[masterTree, imgTree],
{ overwrite: true }
);
},
data: function (masterTree) {
// create tree for data files
var dataTree = pickFiles(dirs.data, {
srcDir: "/",
destDir: dirs.dataDist,
});
return mergeTrees(
[masterTree, dataTree],
{ overwrite: true }
);
},
md5: function (masterTree) {
// add md5 checksums to filenames
return assetRev(masterTree, {
extensions: ["js", "css", "png", "jpg", "gif"],
replaceExtensions: ["html", "js", "css"]
});
},
export: function (masterTree) {
return exportTree(masterTree, {
destDir: dirs.distDir,
clobber: true
});
}
};
/*
* basic pre-processing before actual build
*/
function createJsTree() {
// create tree for .js and .jsx
var jsTree = pickFiles(dirs.src, {
srcDir: "/",
destDir: "",
files: [
"**/*.jsx",
"**/*.js"
]
});
// compile react files
jsTree = filterReact(jsTree, { extensions: ["jsx"] });
// replace @@ENV in js code with current BROCCOLI_ENV environment variable
// { default: "development" | "production" }
return replace(jsTree, {
files: ["**/*.js"],
patterns: [
{
match: "ENV", // replaces @@ENV
replacement: env
}
]
});
}
function compose() {
var args = arguments;
var start = args.length - 1;
return function () {
var i = start;
var result = args[start].apply(this, arguments);
while (i--) {
result = args[i].call(this, result);
}
return result;
};
}
/*
* Start the build
*/
var buildTree = compose(tasks.jsHint, createJsTree);
// export BROCCOLI_ENV={ default: "development" | "production" }
if (env === "development" || env === "production" ) {
// add steps used in both development and production
buildTree = compose(
tasks.img,
tasks.index,
tasks.data,
tasks.css,
tasks.webpack,
buildTree
);
}
if (env === "production") {
// add steps that are exclusively used in production
buildTree = compose(
tasks.md5,
tasks.minifyCSS,
tasks.minifyJs,
buildTree
);
}
// add the following lines to export to target folder,
// say to be served by another system.
// This will make `broccoli serve` and `npm run serve` act as
// `broccoli build target --watch`
// buildTree = compose(
// tasks.export,
// buildTree
// );
module.exports = buildTree();