-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
288 lines (250 loc) · 7.96 KB
/
gulpfile.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
/*
* Settings
*/
// Set this for browserSync to know where your
// url normally is.
var localhost = "https://decision-tree.test";
/*
* NPM Packages
*/
const gulp = require('gulp');
const browserSync = require('browser-sync');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const reload = browserSync.reload;
const crass = require('gulp-crass');
const cssVip = require('gulp-css-vip'); // adds !important tags to all CSS lines
const uglify = require('gulp-uglify');
const rename = require("gulp-rename");
const concat = require("gulp-concat");
const babel = require("gulp-babel");
const plumber = require('gulp-plumber');
const insert = require('gulp-insert');
// Handlebars and dependencies
const handlebars = require('gulp-handlebars');
const wrap = require('gulp-wrap');
const declare = require('gulp-declare');
const cssFiles = [
'base',
'structure-color',
'structure-typography',
'structure'
];
// Static Server + watching scss/html files
gulp.task('serve', ['sass', 'iframeJS', 'TreeJS', 'TreeLoaderJS', 'handlebars', 'editSass', 'editJs'], function() {
browserSync({
proxy: localhost
});
// Watch SCSS file for change to pass on to sass compiler,
gulp.watch(['assets/sass/*.{scss,sass}','assets/sass/*/*.{scss,sass}'], ['sass']);
gulp.watch(['dist/css/'+cssFiles[cssFiles.length - 1].toString()+'.min.css'], ['sassImportantify']);
gulp.watch(['dist/css/'+cssFiles[cssFiles.length - 1].toString()+'-important.min.css'], ['sassCleanSlate']);
// Watch JS file for change to pass on to sass compiler,
gulp.watch('assets/js/iframe-parent/*.js', ['iframeJS']);
gulp.watch('assets/js/Tree/*.js', ['TreeJS']);
gulp.watch('assets/js/TreeLoader/*.js', ['TreeLoaderJS']);
// compile templates
gulp.watch('templates/*.hbs', ['handlebars']);
// compile js
gulp.watch(["dist/js/scripts.js", "dist/js/handlebars.runtime.js", "dist/js/templates.js"], ['concatTreeJS']);
// Watch our CSS file and reload when it's done compiling
gulp.watch("dist/css/structure-typography-important.*").on('change', reload);
// Watch php file
gulp.watch("../*/*.php").on('change', reload);
// watch javascript files
gulp.watch("dist/js/cme-tree.min.js").on('change', reload);
compressJS("dist/js/handlebars.runtime.js");
// edit files
gulp.watch("views/edit/assets/sass/*.scss", ['editSass'])
gulp.watch("views/edit/assets/js/*.js", ['editJs'])
gulp.watch("views/edit/dist/*/*").on('change', reload);
gulp.watch("views/edit/*.php").on('change', reload);
});
gulp.task('sass', function () {
for(let i = 0; i < cssFiles.length; i++) {
processSASS(cssFiles[i]);
}
});
gulp.task('sassImportantify', function () {
for(let i = 0; i < cssFiles.length; i++) {
cssImportantify(cssFiles[i]);
}
});
gulp.task('sassCleanSlate', function () {
for(let i = 0; i < cssFiles.length; i++) {
cssCleanSlate(cssFiles[i]);
}
});
gulp.task('TreeJS', function() {
var jsFiles = 'assets/js/Tree/*.js',
jsDest = 'dist/js';
return gulp.src(jsFiles)
.pipe(plumber())
.pipe(babel())
.pipe(concat('scripts.js'))
.pipe(gulp.dest(jsDest))
.pipe(rename('scripts.min.js'))
.pipe(uglify())
.pipe(gulp.dest(jsDest));
});
gulp.task('TreeLoaderJS', function() {
var jsFiles = 'assets/js/TreeLoader/*.js',
jsDest = 'dist/js';
return gulp.src(jsFiles)
.pipe(plumber())
.pipe(babel({
presets: ['env']
}))
.pipe(concat('TreeLoader.js'))
.pipe(gulp.dest(jsDest))
.pipe(rename('TreeLoader.min.js'))
.pipe(uglify())
.pipe(gulp.dest(jsDest));
});
gulp.task('iframeJS', function() {
var jsFiles = 'assets/js/iframe-parent/*.js',
jsDest = 'dist/js';
return gulp.src(jsFiles)
.pipe(plumber())
.pipe(babel({
presets: ['env']
}))
.pipe(concat('iframe-parent.js'))
.pipe(gulp.dest(jsDest))
.pipe(rename('iframe-parent.min.js'))
.pipe(uglify())
.pipe(gulp.dest(jsDest));
});
function compressJS(src) {
dist = 'dist/js/';
return gulp.src(src)
.pipe(plumber())
.pipe(uglify())
.pipe(rename({
suffix: '.min'
}))
.pipe(gulp.dest(dist));
}
gulp.task('concatTreeJS', function() {
dist = 'dist/js/';
src = [dist+'handlebars.runtime.js',
dist+'templates.js',
dist+'scripts.js'
];
filename = 'cme-tree';
return gulp.src(src)
.pipe(concat('cme-tree.js'))
.pipe(gulp.dest(dist))
.pipe(rename({
suffix: '.min'
}))
.pipe(uglify())
.pipe(gulp.dest(dist));
});
function processSASS(filename) {
return gulp.src('assets/sass/output/'+filename+'.{scss,sass}')
// Converts Sass into CSS with Gulp Sass
.pipe(plumber())
.pipe(sass({
errLogToConsole: true
}))
// adds prefixes to whatever needs to get done
.pipe(autoprefixer())
// minify the CSS
.pipe(crass({pretty:false}))
// rename to add .min
.pipe(rename({
suffix: '.min'
}))
// Outputs CSS files in the css folder
.pipe(gulp.dest('dist/css/'));
}
function cssImportantify(filename) {
return gulp.src('dist/css/'+filename+'.min.css')
// catch errors
.pipe(plumber())
// wrap our file in an .cme-tree__loader-container {}
// before processing it with sass to increase specificty
.pipe(insert.wrap('.cme-tree__loader-container {', '}'))
// process it with sass
.pipe(sass({
errLogToConsole: true
}))
// adds prefixes to whatever needs to get done
.pipe(autoprefixer())
// add !important tags
.pipe(cssVip())
// minify the CSS
.pipe(crass({pretty:false}))
// rename to add .min
.pipe(rename({
basename: filename+'-important',
suffix: '.min'
}))
// Outputs CSS files in the css folder
.pipe(gulp.dest('dist/css/'));
}
function cssCleanSlate(filename) {
return gulp.src(['assets/css/clean-slate.css','dist/css/'+filename+'-important.min.css'])
// catch errors
.pipe(plumber())
// concatenate clean slate with the '-important' files
.pipe(concat(filename+'-clean-slate.css'))
// minify the CSS
// @TODO This makes it so you have to save twice, apparently...
.pipe(crass({pretty:false}))
// rename to add .min
.pipe(rename({
suffix: '.min'
}))
// Outputs CSS files in the css folder
.pipe(gulp.dest('dist/css/'));
}
gulp.task('handlebars', function(){
gulp.src('templates/*.hbs')
.pipe(handlebars({
handlebars: require('handlebars')
}))
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(declare({
namespace: 'TreeTemplates',
noRedeclare: true, // Avoid duplicate declarations
}))
.pipe(concat('templates.js'))
.pipe(gulp.dest('dist/js/'))
.pipe(rename('templates.min.js'))
.pipe(uglify())
.pipe(gulp.dest('dist/js/'));
});
gulp.task('editSass', function () {
return gulp.src('views/edit/assets/sass/edit.scss')
// Converts Sass into CSS with Gulp Sass
.pipe(plumber())
.pipe(sass({
errLogToConsole: true
}))
// adds prefixes to whatever needs to get done
.pipe(autoprefixer())
// minify the CSS
.pipe(crass({pretty:false}))
// rename to add .min
.pipe(rename({
suffix: '.min'
}))
// Outputs CSS files in the css folder
.pipe(gulp.dest('views/edit/dist/css/'));
});
gulp.task('editJs', function () {
var jsFiles = 'views/edit/assets/js/*.js',
jsDest = 'views/edit/dist/js';
return gulp.src(jsFiles)
.pipe(plumber())
.pipe(babel())
.pipe(concat('edit.js'))
.pipe(gulp.dest(jsDest))
.pipe(rename('edit.min.js'))
.pipe(uglify())
.pipe(gulp.dest(jsDest));
});
// Creating a default task
gulp.task('default', ['serve']);