-
Notifications
You must be signed in to change notification settings - Fork 203
/
gruntfile.js
343 lines (312 loc) · 9.4 KB
/
gruntfile.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
module.exports = function(grunt) {
"use strict";
var fs = require("fs");
var vm = require("vm");
var _includeInThisScope = function (path) {
var code = fs.readFileSync(path);
vm.runInThisContext(code, path);
}.bind(this);
var packageFile = grunt.file.readJSON("package.json");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-template");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-contrib-requirejs");
grunt.loadNpmTasks("grunt-md5");
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks("grunt-contrib-cssmin");
grunt.loadNpmTasks("grunt-contrib-handlebars");
grunt.loadNpmTasks('grunt-contrib-jshint');
_includeInThisScope("website/grunt-templates/file-paths.js");
_includeInThisScope("website/grunt-templates/env-specific-constants.js");
var _createD3PieFiles = function() {
var fs = require("fs");
config.template.d3pieBundle.options.data = {
_defaultSettings: fs.readFileSync("d3pie-source/_default-settings.js", 'utf8'),
_helpers: fs.readFileSync("d3pie-source/_helpers.js", 'utf8'),
_labels: fs.readFileSync("d3pie-source/_labels.js", 'utf8'),
_math: fs.readFileSync("d3pie-source/_math.js", 'utf8'),
_segments: fs.readFileSync("d3pie-source/_segments.js", 'utf8'),
_text: fs.readFileSync("d3pie-source/_text.js", 'utf8'),
_tooltips: fs.readFileSync("d3pie-source/_tooltips.js", 'utf8'),
_validate: fs.readFileSync("d3pie-source/_validate.js", 'utf8')
};
grunt.task.run("template:d3pieBundle");
grunt.task.run("uglify:d3pie");
};
/**
* The first step of the build process. This sets various settings in the main grunt config for the current build environment.
* These govern how the subsequent tasks behave.
*/
var setEnv_Dev = function() {
config.template.indexFile.options.data.C = _CONSTANTS.DEV;
config.template.indexFile.options.data.D3PIE_VERSION = packageFile.version;
config.template.devRequireConfig.options.data.handlebarsLib = _CONSTANTS.DEV.HANDLEBARS_LIB;
config.template.devRequireConfig.options.data.baseUrl = _CONSTANTS.DEV.BASE_URL;
var lines = [];
for (var i in _requireJSModulePaths) {
var file = _requireJSModulePaths[i].replace(/\.js$/, "");
lines.push('\t\t"' + i + '": "' + file + '"');
}
config.template.devRequireConfig.options.data.moduleStr = lines.join(",\n");
config.template.constants.options.data.VERSION = packageFile.version;
config.template.constants.options.data.MINIMIZED = _CONSTANTS.DEV.MINIMIZED;
config.template.constants.options.data.DEBUG = _CONSTANTS.DEV.DEBUG;
};
var setEnv_Prod = function() {
var info = _getHandlebarsFileMap();
config.template.indexFile.options.data.C = _CONSTANTS.PROD;
config.template.indexFile.options.data.D3PIE_VERSION = packageFile.version;
config.template.prodRequireConfig.options.data.handlebarsLib = _CONSTANTS.PROD.HANDLEBARS_LIB;
config.template.prodRequireConfig.options.data.baseUrl = _CONSTANTS.PROD.BASE_URL;
config.template.prodRequireConfig.options.data.moduleStr = info.allModules;
config.template.constants.options.data.VERSION = packageFile.version;
config.template.constants.options.data.MINIMIZED = _CONSTANTS.PROD.MINIMIZED;
config.template.constants.options.data.DEBUG = _CONSTANTS.PROD.DEBUG;
config.handlebars.compile.files = info.handlebarsMap;
};
var _getHandlebarsFileMap = function() {
var obj = {};
var allModules = [];
for (var i in _requireJSModulePaths) {
var path = _requireJSModulePaths[i];
if (/\.hbs$/.test(_requireJSModulePaths[i])) {
var parts = _requireJSModulePaths[i].split("/");
parts[parts.length - 1] = "hbs-" + parts[parts.length - 1].replace(/\.hbs$/, ".js");
var hbsFileNameAndPath = parts.join("/");
obj["build/" + hbsFileNameAndPath] = "website/" + path;
var requireJSModuleName1 = "build/" + hbsFileNameAndPath.replace(/\.js$/, "");
allModules.push('"' + i + '": "' + requireJSModuleName1 + '"');
} else {
var requireJSModuleName2 = "build/" + _requireJSModulePaths[i].replace(/\.js$/, "");
allModules.push('"' + i + '": "' + requireJSModuleName2 + '"');
}
}
return {
handlebarsMap: obj,
allModules: allModules.join(",\n")
}
};
var config = {
clean: ["build"],
template: {
d3pieBundle: {
options: { data: {} },
files: {
"d3pie/d3pie.js": ["d3pie-source/d3pie-source.js"]
}
},
indexFile: {
options: {
data: {
C: null,
VERSION: "",
SITE_CSS: "",
CORE_JS: "",
APP_START_FILE: ""
}
},
files: {
"index.html": ["website/grunt-templates/template-index.html"]
}
},
devRequireConfig: {
options: {
data: { }
},
files: {
"website/core/require.config.js": ["website/grunt-templates/template-require.config.js"]
}
},
prodRequireConfig: {
options: {
data: { }
},
files: {
"build/core/require.config.js": ["website/grunt-templates/template-require.config.js"]
}
},
constants: {
options: { data: {} },
files: {
"website/core/constants.js": ["website/grunt-templates/template-constants.js"]
}
}
},
watch: {
scripts: {
files: "d3pie-source/*.js",
tasks: ["createD3PieFiles"]
}
},
uglify: {
d3pie: {
files: {
"d3pie/d3pie.min.js": "d3pie/d3pie.js"
},
options: {
banner: "/*!\n" +
"* d3pie\n" +
"* @author Ben Keen\n" +
"* @version " + packageFile.version + "\n" +
"* @date March 11, 2017\n" +
"* @repo http://github.com/benkeen/d3pie\n" +
"*/\n"
}
},
coreJS: {
files: {
"build/core-libs.min.js": [
"website/libs/jquery.js",
"website/libs/jquery-ui.js",
"website/libs/jquery.slides.js",
"d3pie-source/_default-settings.js",
"website/libs/modernizr.js",
"d3pie/d3pie.js",
"website/libs/bootstrap.min.js",
"website/libs/bootstrap-colorpicker.js",
"website/libs/three.js",
"website/libs/prettify.js",
"website/libs/require.js"
]
},
options: {
compress: {},
mangle: {
except: ["jQuery", "Backbone"]
}
}
},
appStart: {
files: {
"build/core/appStart.js": ["build/core/appStart.js"]
},
options: {
compress: {}
}
}
},
handlebars: {
compile: {
options: {
amd: true,
namespace: false
},
files: {}
}
},
cssmin: {
target: {
files: {
'build/css/core.css': [
'website/css/site.css',
'website/css/bootstrap.css',
'website/css/bootstrap-colorpicker.css',
'website/css/prettify.css',
'website/css/slides.css'
]
}
}
},
requirejs: {
compile: {
options: {
name: "build/core/appStart.js",
out: "build/core/appStartBuild.js",
baseUrl: "./",
mainConfigFile: "build/core/require.config.js"
}
}
},
md5: {
coreCSS: {
files: {
"build/css/core.css": "build/css/core.css"
},
options: {
encoding: null,
keepBasename: true,
keepExtension: true,
after: function(fileChanges, options) {
// store the new CSS filename in the index file template script
config.template.indexFile.options.data.SITE_CSS = fileChanges[0].newPath;
}
}
},
coreJS: {
files: {
"build/core-libs.min.js": "build/core-libs.min.js"
},
options: {
encoding: null,
keepBasename: true,
keepExtension: true,
after: function(fileChanges, options) {
config.template.indexFile.options.data.CORE_JS = fileChanges[0].newPath;
}
}
},
appStart: {
files: {
"build/core/appStartBuild.js": "build/core/appStartBuild.js"
},
options: {
encoding: null,
keepBasename: true,
keepExtension: true,
after: function(fileChanges, options) {
config.template.indexFile.options.data.APP_START_FILE = fileChanges[0].newPath;
}
}
}
},
copy: {
core: {
expand: true,
cwd: "website",
src: ['**'],
dest: 'build/'
}
},
jshint: {
all: [
'd3pie-source/*.js'
],
options: {
reporterOutput: ""
}
}
};
grunt.initConfig(config);
grunt.registerTask("createD3PieFiles", _createD3PieFiles);
grunt.registerTask("setEnv_Dev", setEnv_Dev);
grunt.registerTask("setEnv_Prod", setEnv_Prod);
// the default task is to regenerate the d3pie.js and d3pie.min.js files based on the latest files in d3pie-source/
grunt.registerTask("default", ["jshint","createD3PieFiles", "uglify:d3pie"]);
// tasks for building the website. There are only 2 options: dev and prod
grunt.registerTask("dev", [
"jshint",
"setEnv_Dev",
"template:indexFile",
"template:devRequireConfig",
"template:constants"
]);
grunt.registerTask("prod", [
"jshint",
"setEnv_Prod", // set the build environment constants
"clean", // wipe out the build folder
"template:constants", // create the constants file
"cssmin", // bundle the CSS into a single file
"md5:coreCSS", // rename the file to include its file hash
"uglify:coreJS", // bundle the core JS
"md5:coreJS", // rename the JS lib file
// now copy EVERYTHING in the /website folder over to /build. We're about to do terrible things to it
"copy:core",
"handlebars",
"template:prodRequireConfig",
"uglify:appStart",
"requirejs", // run the requireJS task to bundle up everything into a single file
"md5:appStart",
"template:indexFile" // alright! Now re-generate the index file
]);
};