forked from niccokunzmann/schulcloud-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
384 lines (352 loc) · 10.3 KB
/
gulpfile.js
File metadata and controls
384 lines (352 loc) · 10.3 KB
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
const autoprefixer = require('autoprefixer');
const fs = require('fs');
const gulp = require('gulp');
const babel = require('gulp-babel');
const changed = require('gulp-changed-smart');
const cleanCSS = require('gulp-clean-css');
const concat = require('gulp-concat');
const gulpCount = require('gulp-count');
const filelog = require('gulp-filelog');
const header = require('gulp-header');
const gulpif = require('gulp-if');
const imagemin = require('gulp-imagemin');
const optimizejs = require('gulp-optimize-js');
const plumber = require('gulp-plumber');
const postcss = require('gulp-postcss');
const cssvariables = require('postcss-css-variables');
const rimraf = require('gulp-rimraf');
const sass = require('gulp-sass');
const sassGrapher = require('gulp-sass-grapher');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const path = require('path');
const named = require('vinyl-named');
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const nodemon = require('gulp-nodemon');
const browserSync = require('browser-sync');
const workbox = require('workbox-build');
const webpackConfig = require('./webpack.config');
const baseScripts = [
'./static/scripts/jquery/jquery.min.js',
'./static/scripts/jquery/jquery.serialize-object.js',
'./static/scripts/tether/tether.min.js',
'./static/scripts/bootstrap/bootstrap.min.js',
'./static/scripts/chosen/chosen.jquery.min.js',
'./static/scripts/base.js',
'./static/scripts/toggle/bootstrap-toggle.min.js',
'./static/scripts/mailchimp/mailchimp.js',
'./static/scripts/qrcode/kjua-0.1.1.min.js',
];
function themeName() {
return process.env.SC_THEME || 'default';
}
const nonBaseScripts = [
'./static/scripts/**/*.js',
'!./static/scripts/sw/workbox/*.*',
].concat(baseScripts.map(script => `!${script}`));
// used by all gulp tasks instead of gulp.src(...)
// plumber prevents pipes from stopping when errors occur
// changed only passes on files that were modified since last time
// filelog logs and counts all processed files
function withTheme(src) {
if (typeof src === 'string') {
return [src, `./theme/${themeName()}/${src.slice(2)}`];
}
return src.concat(src
.map(e => `./theme/${themeName()}/${e.slice(2)}`));
}
const beginPipe = src => gulp
.src(withTheme(src))
.pipe(plumber())
.pipe(changed(gulp))
.pipe(filelog());
const beginPipeAll = src => gulp
.src(withTheme(src))
.pipe(plumber())
.pipe(filelog());
// minify images
gulp.task('images', () => beginPipe('./static/images/**/*.*')
.pipe(imagemin())
.pipe(gulp.dest(`./build/${themeName()}/images`)));
// minify static/other
gulp.task('other', () => gulp
.src('./static/other/**/*.*')
.pipe(gulp.dest(`./build/${themeName()}/other`)));
const loadPaths = path.resolve('./static/styles/');
sassGrapher.init('./static/styles/', {
loadPaths,
});
let firstRun = true;
gulp.task('styles', () => {
const themeFile = `./theme/${themeName()}/style.scss`;
return beginPipe('./static/styles/**/*.{css,sass,scss}')
.pipe(gulpif(!firstRun, sassGrapher.ancestors()))
.pipe(header(fs.readFileSync(themeFile, 'utf8')))
.pipe(filelog('PROCESS: '))
.pipe(sourcemaps.init())
.pipe(sass({
sourceMap: true,
includePaths: ['node_modules'],
}).on('error', sass.logError))
.pipe(postcss([
cssvariables({
preserve: true,
}),
autoprefixer({
browsers: ['last 3 version'],
}),
]))
.pipe(cleanCSS({
compatibility: 'ie9',
}))
.pipe(sourcemaps.write('./sourcemaps'))
.pipe(gulp.dest(`./build/${themeName()}/styles`))
.pipe(browserSync.stream());
});
gulp.task('styles-done', ['styles'], () => {
firstRun = false;
});
// copy fonts
gulp.task('fonts', () => beginPipe('./static/fonts/**/*.*').pipe(gulp.dest(`./build/${themeName()}/fonts`)));
// copy static assets
gulp.task('static', () => beginPipe('./static/*').pipe(gulp.dest(`./build/${themeName()}/`)));
// compile/transpile JSX and ES6 to ES5 and minify scripts
gulp.task('scripts', () => beginPipeAll(nonBaseScripts)
.pipe(
named((file) => {
// As a preparation for webpack stream: Transform nonBaseScripts paths
// e.g. '/static/scripts/schics/schicEdit.blub.min.js' -> 'schics/schicEdit.blub.min'
const initialPath = file.history[0].split('scripts')[1];
const pathSegments = initialPath.split('.');
const concretePath = pathSegments
.slice(0, pathSegments.length - 1)
.join('.');
const fileName = concretePath
.split('')
.slice(1)
.join('');
return fileName;
}),
)
.pipe(webpackStream(webpackConfig, webpack))
.pipe(gulp.dest(`./build/${themeName()}/scripts`))
.pipe(browserSync.stream()));
// compile/transpile JSX and ES6 to ES5, minify and concatenate base scripts into all.js
gulp.task('base-scripts', () => beginPipeAll(baseScripts)
.pipe(gulpCount('## js-files selected'))
.pipe(babel({
presets: [
[
'es2015',
{
modules: false,
},
],
],
}))
.pipe(optimizejs())
.pipe(uglify())
.pipe(concat('all.js'))
.pipe(gulp.dest(`./build/${themeName()}/scripts`)));
// compile vendor SASS/SCSS to CSS and minify it
gulp.task('vendor-styles', () => beginPipe('./static/vendor/**/*.{css,sass,scss}')
.pipe(sourcemaps.init())
.pipe(sass({
sourceMap: true,
}))
.pipe(postcss([
autoprefixer({
browsers: ['last 3 version'],
}),
]))
.pipe(cleanCSS({
compatibility: 'ie9',
}))
.pipe(sourcemaps.write('./sourcemaps'))
.pipe(gulp.dest(`./build/${themeName()}/vendor`))
.pipe(browserSync.stream()));
// compile/transpile vendor JSX and ES6 to ES5 and minify scripts
gulp.task('vendor-scripts', () => beginPipe('./static/vendor/**/*.js')
.pipe(babel({
compact: false,
presets: [
[
'es2015',
{
modules: false,
},
],
],
plugins: ['transform-react-jsx'],
}))
.pipe(optimizejs())
.pipe(uglify())
.pipe(gulp.dest(`./build/${themeName()}/vendor`)));
// copy other vendor files
gulp.task('vendor-assets', () => beginPipe([
'./static/vendor/**/*.*',
'!./static/vendor/**/*.js',
'!./static/vendor/**/*.{css,sass,scss}',
]).pipe(gulp.dest(`./build/${themeName()}/vendor`)));
// copy vendor-optimized files
gulp.task('vendor-optimized-assets', () => beginPipe(['./static/vendor-optimized/**/*.*'])
.pipe(gulp.dest(`./build/${themeName()}/vendor-optimized`)));
// copy node modules
const nodeModules = ['mathjax', 'font-awesome'];
gulp.task('node-modules', () => Promise.all(nodeModules
.map(module => beginPipe([`./node_modules/${module}/**/*.*`])
.pipe(gulp.dest(`./build/${themeName()}/vendor-optimized/${module}`)))));
gulp.task('sw-workbox', () => beginPipe(['./static/scripts/sw/workbox/*.js'])
.pipe(gulp.dest(`./build/${themeName()}/scripts/sw/workbox`)));
// service worker patterns used for precaching of files
const globPatterns = [
'fonts/**/*.{woff,css}',
'images/logo/*.svg',
'images/footer-logo.png',
'scripts/all.js',
'scripts/loggedin.js',
'scripts/sw/metrix.js',
'scripts/calendar.js',
'scripts/dashboard.js',
'scripts/courses.js',
'scripts/news.js',
'styles/lib/*.css',
'styles/lib/toggle/*.min.css',
'styles/lib/datetimepicker/*.min.css',
'styles/calendar/*.css',
'styles/news/*.css',
'styles/courses/*.css',
'styles/dashboard/*.css',
'vendor/introjs/intro*.{js,css}',
'vendor-optimized/firebasejs/3.9.0/firebase-app.js',
'vendor-optimized/firebasejs/3.9.0/firebase-messaging.js',
'vendor/feathersjs/feathers.js',
'vendor-optimized/mathjax/MathJax.js',
'images/manifest.json',
];
gulp.task(
'generate-service-worker',
[
'images',
'other',
'styles',
'fonts',
'scripts',
'base-scripts',
'vendor-styles',
'vendor-scripts',
'vendor-assets',
],
() => workbox
.injectManifest({
globDirectory: `./build/${themeName()}/`,
globPatterns,
swSrc: './static/sw.js',
swDest: `./build/${themeName()}/sw.js`,
templatedUrls: {
'/calendar/': [
'../../views/calendar/calendar.hbs',
'../../views/lib/loggedin.hbs',
],
},
})
.then(({ count, size, warnings }) => {
// Optionally, log any warnings and details.
warnings.forEach(console.warn); // eslint-disable-line no-console
console.log(`${count} files will be precached, totaling ${size} bytes.`); // eslint-disable-line no-console
})
.catch((error) => {
console.warn('Service worker generation failed:', error); // eslint-disable-line no-console
}),
);
// clear build folder + smart cache
gulp.task('clear', () => gulp
.src(
[
'./build/*',
'./.gulp-changed-smart.json',
'./.webpack-changed-plugin-cache/*',
],
{
read: false,
},
)
.pipe(rimraf()));
// run all tasks, processing changed files
gulp.task('build-all', [
'images',
'other',
'styles',
'styles-done',
'fonts',
'scripts',
'base-scripts',
'vendor-styles',
'vendor-scripts',
'vendor-assets',
'vendor-optimized-assets',
'generate-service-worker',
'sw-workbox',
'node-modules',
'static',
]);
gulp.task('build-theme-files', ['styles', 'styles-done', 'images', 'static']);
// watch and run corresponding task on change, process changed files only
gulp.task('watch', ['build-all'], () => {
const watchOptions = { interval: 1000 };
gulp.watch(
withTheme('./static/styles/**/*.{css,sass,scss}'),
watchOptions,
['styles', 'styles-done'],
);
gulp.watch(withTheme('./static/images/**/*.*'), watchOptions, [
'images',
]).on('change', browserSync.reload);
gulp.watch(withTheme(nonBaseScripts), watchOptions, [
'scripts',
'generate-service-worker',
]);
gulp.watch(withTheme('./static/vendor-optimized/**/*.*'), watchOptions, [
'vendor-optimized-assets',
]);
gulp.watch(withTheme('./static/sw.js'), watchOptions, [
'generate-service-worker',
]);
gulp.watch(withTheme('./static/*.*'), watchOptions, ['static']);
gulp.watch(withTheme('./static/scripts/sw/workbox/*.*'), watchOptions, [
'sw-workbox',
]);
});
gulp.task('watch-reload', ['watch', 'browser-sync']);
gulp.task('browser-sync', ['nodemon'], () => {
browserSync.init(null, {
proxy: 'http://localhost:3100',
open: false,
port: 7000,
ghostMode: false,
reloadOnRestart: false,
socket: {
clients: {
heartbeatTimeout: 60000,
},
},
});
});
gulp.task('nodemon', (cb) => {
let started = false;
return nodemon({
ext: 'js hbs json',
script: './bin/www',
watch: ['views/', 'controllers/', 'helpers'],
exec: 'node --inspect=9310',
}).on('start', () => {
if (!started) {
cb();
started = true;
}
setTimeout(browserSync.reload, 3000); // server-start takes some time
});
});
// run this if only 'gulp' is run on the commandline with no task specified
gulp.task('default', ['build-all']);