-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gruntfile.js
209 lines (194 loc) · 5.08 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
/* eslint import/no-dynamic-require:0 */
/* eslint global-require:0 */
/* eslint-disable-next-line func-names */
module.exports = function (grunt) {
// Utility to load the different option files
// based on their names
function loadConfig(path) {
const glob = require('glob');
const object = {};
let key;
glob.sync('*', { cwd: path }).forEach((option) => {
key = option.replace(/\.js$/, '');
object[key] = require(path + option);
});
return object;
}
// Initial config
const config = {
pkg: grunt.file.readJSON('package.json'),
env: process.env,
paths: {
// PHP assets
php: {
files_std: [
// Standard file match
'**/*.php',
'!node_modules/**/*.php',
'!vendor/**/*.php',
],
files: '<%= paths.php.files_std %>', // Dynamic file match
},
// Files which should be prettified with Prettier.
prettier: {
files_std: [
// Standard file match
'<%= paths.js.files %>',
'<%= paths.sass.files %>',
'<%= paths.json.files %>',
],
files: '<%= paths.prettier.files_std %>', // Dynamic file match
},
// JavaScript assets
js: {
grunt: {
tasks: 'tasks/**/*.js',
gruntfile: 'Gruntfile.js',
},
src: 'src/assets/js', // Development code
dest: 'dist/assets/js', // Production code
dist_file_name: 'app.js',
files_std: [
// Standard file match
'<%= paths.js.src %>/**/*.js',
'!<%= paths.js.dest %>/**/*',
'<%= paths.js.grunt.tasks %>',
'<%= paths.js.grunt.gruntfile %>',
],
files: '<%= paths.js.files_std %>', // Dynamic file match
},
// Sass assets
sass: {
src: 'src/assets/scss', // Source files dir
dest: 'dist/assets/css', // Compiled files dir
files_std: [
// Standard file match
'<%= paths.sass.src %>/**/*.scss',
],
files: '<%= paths.sass.files_std %>', // Dynamic file match
},
// CSS assets
css: {
src: 'dist/assets/css', // Source files dir
files_std: [
// Standard file match
'<%= paths.css.src %>/*.css',
],
files: '<%= paths.css.files_std %>', // Dynamic file match
},
// JSON assets
json: {
files_std: [
// Standard file match
'**/*.json',
'.eslintrc',
'.prettierrc',
'.stylelintrc.json',
'!package-lock.json',
'!node_modules/**/*.json',
'!vendor/**/*.json',
],
files: '<%= paths.json.files_std %>', // Dynamic file match
},
// html assets
html: {
src: '.', // Source files dir
files_std: [
// Standard file match
'<%= paths.html.src %>/**/*.{html,htm}',
'!./node_modules/**/*.{html,htm}',
'!./vendor/**/*.{html,htm}',
],
files: '<%= paths.html.files_std %>', // Dynamic file match
},
// Images
img: {
ext: 'png,jpg,gif,svg,jpeg',
src: 'src/assets/images/**/*.{<%= paths.img.ext %>}', // Source files
dest: 'dist/assets/images', // destination files dir
files_std: [
// Standard file match
'<%= paths.img.src %>',
'!<%= paths.img.dest %>',
],
files: '<%= paths.img.files_std %>', // Dynamic file match
},
},
};
// Load tasks from the tasks folder
grunt.loadTasks('tasks');
// Load all task options in tasks/options
grunt.util._.extend(config, loadConfig('./tasks/options/'));
/**
* Adds input source map for babel task.
* Because grunt tries to read the map before it was generated, this has to be done
* in a task. configureBabelInputSourceMap task must always be called before babel task.
*/
function setBabelInputSourceMap() {
config.babel.options.inputSourceMap = grunt.file.readJSON(
`${config.paths.js.dest}/${config.paths.js.dist_file_name}.js.map`,
);
}
grunt.task.registerTask(
'configureBabelInputSourceMap',
'configures babel input source map',
setBabelInputSourceMap,
);
grunt.initConfig(config);
grunt.event.on('watch', (action, filepath) => {
// Determine task based on filepath
function getExt(path) {
let ret = '';
const i = path.lastIndexOf('.');
if (i !== -1 && i <= path.length) {
ret = path.substr(i + 1);
}
return ret;
}
switch (getExt(filepath)) {
// PHP
case 'php':
grunt.config('paths.php.files', [filepath]);
break;
// JavaScript
case 'js':
grunt.config('paths.js.files', [filepath]);
grunt.config('paths.prettier.files', [filepath]);
break;
// SASS / SCSS
case 'sass':
case 'scss':
grunt.config('paths.sass.files', [filepath]);
grunt.config('paths.prettier.files', [filepath]);
break;
// CSS
case 'css':
grunt.config('paths.css.files', [filepath]);
break;
// JSON
case 'json':
case 'eslintrc':
case 'prettierrc':
grunt.config('paths.json.files', [filepath]);
grunt.config('paths.prettier.files', [filepath]);
break;
// html
case 'htm':
case 'html':
grunt.config('paths.html.files', [filepath]);
break;
// Images
case 'png':
case 'jpg':
case 'gif':
case 'svg':
case 'jpeg':
grunt.config('paths.img.files', [filepath]);
break;
default:
break;
}
});
// loads any tasks listed in devDependencies in package.json
require('load-grunt-tasks')(grunt);
};