forked from web-standards-ru/web-standards.ru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
110 lines (95 loc) · 2.41 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
const babel = require('gulp-babel');
const buffer = require('vinyl-buffer');
const del = require('del');
const fs = require('fs');
const gulp = require('gulp');
const paths = require('vinyl-paths');
const postcss = require('gulp-postcss');
const replace = require('gulp-replace');
const rev = require('gulp-rev');
const rewrite = require('gulp-rev-rewrite');
const rollup = require('rollup-stream');
const source = require('vinyl-source-stream');
const terser = require('gulp-terser');
// Styles
gulp.task('styles', () => {
return gulp.src('dist/styles/{styles,print}.css')
.pipe(postcss([
require('postcss-import'),
require('postcss-color-hex-alpha'),
require('autoprefixer'),
require('postcss-csso'),
]))
.pipe(gulp.dest('dist/styles'));
});
// Scripts
gulp.task('scripts', function() {
return rollup({
input: 'dist/scripts/index.js',
format: 'iife',
})
.pipe(source('scripts.js'))
.pipe(buffer())
.pipe(babel({
presets: ['@babel/preset-env'],
}))
.pipe(terser())
.pipe(gulp.dest('dist'));
});
// Paths
gulp.task('paths', () => {
return gulp.src('dist/**/*.html')
.pipe(replace(
/(<script) type="module"( src="\/scripts)\/index(.js">)/, '$1$2$3'
))
.pipe(gulp.dest('dist'));
});
// Cache
gulp.task('cache:hash', () => {
return gulp.src([
'dist/fonts/*.woff2',
'dist/images/**/*.{svg,png,jpg}',
'dist/scripts.js',
'dist/styles/*.css',
'dist/manifest.json',
], {
base: 'dist',
})
.pipe(paths(del))
.pipe(rev())
.pipe(gulp.dest('dist'))
.pipe(rev.manifest('rev.json'))
.pipe(gulp.dest('dist'));
});
gulp.task('cache:replace', () => {
const manifest = fs.readFileSync('dist/rev.json');
return gulp.src([
'dist/**/*.{html,css}',
'dist/manifest-*.json',
])
.pipe(rewrite({
manifest,
}))
.pipe(gulp.dest('dist'));
});
gulp.task('cache', gulp.series(
'cache:hash',
'cache:replace'
));
// Clean
gulp.task('clean', () => {
return del([
'dist/styles/**/*',
'!dist/styles/{styles,print}-*.css',
'dist/scripts/**/*',
'dist/rev.json',
]);
});
// Build
gulp.task('build', gulp.series(
'styles',
'scripts',
'paths',
'cache',
'clean'
));