-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
304 lines (279 loc) · 9.15 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
'use strict';
//####################################
// Lists of Gulp Plugins
//####################################
const gulp = require('gulp');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const sourcemaps = require('gulp-sourcemaps');
const concat = require('gulp-concat');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const pump = require('pump');
const rename = require('gulp-rename');
const htmlmin = require('gulp-htmlmin');
const bsync = require('browser-sync').create();
const imagemin = require('gulp-imagemin');
const estream = require('event-stream');
const cssmin = require('gulp-cssmin');
const htmlreplace = require('gulp-html-replace');
const runsequence = require('run-sequence');
const clean = require('gulp-clean');
const fs = require('fs');
const yargs = require('yargs').argv;
const gulpif = require('gulp-if');
const sprity = require('sprity');
//####################################
// List of Gulp tasks
//
// You can edit tasks names. For
// example: by default there's a css
// task, but you can exchange the name
// as you want. You could call it as
// styles, and you use it like:
//
// gulp styles
//####################################
const tasks = {
sprites: 'sprites',
css: 'css',
js_concat: 'js',
js_uglify: 'compress',
html: 'views',
html_replace: 'htmlreplace',
watch: 'watch',
server: 'server',
images: 'images',
production: 'production',
clean: 'clean'
};
//####################################
// List of Gulp paths
//
// You can rename the project folders
// as you want. For example, you could
// call the production folder as 'build'.
//
// You can also exchange the working
// project folders. For example, you
// can call your js folders as
// 'JavaScripts' instead of 'js'
//####################################
const project_dist = 'www';
const project_src = 'app';
const paths = {
// JavaScripts
scripts: {
dest: `${project_dist}/js`,
origin: {
internal_root: `${project_src}/scripts`,
internal: [
`${project_src}/scripts/countdown.js`,
`${project_src}/scripts/smooth-scroll.js`,
`${project_src}/scripts/script.js`
],
external: [
]
}
},
// Styles (SASS/CSS)
styles: {
dest: `${project_dist}/css`,
origin: {
// SASS files
internal: [
`${project_src}/styles/style.{scss,sass}`
],
// CSS files
external: [
]
},
origin_root: `${project_src}/styles`
},
// Views
views: {
dest: project_dist,
origin: `${project_src}/**/*.{html,php}`
},
// Images to be minified
images: {
dest: `${project_dist}/images`,
origin: `${project_src}/images/**/*`,
sprites_origins: `${project_src}/images/sprites/**/*`,
},
// Folders and files to be cleaned after development
to_be_cleanded: [
project_dist,
'node_modules'
]
}
//####################################
// List of Gulp tasks
//
// There's some gulp tasks to work
// with code concatenation, uglification,
// replaces and a lot of other possibilities
//####################################
gulp.task(tasks.css, () => {
const cssStream = gulp.src(paths.styles.origin.external);
const sassStream = gulp.src(paths.styles.origin.internal)
.pipe(sass({
outputStyle: 'compressed'
}))
.on('error', sass.logError);
return estream.merge(cssStream, sassStream)
.pipe(sourcemaps.init())
.pipe(concat('style.css'))
.pipe(autoprefixer())
.pipe(cssmin())
.pipe(sourcemaps.write('.'))
.pipe(bsync.stream({match: '**/*.css'}))
.pipe(gulp.dest(paths.styles.dest))
.on('end', () => console.log(`SASS files has been concatenated and minifed`));
});
gulp.task(tasks.js_concat, () => {
return gulp.src(paths.scripts.origin.external.concat(paths.scripts.origin.internal))
.pipe(sourcemaps.init())
.pipe(concat('script.js'))
.pipe(babel({
presets: ['es2015']
}))
.pipe(sourcemaps.write('.'))
.pipe(bsync.stream({match: '**/*.js'}))
.pipe(gulp.dest(paths.scripts.dest))
.on('end', () => console.log(`Listed origin and external JavaScript files has been concatenated`));
});
gulp.task(tasks.js_uglify, cb => {
runsequence(tasks.js_concat, () => {
const options = {
preserveComments: 'license',
compress: {
drop_console: true
}
};
pump([
gulp.src(paths.scripts.dest + '/script.js'),
uglify(options),
rename('script.min.js'),
gulp.dest(paths.scripts.dest)
], cb)
.on('end', () => console.log(`${paths.scripts.dest}/script.js has been minified`));
});
});
gulp.task(tasks.html, () => {
return gulp.src(paths.views.origin)
.pipe(htmlmin({
collapseWhitespace: true,
removeComments: true,
ignoreCustomComments: [
/build:[a-zA-Z]{1,}/,
/endbuild/,
]
}))
.pipe(bsync.stream({match: '**/*.{html,php}'}))
.pipe(gulp.dest(paths.views.dest))
.on('end', () => console.log(`${paths.views.origin} has been minified`));
});
gulp.task(tasks.watch, () => {
gulp.watch(paths.scripts.origin.internal_root + '/**/*.js', [tasks.js_concat]);
gulp.watch(paths.styles.origin_root + '/**/*.{sass,scss}', [tasks.css]);
gulp.watch(paths.views.origin, [tasks.html]);
});
gulp.task(tasks.server, () => {
bsync.init({
server: {
baseDir: `${project_dist}/`
}
});
gulp.watch(paths.scripts.origin.internal_root + '/**/*.js', [tasks.js_concat]).on('change', bsync.reload);
gulp.watch(paths.styles.origin_root + '/**/*.scss', [tasks.css]).on('change', bsync.reload);
gulp.watch(paths.views.origin, [tasks.html]).on('change', bsync.reload);
});
gulp.task(tasks.images, () => {
return gulp.src(paths.images.origin)
.pipe(imagemin([
imagemin.gifsicle({
interlaced: true,
optimizationLevel: 1 // Minimum 1 and Maximum 3
}),
imagemin.jpegtran({
progressive: true
}),
imagemin.optipng({
optimizationLevel: 5 // Minimum 0 and Maximum 7
}),
imagemin.svgo({
plugins: [
{removeViewBox: true}
]
})
]))
.pipe(gulp.dest(paths.images.dest))
.on('end', () => console.log(`${paths.images.dest} has now optimizated images from ${paths.images.origin}`));
});
gulp.task(tasks.sprites, () => {
return sprity.src({
src: `${paths.images.sprites_origins}.{png,jpg}`,
style: `./sprites.scss`,
processor: 'sass'
}).pipe(gulpif(
'*.png',
gulp.dest(paths.images.dest),
gulp.dest(paths.styles.origin_root)
)).on('end', () => {
console.log(`${paths.images.dest}/sprite.png has been created`);
console.log(`${paths.styles.origin_root}/sprites.scss has been created`);
})
});
gulp.task(tasks.html_replace, () => {
runsequence(tasks.html, () => {
let css = `${paths.styles.dest}/style.css`;
let js = `${paths.scripts.dest}/script.js`;
// Use gulp htmlreplace --xcss
if(yargs.xcss) {
// Take the content of CSS file and rip off all the "../"
// references to refer from the index.html
css = fs.readFileSync(css, 'utf8').split('../').join('');
css = `<style>${css}</style>`;
console.log('Exchanged the CSS reference for the stylesheet content');
}
// Use gulp htmlreplace --xjs
//
// You can use like: gulp htmlreplace --xjs --xcss
// to apply css and javascript replacement
if(yargs.xjs) {
//Take the content of JavaScript concatenated file and put it direct on body of index.html
js = fs.readFileSync(js, 'utf8');
js = `<script>${js}</script>`;
console.log('Exchanged the JS reference for the script content');
}
// It takes all html/php files and apply the html
// replacement. It's important to say that all the
// files will have the same reference/content for
// CSS or JavaScript.
gulp.src(`${project_dist}/**/*.{html,php}`)
.pipe(htmlreplace({
'js': js,
'css': css
}))
.pipe(gulp.dest(`${project_dist}/`))
.on('end', () => {
console.log('HTML replacement is done');
});
});
});
gulp.task(tasks.production, () => {
runsequence(
tasks.images,
tasks.js_uglify,
tasks.css,
tasks.html_replace,
() => {
console.log('The production task has finished.');
}
);
});
gulp.task(tasks.clean, () => {
return gulp.src(paths.to_be_cleanded)
.pipe(clean({ force: true }));
});