forked from gjb2048/moodle-theme_essential
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
478 lines (463 loc) · 17.1 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/**
* Gruntfile for compiling theme_essential .less files.
*
* This file configures tasks to be run by Grunt
* http://gruntjs.com/ for the current theme.
*
*
* Requirements:
* -------------
* nodejs, npm, grunt-cli.
*
* Installation:
* -------------
* node and npm: instructions at http://nodejs.org/
*
* grunt-cli: `[sudo] npm install -g grunt-cli`
*
* node dependencies: run `npm install` in the root directory.
*
*
* Usage:
* ------
* Call tasks from the theme root directory. Default behaviour
* (calling only `grunt`) is to run the watch task detailed below.
*
*
* Porcelain tasks:
* ----------------
* The nice user interface intended for everyday use. Provide a
* high level of automation and convenience for specific use-cases.
*
* grunt watch Watch the less directory (and all subdirectories)
* for changes to *.less files then on detection
* run 'grunt compile'
*
* Options:
*
* --dirroot=<path> Optional. Explicitly define the
* path to your Moodle root directory
* when your theme is not in the
* standard location.
* grunt compile Run the .less files through the compiler, create the
* RTL version of the output, then run decache so that
* the results can be seen on the next page load.
*
* Options:
*
* --dirroot=<path> Optional. Explicitly define the
* path to your Moodle root directory
* when your theme is not in the
* standard location.
*
* --build=<type> Optional. 'p'(default) or 'd'. If 'p'
* then 'production' CSS files. If 'd'
* then 'development' CSS files unminified
* and with source map to less files.
*
* --urlprefix=<path> Optional. Explicitly define
* the path between the domain
* and the installation in the
* URL, i.e. /moodle27 being:
* --urlprefix=/moodle27
*
* grunt amd Create the Asynchronous Module Definition JavaScript files. See: MDL-49046.
* Done here as core Gruntfile.js currently *nix only.
*
* grunt svg Change the colour of the SVGs in pix_core by
* text replacing #999 with a new hex colour.
* Note this requires the SVGs to be #999 to
* start with or the replace will do nothing
* so should usually be preceded by copying
* a fresh set of the original SVGs.
*
* Options:
*
* --svgcolour=<hexcolour> Hex colour to use for SVGs
*
* Plumbing tasks & targets:
* -------------------------
* Lower level tasks encapsulating a specific piece of functionality
* but usually only useful when called in combination with another.
*
* grunt less Compile all less files.
*
* grunt decache Clears the Moodle theme cache.
*
* Options:
*
* --dirroot=<path> Optional. Explicitly define
* the path to your Moodle root
* directory when your theme is
* not in the standard location.
*
* grunt replace Run all text replace tasks.
*
* grunt cssflip Create essential-rtl.css by flipping the direction styles
* in essential.css. Ref: https://www.npmjs.org/package/css-flip
*
*
* @package theme
* @subpackage essential
* @author G J Barnard - gjbarnard at gmail dot com and {@link http://moodle.org/user/profile.php?id=442195}
* @author Based on code originally written by Joby Harding, Bas Brands, David Scotson and many other contributors.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
module.exports = function(grunt) { // jshint ignore:line
// Import modules.
var path = require('path'); // jshint ignore:line
// Theme Bootstrap constants.
var MOODLEURLPREFIX = grunt.option('urlprefix') || '',
THEMEDIR = path.basename(path.resolve('.'));
// PHP strings for exec task.
var moodleroot = path.dirname(path.dirname(__dirname)), // jshint ignore:line
dirrootopt = grunt.option('dirroot') || process.env.MOODLE_DIR || ''; // jshint ignore:line
// Allow user to explicitly define Moodle root dir.
if ('' !== dirrootopt) {
moodleroot = path.resolve(dirrootopt);
}
// Production / development.
var build = grunt.option('build') || 'd'; // Development for 'watch' task.
if ((build != 'p') && (build != 'd')) {
build = 'p';
console.log('-build switch only accepts \'p\' for production or \'d\' for development,');
console.log('e.g. -build=p or -build=d. Defaulting to development.');
}
var PWD = process.cwd(); // jshint ignore:line
var decachephp = '../../admin/cli/purge_caches.php';
var svgcolour = grunt.option('svgcolour') || '#999';
grunt.initConfig({
less: {
essential_p: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: false,
},
src: 'less/essential.less',
dest: 'style/essential.css'
},
editor_p: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: false,
},
src: 'less/editor.less',
dest: 'style/editor.css'
},
bootstrap_pix_p: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: false,
},
src: 'less/bootstrap-pix.less',
dest: 'style/bootstrap-pix.css'
},
fontawesome_p: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: false,
},
src: 'less/fontawesome.less',
dest: 'style/fontawesome.css'
},
scrollbars_p: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: false,
},
src: 'less/essential-scrollbars.less',
dest: 'style/essential-scrollbars.css'
},
settings_p: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: false,
},
src: 'less/essential-settings.less',
dest: 'style/essential-settings.css'
},
alternative_p: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: false,
},
src: 'less/essential-alternative.less',
dest: 'style/essential-alternative.css'
},
essential_d: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: true,
sourceMapRootpath: MOODLEURLPREFIX + '/theme/' + THEMEDIR,
sourceMapFilename: 'style/essential.treasure.map'
},
src: 'less/essential.less',
dest: 'style/essential.css'
},
editor_d: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: true,
sourceMapRootpath: MOODLEURLPREFIX + '/theme/' + THEMEDIR,
sourceMapFilename: 'style/editor.treasure.map'
},
src: 'less/editor.less',
dest: 'style/editor.css'
},
bootstrap_pix_d: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: true,
sourceMapRootpath: MOODLEURLPREFIX + '/theme/' + THEMEDIR,
sourceMapFilename: 'style/bootstrap-pix.treasure.map'
},
src: 'less/bootstrap-pix.less',
dest: 'style/bootstrap-pix.css'
},
fontawesome_d: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: true,
sourceMapRootpath: MOODLEURLPREFIX + '/theme/' + THEMEDIR,
sourceMapFilename: 'style/fontawesome.treasure.map'
},
src: 'less/fontawesome.less',
dest: 'style/fontawesome.css'
},
scrollbars_d: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: true,
sourceMapRootpath: MOODLEURLPREFIX + '/theme/' + THEMEDIR,
sourceMapFilename: 'style/essential-scrollbars.treasure.map'
},
src: 'less/essential-scrollbars.less',
dest: 'style/essential-scrollbars.css'
},
settings_d: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: true,
sourceMapRootpath: MOODLEURLPREFIX + '/theme/' + THEMEDIR,
sourceMapFilename: 'style/essential-settings.treasure.map'
},
src: 'less/essential-settings.less',
dest: 'style/essential-settings.css'
},
alternative_d: {
options: {
compress: false,
cleancss: false,
paths: "./less",
report: 'min',
sourceMap: true,
sourceMapRootpath: MOODLEURLPREFIX + '/theme/' + THEMEDIR,
sourceMapFilename: 'style/essential-alternative.treasure.map'
},
src: 'less/essential-alternative.less',
dest: 'style/essential-alternative.css'
}
},
exec: {
decache: {
cmd: 'php "' + decachephp + '"',
callback: function(error) {
// The exec process will output error messages.
// Just add one to confirm success.
if (!error) {
grunt.log.writeln("Moodle theme cache reset.");
}
}
}
},
watch: {
// Watch for any changes to less files and compile.
files: ["less/**/*.less"],
tasks: ["compile"],
options: {
spawn: false
}
},
cssmin: {
essential_p: {
files: [{
expand: true,
cwd: 'style',
src: ['essential.css', 'bootstrap-pix.css', 'essential-alternative.css', 'editor.css', 'fontawesome.css'], // jshint ignore:line
dest: 'style',
ext: '.css'
}]
}
},
cssmetrics: {
dist: {
src: [
'style/*.css'
]
}
},
copy: {
svg_core: {
expand: true,
cwd: 'pix_core_originals/',
src: '**',
dest: 'pix_core/',
},
svg_plugins: {
expand: true,
cwd: 'pix_plugins_originals/',
src: '**',
dest: 'pix_plugins/',
}
},
replace: {
svg_colours_core: {
src: 'pix_core/**/*.svg',
overwrite: true,
replacements: [{
from: '#999',
to: svgcolour
}]
},
svg_colours_plugins: {
src: 'pix_plugins/**/*.svg',
overwrite: true,
replacements: [{
from: '#999',
to: svgcolour
}]
},
placeholder: {
src: 'style/essential.css',
overwrite: true,
replacements: [{
from: '/* Essential placeholder */',
to: 'div#page::before { content: "Development version - recomplile LESS with \'grunt compile -build=p\' for production CSS."; font-size: 2em; margin-top: 24px; margin-bottom: 24px; line-height: 42px; text-align: center; }' // jshint ignore:line
}]
}
},
svgmin: {
options: {
plugins: [{
removeViewBox: false
}, {
removeUselessStrokeAndFill: false
}, {
convertPathData: {
straightCurves: false
}
}]
},
dist: {
files: [{
expand: true,
cwd: 'pix_core',
src: ['**/*.svg'],
dest: 'pix_core/',
ext: '.svg'
}, {
expand: true,
cwd: 'pix_plugins',
src: ['**/*.svg'],
dest: 'pix_plugins/',
ext: '.svg'
}]
}
},
jshint: {
options: {jshintrc: moodleroot + '/.jshintrc'},
files: ['**/amd/src/*.js']
},
uglify: {
options: {
preserveComments: 'some'
},
dynamic_mappings: {
files: grunt.file.expandMapping(
['**/src/*.js', '!**/node_modules/**'],
'',
{
cwd: PWD,
rename: function(destBase, destPath) {
destPath = destPath.replace('src', 'build');
destPath = destPath.replace('.js', '.min.js');
destPath = path.resolve(PWD, destPath);
return destPath;
}
}
)
}
}
});
// Load contrib tasks.
grunt.loadNpmTasks("grunt-contrib-less");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-exec");
grunt.loadNpmTasks("grunt-text-replace");
grunt.loadNpmTasks("grunt-css-metrics");
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-svgmin');
// Load core tasks.
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
// Register tasks.
grunt.registerTask("default", ["watch"]);
grunt.registerTask("decache", ["exec:decache"]);
grunt.registerTask("css", [
"less:essential_" + build,
"less:editor_" + build,
"less:scrollbars_" + build,
"less:settings_" + build,
"less:bootstrap_pix_" + build,
"less:fontawesome_" + build,
"less:alternative_" + build]);
if (build == 'd') {
grunt.registerTask("compile", ["css", "replace:placeholder", 'cssmetrics', "decache"]);
} else {
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.registerTask("compile", ["css", "cssmin:essential_p", 'cssmetrics', "decache"]);
}
grunt.registerTask("copy:svg", ["copy:svg_core", "copy:svg_plugins"]);
grunt.registerTask("replace:svg_colours", ["replace:svg_colours_core", "replace:svg_colours_plugins"]);
grunt.registerTask("svg", ["copy:svg", "replace:svg_colours", "svgmin"]);
grunt.registerTask("amd", ["jshint", "uglify", "decache"]);
};