forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
101 lines (83 loc) · 2.81 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
// Initialize modules
// Importing specific gulp API functions lets us write them below as series() instead of gulp.series()
const { src, dest, watch, series, parallel } = require('gulp');
// Importing all the Gulp-related packages we want to use
const sourcemaps = require('gulp-sourcemaps');
const sass = require('gulp-sass');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const babel = require('gulp-babel');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const replace = require('gulp-replace');
const minifyCSS = require("gulp-minify-css");
const gulp = require('gulp');
const prettier = require('gulp-prettier');
// File paths
const files = {
jsPath: 'js/**/*.js',
cssPath: 'css/*.css',
sassPath: 'css/*.sass'
}
const sassTask = () => {
return src(files.sassPath)
.pipe(sourcemaps.init()) // initialize sourcemaps first
.pipe(sass()) // compile SASS to CSS
.pipe(postcss([ autoprefixer(), cssnano() ])) // PostCSS plugins
.pipe(sourcemaps.write('.')) // write sourcemaps file in current directory
.pipe(dest('dist/css')
); // put final CSS in dist folder
}
const cssTask = () => {
return src(files.cssPath)
.pipe(minifyCSS({compatibility: 'ie8'}))
.pipe(gulp.dest('dist/css'));
};
// JS task: concatenates and uglifies JS files to app.min.js
const jsTask = () => {
return src([
files.jsPath
])
.pipe(concat('app.min.js'))
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(uglify())
.pipe(dest('dist')
);
}
// Cachebust
const cbString = new Date().getTime();
const cacheBustTask = () => {
return src(['index.html'])
.pipe(replace(/cb=\d+/g, 'cb=' + cbString))
.pipe(dest('.'));
}
//This gulp task formats the js files
const prettify = () => {
return gulp.src(files.jsPath)
.pipe(prettier({ singleQuote: true,
trailingComma: "all"
}))
.pipe(gulp.dest('./dist/js'));
};
//to check whether or not files adhere to Prettier's formatting
const validate = () => {
return gulp.src(files.jsPath)
.pipe(prettier.check({ singleQuote: true, trailingComma: "all"}));
};
// Watch task: watch SASS , CSS and JS files for changes
// If any change, run sass, css and js tasks simultaneously
const watchTask = () => {
watch([ files.jsPath, files.cssPath, files.sassPath ],
parallel( jsTask, cssTask, sassTask));
}
// Export the default Gulp task so it can be run
// Runs the sass ,css and js tasks simultaneously
// then runs prettify, cacheBust, watch task, then validate
exports.default = series(
parallel( jsTask, cssTask , sassTask ), prettify,
cacheBustTask,
watchTask, validate
);