forked from vufind-org/vufind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
275 lines (254 loc) · 7.92 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
module.exports = function(grunt) {
const fs = require("fs");
// Load dart-sass
grunt.loadNpmTasks('grunt-dart-sass');
// Local custom tasks
if (fs.existsSync("./Gruntfile.local.js")) {
require("./Gruntfile.local.js")(grunt);
}
require('jit-grunt')(grunt); // Just in time library loading
function getLoadPaths(file) {
var config;
var parts = file.split('/');
parts.pop(); // eliminate filename
// initialize search path with directory containing LESS file
var retVal = [];
retVal.push(parts.join('/'));
retVal.push(parts.join('/') + '/vendor/');
// Iterate through theme.config.php files collecting parent themes in search path:
while (config = fs.readFileSync("themes/" + parts[1] + "/theme.config.php", "UTF-8")) {
// First identify mixins:
var mixinMatches = config.match(/["']mixins["']\s*=>\s*\[([^\]]+)\]/);
if (mixinMatches !== null) {
var mixinParts = mixinMatches[1].split(',');
for (var i = 0; i < mixinParts.length; i++) {
parts[1] = mixinParts[i].trim().replace(/['"]/g, '');
retVal.push(parts.join('/') + '/');
}
}
// Now move up to parent theme:
var matches = config.match(/["']extends["']\s*=>\s*['"](\w+)['"]/);
// "extends" set to "false" or missing entirely? We've hit the end of the line:
if (matches === null || matches[1] === 'false') {
break;
}
parts[1] = matches[1];
retVal.push(parts.join('/') + '/');
retVal.push(parts.join('/') + '/vendor/');
}
return retVal;
}
var fontAwesomePath = '"../../bootstrap3/css/fonts"';
var lessFileSettings = [{
expand: true,
src: "themes/*/less/compiled.less",
rename: function (dest, src) {
return src.replace('/less/', '/css/').replace('.less', '.css');
}
}];
grunt.initConfig({
// LESS compilation
less: {
compile: {
files: lessFileSettings,
options: {
paths: getLoadPaths,
compress: true,
modifyVars: {
'fa-font-path': fontAwesomePath
}
}
}
},
// Less with maps
lessdev: {
less: {
}
},
// SASS compilation
'scss': {
'dart-sass': {
options: {
outputStyle: 'compressed',
quietDeps: true
}
}
},
'check:scss': {
'dart-sass': {
options: {
quietDeps: true
}
}
},
// Convert LESS to SASS, mostly for development team use
lessToSass: {
convert: {
files: [
{
expand: true,
cwd: 'themes/bootstrap3/less',
src: ['*.less', 'components/**/*.less', 'mixins/**/*.less'],
ext: '.scss',
dest: 'themes/bootstrap3/scss'
},
{
expand: true,
cwd: 'themes/bootprint3/less',
src: ['*.less'],
ext: '.scss',
dest: 'themes/bootprint3/scss'
},
{
expand: true,
cwd: 'themes/sandal/less',
src: ['*.less'],
ext: '.scss',
dest: 'themes/sandal/scss'
}
],
options: {
replacements: [
// Activate SCSS
{
pattern: /\/\* #SCSS>/gi,
replacement: "/* #SCSS> */",
order: -1 // Do before anything else
},
{
pattern: /<#SCSS \*\//gi,
replacement: "/* <#SCSS */",
order: -1
},
// Deactivate LESS
{
pattern: /\/\* #LESS> \*\//gi,
replacement: "/* #LESS>",
order: -1
},
{
pattern: /\/\* <#LESS \*\//gi,
replacement: "<#LESS */",
order: -1
},
{ // Change separator in @include statements
pattern: /@include ([^\(]+)\(([^\)]+)\);/gi,
replacement: function mixinCommas(match, $1, $2) {
return '@include ' + $1 + '(' + $2.replace(/;/g, ',') + ');';
},
order: 4 // after defaults included in less-to-sass
},
{ // Remove unquote
pattern: /unquote\("([^"]+)"\)/gi,
replacement: function ununquote(match, $1) {
return $1;
},
order: 4
},
{ // Inline &:extends converted
pattern: /&:extend\(([^\)]+?)( all)?\)/gi,
replacement: '@extend $1',
order: 4
},
{ // Wrap variables in calcs with #{}
pattern: /calc\([^;]+/gi,
replacement: function calcVariables(match) {
return match.replace(/(\$[\w\-]+)/gi, '#{$1}');
},
order: 4
},
{ // Remove !default from extends (icons.scss)
pattern: /@extend ([^;}]+) !default;/gi,
replacement: '@extend $1;',
order: 5
}
]
}
}
},
watch: {
options: {
atBegin: true
},
less: {
files: 'themes/*/less/**/*.less',
tasks: ['less']
},
lessdev: {
files: 'themes/*/less/**/*.less',
tasks: ['lessdev']
},
scss: {
files: 'themes/*/scss/**/*.scss',
tasks: ['scss']
}
}
});
grunt.registerMultiTask('lessdev', function lessWithMaps() {
grunt.config.set('less', {
dev: {
files: lessFileSettings,
options: {
paths: getLoadPaths,
sourceMap: true,
sourceMapFileInline: true,
modifyVars: {
'fa-font-path': fontAwesomePath
}
}
}
});
grunt.task.run('less');
});
grunt.registerMultiTask('scss', function sassScan() {
grunt.config.set('dart-sass', getSassConfig(this.data.options, false));
grunt.task.run('dart-sass');
});
grunt.registerMultiTask('check:scss', function sassCheck() {
grunt.config.set('dart-sass', getSassConfig(this.data.options, true));
grunt.task.run('dart-sass');
});
grunt.registerTask('default', function help() {
grunt.log.writeln(`\nHello! Here are your grunt command options:
- grunt less = compile and compress all themes' LESS files to css.
- grunt scss = compile and map all themes' SASS files to css.
- grunt lessdev = compile and map all themes' LESS files to css.
- grunt check:scss = check all themes' SASS files.
- grunt watch:[cmd] = continuous monitor source files and run command when changes are detected.
- grunt watch:less
- grunt watch:scss
- grunt watch:lessdev
- grunt lessToSass = transpile all LESS files to SASS.`);
});
function getSassConfig(additionalOptions, checkOnly) {
var sassConfig = {},
path = require('path'),
themeList = fs.readdirSync(path.resolve('themes')).filter(function (theme) {
return fs.existsSync(path.resolve('themes/' + theme + '/scss/compiled.scss'));
});
for (var i in themeList) {
if (Object.prototype.hasOwnProperty.call(themeList, i)) {
var config = {
options: {},
files: [{
expand: true,
cwd: path.join('themes', themeList[i], 'scss'),
src: ['compiled.scss'],
dest: checkOnly ? null : path.join('themes', themeList[i], 'css'),
ext: '.css'
}]
};
for (var key in additionalOptions) {
if (Object.prototype.hasOwnProperty.call(additionalOptions, key)) {
config.options[key] = additionalOptions[key];
}
}
config.options.includePaths = getLoadPaths('themes/' + themeList[i] + '/scss/compiled.scss');
// This allows loading of styles from composer dependencies:
config.options.includePaths.push('vendor/');
sassConfig[themeList[i]] = config;
}
}
return sassConfig;
}
};