This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
forked from mpcondo/gulp-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
62 lines (56 loc) · 1.7 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
//Initialize modules
const { src, dest, watch, series, parallel } = require("gulp");
//- importing npm packages
const sourcemaps = require("gulp-sourcemaps");
const sass = require("gulp-sass");
const concat = require("gulp-concat");
const uglify = require("gulp-uglify");
const postcss = require("gulp-postcss");
const autoprefixer = require("autoprefixer");
const cssnano = require("cssnano");
var replace = require("gulp-replace");
//path variables
const files = {
scssPath: "app/scss/**/*.scss",
jsPath: "app/js/**/*.js",
};
//SASS TASK
//compiles sass files
function scssTask() {
return src(files.scssPath)
.pipe(sourcemaps.init())
.pipe(sass())
.pipe(postcss([autoprefixer(), cssnano()]))
.pipe(sourcemaps.write("."))
.pipe(dest("dist"));
}
//JS TASK
//concat and minify js files, adds minified js to dist folder
function jsTask() {
return src(files.jsPath)
.pipe(concat("all.js"))
.pipe(uglify())
.pipe(dest("dist"));
}
//CACHE BUSTING
//clears cache when css & js changes are made
///"cacheBusting" attribute is added to stylesheet link, and script link in index.html
///to track changes by milliseonds of the time file is saved.
cacheBustingString = new Date().getTime();
function cacheBustingTask() {
return src(["index.html"])
.pipe(replace(/cacheBusting=\d+/g, "cacheBusting=" + cacheBustingString))
.pipe(dest("."));
}
//WATCH TASK
//Monitors files to detect changes in files before running scssTask and jsTask
function watchTask() {
watch([files.scssPath, files.jsPath], parallel(scssTask, jsTask));
}
//DEFAULT TASK
//main gulp task, needs to be publicly accessible by command line
exports.default = series(
parallel(scssTask, jsTask),
cacheBustingTask,
watchTask
);