-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
149 lines (134 loc) · 4.59 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
'use strict';
const gulp = require('gulp');
const runSequence = require('run-sequence');
const plumber = require('gulp-plumber');
const notify = require("gulp-notify");
const gutil = require('gulp-util');
const file = require('gulp-file');
const filter = require('gulp-filter');
const argv = require('yargs').default('production', false).argv;
const gulpif = require('gulp-if');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const jade = require('gulp-jade');
const less = require('gulp-less');
const autoprefixer = require('gulp-autoprefixer');
const cleanCss = require('gulp-clean-css');
const imagemin = require('gulp-imagemin');
const browserSync = require('browser-sync').create();
const path = require('path');
const swPrecache = require('sw-precache');
const del = require('del');
const gzip = require('gulp-gzip');
const sitemap = require('gulp-sitemap');
const SRC_DIR = 'src';
const DEST_PATH = 'public';
const JADE_ENTRY_PATH = path.join(SRC_DIR, 'jade/*.jade')
const JADE_PATH = path.join(SRC_DIR, 'jade/**/*.jade')
const LESS_PATH = path.join(SRC_DIR, 'less/**/*.less');
const LESS_ENTRY = path.join(SRC_DIR, 'less/nacar.less');
const IMG_PATH = path.join(SRC_DIR, 'img/**/*');
const JS_PATH = path.join(SRC_DIR, '**/*.js');
gulp.task('jade', function () {
return gulp.src(JADE_ENTRY_PATH)
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(jade({pretty: !argv.production}))
.pipe(gulp.dest(DEST_PATH))
.pipe(browserSync.stream({once: true}))
.pipe(sitemap({
siteUrl: 'https://www.merceria-nacar.es',
changefreq: 'monthly',
mappings: [{
pages: ['index.html'],
priority: 1,
},{
pages: ['news.html'],
priority: 0,
},{
pages: ['productos.html'],
priority: 0,
},{
pages: ['location.html'],
priority: 0.8,
},{
pages: ['contact.html'],
priority: 0.8,
}],
})).pipe(gulpif(argv.production, gulp.dest(DEST_PATH)))
.pipe(gulpif(argv.production, gzip()))
.pipe(gulpif(argv.production, gulp.dest(DEST_PATH)));;
});
gulp.task('less', function () {
return gulp.src(LESS_ENTRY)
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(gulpif(!argv.production, sourcemaps.init()))
.pipe(less())
.pipe(autoprefixer()).pipe(gulpif(argv.production, cleanCss({keepSpecialComments: 0})))
.pipe(gulpif(!argv.production, sourcemaps.write()))
.pipe(gulp.dest(DEST_PATH))
.pipe(browserSync.stream())
.pipe(gulpif(argv.production, gzip()))
.pipe(gulpif(argv.production, gulp.dest(DEST_PATH)));
});
gulp.task('img', function() {
return gulp.src(IMG_PATH)
.pipe(imagemin())
.pipe(gulp.dest(path.join(DEST_PATH, 'img')))
.pipe(browserSync.stream())
.pipe(filter(['**/*.svg']))
.pipe(gulpif(argv.production, gzip()))
.pipe(gulpif(argv.production, gulp.dest(path.join(DEST_PATH, 'img'))));
});
gulp.task('js', function () {
return gulp.src(JS_PATH)
.pipe(gulpif(argv.production, uglify()))
.pipe(gulp.dest(DEST_PATH))
.pipe(browserSync.stream())
.pipe(gulpif(argv.production, gzip()))
.pipe(gulpif(argv.production, gulp.dest(DEST_PATH)));
});
gulp.task('build-files', ['jade', 'less', 'img', 'js']);
gulp.task('clean', function () {
return del(path.join(DEST_PATH, '**/*'));
});
gulp.task('generate-service-worker:clean', function() {
return del(path.join(DEST_PATH, 'service-worker.js'));
});
gulp.task('generate-service-worker', ['generate-service-worker:clean'], function() {
return swPrecache.generate({
cacheId: 'nacar',
staticFileGlobs: [path.join(DEST_PATH, '**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff}')],
stripPrefix: DEST_PATH,
//dontCacheBustUrlsMatching: /./,
handleFetch: argv.production,
logger: gutil.log,
}).then((serviceWorkerString) => {
return file('service-worker.js', serviceWorkerString, {src: true})
.pipe(gulpif(argv.production, uglify()))
.pipe(gulp.dest(DEST_PATH))
.pipe(gulpif(argv.production, gzip()))
.pipe(gulpif(argv.production, gulp.dest(DEST_PATH)));
});
});
gulp.task('build', function(callback) {
runSequence('build-files', 'generate-service-worker', callback);
});
gulp.task('watch', ['build'], function () {
gulp.watch(JADE_PATH, ['jade', 'generate-service-worker']);
gulp.watch(LESS_PATH, ['less', 'generate-service-worker']);
gulp.watch(IMG_PATH, ['img', 'generate-service-worker']);
gulp.watch(JS_PATH, ['js', 'generate-service-worker']);
});
gulp.task('serve', ['watch'], function() {
browserSync.init({
server: {
baseDir: "./public",
serveStaticOptions: {
extensions: ["html"],
},
},
});
});
gulp.task('default', function(callback) {
runSequence('clean', 'build', callback);
});