-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
209 lines (181 loc) · 4.75 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
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
// Configuration
const devUrl = 'http://basic.local.com'; // The local development URL for BrowserSync
const enableTests = false; // Set to true to enable tests
// Gulp plugins
const browserSync = require('browser-sync').create();
const eslint = require('gulp-eslint');
const gulp = require('gulp');
const imagemin = require('gulp-imagemin');
const notify = require('gulp-notify');
const plumber = require('gulp-plumber');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const webpack = require('webpack-stream');
const zip = require('gulp-zip');
// Source Folders
const baseDir = 'src';
const imageFiles = baseDir + '/images/**/*.{png,gif,jpg}';
const jsFiles = baseDir + '/js/**/*.{js,jsx}';
const sassFiles = baseDir + '/scss/**/*.scss';
// Build Folders
const buildFolder = 'dist';
const buildCssFolder = buildFolder + '/css';
const buildImageFolder = buildFolder + '/images';
const buildJsFolder = buildFolder + '/js';
// Application State
const state = {
// Shouldn't try to minify JS if there are errors
shouldMinify: true
};
/**
* Handles errors with notifications
*/
const handleErrors = function () {
const args = Array.prototype.slice.call(arguments);
notify.onError({
title: '<%= error.name %>',
message: '<%= error.message %>'
}).apply(this, args);
};
/**
* Handles the deleting of watched files
* @param {object} event
*/
const fileDeleter = function (event) {
const del = require('del');
const path = require('path');
if (event.type === 'deleted') {
const filePathFromSrc = path.relative(path.resolve(baseDir), event.path);
const destFilePath = path.resolve(buildFolder, filePathFromSrc);
del.sync(destFilePath);
}
};
/**
* Lints the source
*/
gulp.task('eslint', function () {
state.shouldMinify = true;
return gulp.src([jsFiles])
.pipe(eslint())
.pipe(plumber())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
.on('error', notify.onError((args) => {
state.shouldMinify = false;
return handleErrors(args);
}));
});
/**
* Runs by default
*/
gulp.task('default', [
'scripts',
'copy',
'images',
'styles'
], () => {
browserSync.init({
proxy: devUrl,
notify: false
});
});
/**
* Runs by default
*/
gulp.task('build', [
'scripts',
'copy',
'images',
'styles'
]);
/**
* Compresses image files for production
*/
gulp.task('images', () => {
gulp.src(imageFiles)
.pipe(plumber({errorHandler: handleErrors}))
.pipe(imagemin())
.pipe(gulp.dest(buildImageFolder))
.pipe(browserSync.stream());
});
/**
* Minifies JS files for production
*/
gulp.task('scripts', ['eslint'], () => {
if (!state.shouldMinify) return gulp;
return gulp.src(baseDir + '/js/main.js')
.pipe(plumber({errorHandler: handleErrors}))
.pipe(webpack(require('./webpack.config.js')))
.pipe(gulp.dest(buildJsFolder))
.pipe(browserSync.stream());
});
/**
* Compiles SCSS to CSS and minifies CSS
*/
gulp.task('styles', () => {
gulp.src(sassFiles)
.pipe(plumber({errorHandler: handleErrors}))
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'compressed'
}))
.pipe(sourcemaps.write('./', {
includeContent: true,
sourceRoot: './'
}))
.pipe(gulp.dest(buildCssFolder))
.pipe(browserSync.stream());
});
/**
* Copy the html files to the build directory
*/
gulp.task('copy', function () {
var files = [
baseDir + '/**',
'!' + sassFiles,
'!' + imageFiles,
'!' + jsFiles
];
if (!enableTests) {
files.push('!' + baseDir + '/app/tests/**');
}
return gulp.src(files, { nodir: true, dot: true })
.pipe(plumber({errorHandler: handleErrors}))
.pipe(gulp.dest(buildFolder))
.pipe(browserSync.stream());
});
/**
* Watches for changes in files and does stuff
*/
gulp.task('watch', ['copy', 'images', 'styles', 'scripts'], () => {
const imageWatcher = gulp.watch([imageFiles], ['images']);
const copyWatcher = gulp.watch([
baseDir + '/**',
'!' + sassFiles,
'!' + imageFiles,
'!' + jsFiles
], { dot: true }, ['copy']);
gulp.watch([jsFiles], ['scripts']);
gulp.watch([sassFiles], ['styles']);
copyWatcher.on('change', fileDeleter);
imageWatcher.on('change', fileDeleter);
browserSync.init({
proxy: devUrl,
notify: false
});
});
gulp.task('cleanup', [], function () {
});
gulp.task('zip', ['copy', 'images', 'styles', 'scripts'], () => {
var today = new Date();
gulp.src('dist/**/*')
.pipe(zip(
today.getFullYear().toString() + "-" +
today.getMonth().toString() + "-" +
today.getDay().toString() + "_" +
today.getHours().toString() +
today.getMinutes().toString() +
'-dist.zip'
))
.pipe(gulp.dest('./'));
});