-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
207 lines (168 loc) · 5.61 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
'use strict';
const gulp = require('gulp');
const minifyCss = require('gulp-minify-css'); // css minification
// const sass = require('gulp-sass'); // sass transpille
const useref = require('gulp-useref'); // concat sources linked in index.html
const gulpif = require('gulp-if'); // if condition for pipes
const minifyHTML = require('gulp-minify-html'); // minify html
const image = require('gulp-image'); // image optimizer
const watch = require('gulp-watch'); // file watcher
const concat = require('gulp-concat'); // file concatenation
const bs = require('browser-sync').create(); // live reload server
const babel = require('gulp-babel'); // es6 -> es5
const argv = require('yargs').argv; // command line params
const uglify = require('gulp-uglify'); // js minification
const postcss = require('gulp-postcss'); // css post processor
const sourcemaps = require('gulp-sourcemaps'); // sourcemaps
const autoprefixer = require('gulp-autoprefixer'); // css autoprefixer
const rev = require('gulp-rev'); // revision master
const revReplace = require('gulp-rev-replace'); // replace file names from rev-manifest.json
const pug = require('gulp-pug'); // html template engine
const del = require('del'); // remove files/directories
const filter = require('gulp-filter');
const sources = {
js: [
'./src/**/*.js',
'!./src/bower_components/**',
'!./src/js/modules/**',
'!./src/**/*.spec.js',
'!./src/js/config/*',
'!./src/**/*.test.js'
],
rootFiles: [
"./src/manifest.json",
"./src/service-worker.js",
],
fonts: [
"./src/fonts/*"
],
langs: [
'./src/js/langs/**.json'
],
css: [
'./src/styles/**/*.pcss',
'!./src/bower_components/**/*'
]
};
const pathes = {
output: './dist'
};
const uglifyOpts = {
mangle: false,
compress: {
sequences: true, // join consecutive statemets with the “comma operator”
properties: false, // optimize property access: a['foo'] → a.foo
dead_code: true, // discard unreachable code
drop_debugger: true, // discard “debugger” statements
unsafe: false, // some unsafe optimizations (see below)
conditionals: true, // optimize if-s and conditional expressions
comparisons: false, // optimize comparisons
evaluate: false, // evaluate constant expressions
booleans: false, // optimize boolean expressions
loops: true, // optimize loops
unused: true, // drop unused variables/functions
hoist_funs: true, // hoist function declarations
hoist_vars: false, // hoist variable declarations
if_return: true, // optimize if-s followed by return/continue
join_vars: true, // join var declarations
cascade: true, // try to cascade `right` into `left` in sequences
side_effects: true, // drop side-effect-free statements
warnings: true, // warn about potentially dangerous optimizations/code
global_defs: {} // global definitions
}
};
gulp.task('revision', () =>
gulp.src(['dist/**/*.css', 'dist/**/*.js'], { base: process.cwd() + '/dist' })
.pipe(gulpif(argv.prod, rev()))
.pipe(gulp.dest(pathes.output))
.pipe(gulpif(argv.prod, rev.manifest()))
.pipe(gulp.dest(pathes.output))
);
gulp.task('rootFiles', () =>
gulp.src(sources.rootFiles)
.pipe(gulp.dest(pathes.output))
);
gulp.task('fonts', () =>
gulp.src(sources.fonts)
.pipe(gulp.dest(pathes.output + '/fonts'))
);
gulp.task('index', () => {
const manifest = argv.prod
? gulp.src(pathes.output + '/rev-manifest.json')
: null;
return gulp.src('./src/index.html', {base: process.cwd() + '/src'})
.pipe(useref())
.pipe(gulpif('*.js', gulpif(argv.prod, uglify())))
.pipe(gulpif('*.css', gulpif(argv.prod, minifyCss())))
.pipe(gulp.dest(pathes.output))
.pipe(gulpif(argv.prod, (revReplace({ manifest: manifest }))))
.pipe(gulp.dest(pathes.output));
});
gulp.task('scripts', () =>
gulp.src(sources.js)
.pipe(gulpif(!argv.prod, sourcemaps.init()))
.pipe(babel())
.pipe(concat('main.js'))
.pipe(gulpif(!argv.prod, sourcemaps.write()))
.pipe(gulpif(argv.prod, uglify(uglifyOpts)))
.pipe(gulp.dest(pathes.output + '/js'))
);
gulp.task('styles', () => {
const postcssFilter = filter(['*.pcss'], { restore: true });
return gulp.src(sources.css)
.pipe(gulpif(!argv.prod, sourcemaps.init()))
.pipe(postcss([require('postcss-nested')]))
.pipe(autoprefixer({ browsers: ['last 2 versions'], cascade: false }))
.pipe(concat('style.css'))
.pipe(gulpif(!argv.prod, sourcemaps.write()))
.pipe(gulp.dest(pathes.output + '/css'))
.pipe(gulpif(!argv.noreload, bs.stream()));
});
gulp.task('components', () =>
gulp.src(['./src/components/**/*.html', './src/components/**/*.pug'], {base: process.cwd() + '/src/components'})
.pipe(gulpif('*.pug', pug()))
.pipe(minifyHTML({
conditionals: true,
spare: true
}))
.pipe(gulp.dest('./dist/components'))
);
gulp.task('image', () =>
gulp.src('./src/images/**/*')
.pipe(gulp.dest('./dist/images/'))
);
var livereload = () => {
if (argv.noreload) {
return false;
}
bs.reload();
};
gulp.task('watch', () => {
bs.init({
server: {
baseDir: './dist/'
},
open: !argv.noreload
});
watch(sources.css, gulp.series('styles'));
watch(sources.rootFiles, gulp.series('rootFiles'));
watch(sources.fonts, gulp.series('fonts'));
watch(sources.js, gulp.series('scripts'));
watch(['./i/*'], gulp.series('image'));
watch(['./src/components/**/*.html', './src/components/**/*.pug', './src/*.html'], gulp.series('components')).on('change', livereload);
});
gulp.task('clean', () => del(pathes.output));
gulp.task('build',
gulp.series(
'clean',
'components',
'image',
'styles',
'scripts',
'revision',
'rootFiles',
'fonts',
'index'
)
);
gulp.task('serve', gulp.series('build', 'watch'));