-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gruntfile.js
287 lines (267 loc) · 9.61 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
/**
* Gruntfile for compiling plugin .scss files AND .vue 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:
* Default behaviour is to watch all .less files and compile
* into compressed CSS when a change is detected to any and then
* clear the theme's caches. Invoke either `grunt` or `grunt watch`
* in the theme's root directory.
*
* To separately compile only moodle or editor .less files
* run `grunt less:moodle` or `grunt less:editor` respectively.
*
* To only clear the theme caches invoke `grunt exec:decache` in
* the theme's root directory.
*
* @author Joby Harding / David Scotson / Stuart Lamour / Guy Thomas (vue files and es5 transpilation)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
const sass = require('node-sass');
module.exports = function(grunt) {
// We need to include the core Moodle grunt file too, so that we can run the tasks that are defined there.
require("grunt-load-gruntfile")(grunt);
grunt.loadGruntfile("../../Gruntfile.js");
// PHP strings for exec task.
const moodleroot = 'dirname(dirname(__DIR__))',
configfile = moodleroot + ' . "/config.php"';
let decachephp = '';
decachephp += "define(\"CLI_SCRIPT\", true);";
decachephp += "require(" + configfile + ");";
// The previously used theme_reset_all_caches() stopped working for us, we investigated but couldn't figure out why.
// Using purge_all_caches() is a bit of a nuclear option, as it clears more than we should need to
// but it gets the job done.
decachephp += "purge_all_caches();";
const force = grunt.option('force') ? 1 : 0;
const cwd = process.env.PWD || process.cwd();
const path = require('path');
const inAMD = path.basename(cwd) == 'amd';
// Globbing pattern for matching all AMD JS source files.
const es5Src = [inAMD ? cwd + '/es5/*.js' : '**/amd/es5/*.js'];
const amdSrc = [inAMD ? cwd + '/src/*.js' : '**/amd/src/*.js'];
/**
* Function to generate the destination for the uglify task
* (e.g. build/file.min.js). This function will be passed to
* the rename property of files array when building dynamically:
* http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
*
* @param {String} destPath the current destination
* @param {String} srcPath the matched src path
* @return {String} The rewritten destination path.
*/
var uglifyRename = function(destPath, srcPath) {
destPath = srcPath.replace('src', 'build');
destPath = destPath.replace('.js', '.min.js');
destPath = path.resolve(cwd, destPath);
return destPath;
};
/**
* For ES5 folder.
* Function to generate the destination for the uglify task
* (e.g. build/file.min.js). This function will be passed to
* the rename property of files array when building dynamically:
* http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
*
* @param {String} destPath the current destination
* @param {String} srcPath the matched src path
* @return {String} The rewritten destination path.
*/
var uglifyRenameEs5 = function(destPath, srcPath) {
destPath = srcPath.replace('es5', 'build');
destPath = destPath.replace('.js', '.min.js');
destPath = path.resolve(cwd, destPath);
return destPath;
};
var vuephp = `define("CLI_SCRIPT", true);
require(${configfile});
$force = ${force};
$workingDir = __DIR__;
$vueCompDir = __DIR__."/vue/comps";
if (!file_exists($vueCompDir)) {
$files = [];
} else {
$files = scandir($vueCompDir);
}
foreach ($files as $file) {
if ($file === "." || $file === "..") {
continue;
}
$compName = $file;
$compDir = "$vueCompDir/$compName";
if (is_dir($compDir)) {
$vueFile = "{$compDir}/{$compName}.vue";
$hashFile = "{$compDir}/.vuefilehash";
$hash = null;
if (file_exists($hashFile)) {
$hash = file_get_contents($hashFile);
}
$currentHash = sha1_file($vueFile);
if ($currentHash === $hash && !$force) {
echo "\\n Skipping up to date vue file {$compName}.vue \\n";
continue;
}
file_put_contents($hashFile, $currentHash);
if (file_exists($vueFile)) {
$cmd = <<<CMD
cd $compDir; vue-cli-service build --target lib --name $compName {$compName}.vue;
mv $compDir/dist $compDir/modern;
cd $workingDir; npx babel $compDir/modern -d $compDir/dist;
cd $compDir/modern;
find . -not -name "*.js" -not -name "*.js.map" -not -name "." -exec cp {} $compDir/dist \\;
rm -rf $compDir/modern
CMD;
echo shell_exec($cmd);
}
}
}
`;
grunt.mergeConfig = grunt.config.merge;
grunt.mergeConfig({
eslint: {
// Even though warnings dont stop the build we don't display warnings by default because
// at this moment we've got too many core warnings.
options: { "parserOptions": { "ecmaVersion": 2018 }, quiet: !grunt.option('show-lint-warnings')},
amd: {src: amdSrc},
babel: {src: amdSrc}
},
uglify: {
amd: {
files: [{
expand: true,
src: amdSrc,
rename: uglifyRename
}],
options: {report: 'none'}
},
es5: {
files: [{
expand: true,
src: es5Src,
rename: uglifyRenameEs5
}],
options: {report: 'none'}
}
},
sass: {
compile: {
options: {
compress: false,
implementation: sass,
sourceMap: true
},
files: {
"styles.css": "scss/styles.scss",
}
}
},
sasslint: {
src: "styles.css",
target: "scss/styles.scss"
},
autoprefixer: {
options: {
browsers: [
'Android 2.3',
'Android >= 4',
'Chrome >= 20',
'Firefox >= 24', // Firefox 24 is the latest ESR.
'Explorer >= 9',
'iOS >= 6',
'Opera >= 12.1',
'Safari >= 6'
]
},
core: {
options: {
map: false
},
src: ['styles.css'],
},
},
exec: {
decache: {
cmd: "php -r '" + decachephp + "'",
callback: function(error, stdout, stderror) {
// Exec will output error messages.
// Just add one to confirm success.
if (!error) {
grunt.log.writeln("Moodle theme cache reset.");
}
}
},
vue: {
cmd: "php -r '" + vuephp + "'",
callback: function(error, stdout, stderror) {
// Exec will output error messages.
// Just add one to confirm success.
if (!error) {
grunt.log.writeln("Vue compiled.");
}
},
stdout: true,
stderr: true,
stdin: true
},
babel: {
cmd: "npx babel amd/src -d amd/es5",
stdout: true,
stderr: true,
stdin: true
},
cleanUpEs5: {
cmd: "rm -rf amd/es5",
stdout: true,
stderr: true,
stdin: true
}
},
watch: {
// Watch for any changes to sass files and compile.
sass: {
files: ["scss/*.scss"],
tasks: ["compile"],
options: {
spawn: false
}
},
babel: {
files: ["amd/src/*.js"],
tasks: ["babel"],
options: {
spawn: false
}
},
vue: {
files: ["vue/comps/**/*.vue"],
tasks: ["vue"],
options: {
spawn: false
}
}
}
});
// Load contrib tasks.
grunt.loadNpmTasks("grunt-autoprefixer");
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-sass-lint');
grunt.loadNpmTasks("grunt-sass");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks("grunt-exec");
// Register tasks.
grunt.registerTask("default", ["watch"]);
grunt.registerTask("compile", ["sass:compile", "autoprefixer", "decache"]);
grunt.registerTask("decache", ["exec:decache"]);
grunt.registerTask("vue", ["exec:vue"]);
grunt.registerTask("babel", ["exec:babel", "eslint:babel", "uglify:es5"]);
grunt.registerTask("build", ["babel", "vue", "exec:cleanUpEs5", "compile"]);
};