-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
184 lines (161 loc) · 5.06 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
// Built from article: https://wpbeaches.com/getting-browsersync-running-with-gulp-4-and-valet/
// Code here: https://github.com/DanBuda11/gulp-framework/blob/master/gulpfile.js
// ************************* Imports *************************
const gulp = require("gulp");
const { src, dest, series, parallel, watch } = require('gulp');
// BrowserSync for dev server and hot reloading
const bs = require('browser-sync').create();
//const sass = require('gulp-sass');
// Minimize HTML
//const htmlmin = require('gulp-htmlmin');
// Minimize & optimize CSS
const cleanCSS = require('gulp-clean-css');
// Remove unused/dead CSS
//const purifyCSS = require('gulp-purifycss');
// PostCSS with autoprefixer
const postcss = require('gulp-postcss');
// Babel for Gulp
//const babel = require('gulp-babel');
// Minimize JS
//const uglify = require('gulp-uglify');
// Minify images
const imagemin = require('gulp-imagemin');
// Show sizes of files in the terminal
//const size = require('gulp-size');
// Remove comments from files for production
//const strip = require('gulp-strip-comments');
// Used to wipe contents of dist when running build task
const del = require('del');
//css
const sourcemaps = require('gulp-sourcemaps');
const concat = require('gulp-concat');
//gh pages
const ghPages = require('gh-pages');
// ************************* Folder Paths *************************
const paths = {
input: 'src',
output: 'dist',
devHTML: 'src/*.html',
devCSS: 'src/css/*.css',
//devSCSS: 'src/scss/*.scss',
devJS: 'src/js/*.js',
devImages: 'src/img/*.{png,gif,jpg,jpeg,svg}',
//devFavicons: 'src/*.{ico,png,xml,svg,webmanifest}',
prodCSS: 'dist/css',
prodJS: 'dist/js',
prodImages: 'dist/img',
//normalize: 'src/css/normalize.css',
};
// ************************* Development Tasks *************************
// Task to run the BrowserSync server
function browserSync() {
// Run serveSass when starting the dev server to make sure the SCSS & dev CSS are the same
css();
bs.init({
// Dev server will run at localhost:8080
port: 3000,
server: {
baseDir: paths.input,
},
});
watch(paths.devHTML).on('change', bs.reload);
watch(paths.devCSS, css).on('change', bs.reload);
watch(paths.devJS).on('change', bs.reload);
}
// ************************* Production Tasks *************************
// Wipe contents of dist folder
function clean() {
return del([`${paths.output}/**`, `!${paths.output}`]);
}
// Minimize HTML files
function buildHTML() {
return src(paths.devHTML)
//.pipe(strip())
//.pipe(htmlmin({ collapseWhitespace: true, minifyJS: true }))
//.pipe(size({ showFiles: true }))
.pipe(dest(paths.output));
}
// Move any files in font foler to dist folder for deploy
function fonts (cb) {
return gulp.src("src/fonts/*")
.pipe(gulp.dest("dist/fonts"))
cb();
}
// Compiles CSS
function css () {
// Inital sass file to grab for compile
return src([
// we will use concat to package these files together
'src/css/*.css'
])
.pipe(sourcemaps.init())
.pipe(
postcss([
require('autoprefixer'),
require('postcss-preset-env')({
stage:1,
// PostCSS Preset Env supports any standard browserslist configuration
browsers: ["IE 11, last 2 versions"]
})
])
)
.pipe(concat("app.css"))
// Creates minifed CSS file with ie8 compatible syntax
.pipe(
cleanCSS({
compatibility: 'ie8'
})
)
// Writes sourcemap to properly debug minified CSS and identify line location of any errors
.pipe(sourcemaps.write())
// Saves compiled CSS to chosen folder
.pipe(gulp.dest("dist/css"))
// Reload live server to reflect new code
.pipe(bs.stream())
}
// Move normalize.css from src/css to dist/css
/* function buildNormalize() {
return src(paths.normalize)
.pipe(cleanCSS())
.pipe(size({ showFiles: true }))
.pipe(dest(paths.prodCSS));
} */
// Minimize JavaScript files
function buildJS() {
return src(paths.devJS)
.pipe(gulp.dest("dist/js"))
}
// Move any files in image folder to dist folder for deploy
function buildImages() {
return src(paths.devImages)
// Minimize images
.pipe(imagemin())
//.pipe(size({ showFiles: true }))
.pipe(gulp.dest("dist/img"))
}
// ************************* Exported Tasks *************************
// netlify originally made our site from gh as a static site
// but now, we've added gulp script to build things
// in settings > build & deploy > change publist directory to 'dist' folder
exports.deploy = function(cb) {
ghPages.publish('dist');
cb();
}
// Run gulp serve in the terminal to start development mode
exports.serve = browserSync;
// Run gulp clean to empty dist folder
exports.clean = clean;
// Run gulp build to run production build
exports.build = series(
clean,
parallel(
buildHTML,
css,
fonts,
//buildFavicon,
//buildCSS,
//buildNormalize,
buildJS,
buildImages,
),
);