-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
178 lines (147 loc) · 4.72 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
/*
Name: Minimal Web Boilerplate
Desc: Gulp file with tasks to build the dist app.
Author: Edjan Santos
*/
// setting up the refers
const gulp = require('gulp'),
gutil = require('gulp-util'),
sourcemaps = require('gulp-sourcemaps'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
shell = require('gulp-shell'),
sass = require('gulp-sass'),
browserSync = require('browser-sync'),
compress = require('compression');
let jsVendorDependencies = [];
let jsDependencies = [];
let cssDependencies = [
'src/assets/styles/**/*.scss',
'src/assets/styles/**/*.css'
];
let fontsDependencies = [
'src/assets/fonts/**'
];
let anotherFiles = [
'src/**/*.json'
]
let dataFiles = ['src/data/**.**']
let imagesFiles = [
'src/assets/images/**/*.png',
'src/assets/images/**/*.jpg'
]
let distPath = 'dist';
function handleError(err) {
console.log(err.toString());
this.emit('end');
}
// If you want remove sass support, use this to build your css files
gulp.task('build-styles', function () {
return gulp.src(cssDependencies)
.pipe(sourcemaps.init())
.on('error', function (err) {
console.log(err);
})
.pipe(concat('style.css'))
.pipe(sourcemaps.write())
.pipe(gulp.dest(distPath + '/assets/styles'))
.pipe(browserSync.reload({ stream: true }))
});
// Build your javascript files
gulp.task('build-js', function () {
gulp.src(jsVendorDependencies)
.pipe(sourcemaps.init())
.pipe(concat('vendor.js'))
//only uglify if gulp is ran with '--type production'
.pipe(gutil.env.type === 'production' ? uglify() : gutil.noop())
.pipe(sourcemaps.write())
.pipe(gulp.dest(distPath + '/js'))
.pipe(browserSync.reload({ stream: true }));
return gulp.src(jsDependencies)
.pipe(sourcemaps.init())
.pipe(concat('bundle.js'))
//only uglify if gulp is ran with '--type production'
.pipe(gutil.env.type === 'production' ? uglify() : gutil.noop())
.pipe(sourcemaps.write())
.pipe(gulp.dest(distPath + '/js'))
.pipe(browserSync.reload({ stream: true }));
});
// Copy
gulp.task('copy-fonts', function () {
return gulp
.src(fontsDependencies)
.pipe(gulp.dest(distPath + '/assets/fonts'));
});
// Copy HTML files
gulp.task('copy-html', function () {
return gulp
.src(['src/**/*.html', 'src/**/*.htm'])
.pipe(gulp.dest(distPath))
.pipe(browserSync.reload({ stream: true }));
});
// Copy image files
gulp.task('copy-images', function () {
return gulp
.src(imagesFiles)
.pipe(gulp.dest(distPath + '/assets/images'));
});
// Copy another files
gulp.task('copy-resources', function () {
return gulp
.src(anotherFiles)
.pipe(gulp.dest(distPath));
});
// Copy data files
gulp.task('copy-data', function () {
return gulp
.src(dataFiles)
.pipe(gulp.dest(distPath + '/data'));
});
// Browsersync configurations
gulp.task('browserSync', function () {
browserSync({
server: {
baseDir: "dist/",
middleware: function (req, res, next) {
var gzip = compress();
gzip(req, res, next);
}
},
options: {
reloadDelay: 250
},
notify: false,
});
});
//basically just keeping an eye on all HTML files
gulp.task('html', function () {
//watch any and all HTML files and refresh when something changes
return gulp.src('src/**/*.html')
.pipe(browserSync.reload({ stream: true }))
.on('error', gutil.log);
});
// Task to clean dist folder
gulp.task('clean', shell.task([
'rm -rf dist'
]));
// Task to compile your sass files
gulp.task('build-sass', function () {
return gulp.src(cssDependencies)
.pipe(sass({ outputStyle: 'compressed' }).on('error', sass.logError))
.pipe(concat('style.css'))
.pipe(gulp.dest(distPath + '/assets/styles'))
.pipe(browserSync.reload({ stream: true }));
});
// Watch changes in your files and start the browsersync server
gulp.task('watch', function () {
gulp.watch(['src/**/*.js'], ['build-js']);
gulp.watch(['src/assets/styles/**/*.css', 'src/assets/styles/**/*.scss'], ['build-sass']);
gulp.watch(['src/**/*.html'], ['copy-html']);
gulp.watch("src/**/*.html").on('change', browserSync.reload);
gulp.watch("src/**/*.js").on('change', browserSync.reload);
gulp.watch("src/**/*.css").on('change', browserSync.reload);
gulp.start('default');
});
// Build your app
gulp.task('build', ['copy-html', 'html', 'build-js', 'build-sass', 'copy-images', 'copy-resources', 'copy-fonts', 'copy-data']);
gulp.task('default', ['browserSync', 'build']);