-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·158 lines (136 loc) · 5.11 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
// system
const fs = require( "fs" );
const path = require( "path" );
// parameters
const config = JSON.parse(
fs.readFileSync( path.join( process.cwd(), ".hugulprc" ) )
);
// common
const gulp = require( "gulp" );
const size = require( "gulp-size" );
const pump = require( "pump" );
// styles
const cleancss = require( "gulp-clean-css" );
// revision
const del = require( "del" );
const critical = require( "critical" ).stream;
// reference
const replace = require( "gulp-rev-replace" );
// minify html
const htmlmin = require( "gulp-htmlmin" );
gulp.task( "clean", function () {
return del( [ config.build.target ] );
} );
gulp.task( "assets:img", function () {
return gulp
.src( path.join( config.watch.source, config.path.img, "**", "*.*" ) )
.pipe( size( { title: "images: " } ) )
.pipe( gulp.dest( path.join( config.watch.target, config.path.img ) ) );
} );
gulp.task( "assets:scripts", function () {
return gulp
.src( path.join( config.watch.source, config.path.scripts, "**", "*.js" ) )
.pipe( size( { title: "scripts: " } ) )
.pipe( gulp.dest( path.join( config.watch.target, config.path.scripts ) ) );
} );
gulp.task( "watch:img", function () {
return gulp
.watch( path.join( config.watch.source, config.path.img, "**", "*.*" ), gulp.parallel( "assets:img" ) );
} );
gulp.task( "watch:scripts", function () {
return gulp
.watch( path.join( config.watch.source, config.path.scripts, "**", "*.js" ), gulp.parallel( "assets:scripts" ) );
} );
gulp.task( "watch:styles", function () {
return gulp
.watch( [
path.join( config.watch.source, config.path.styles, "**", "*.css" )
], gulp.parallel( "assets:styles" ) );
} );
gulp.task( "watch", gulp.parallel( "watch:scripts", "watch:styles", "watch:img" ) );
// .pipe(changed('staging/img'))
gulp.task( "images", function () {
return gulp
.src( [
path.join( config.build.source, "**", "*.png" ),
path.join( config.build.source, "**", "*.gif" ),
path.join( config.build.source, "**", "*.jpg" ),
path.join( config.build.source, "**", "*.jpeg" ),
path.join( config.build.source, "**", "*.svg" ),
] )
.pipe( gulp.dest( config.build.target ) ); // i.e.: public/images
} );
// default styles task
gulp.task( "styles", function ( cb ) {
return gulp
.src( path.join( config.build.source, "**", "*.css" ) )
.pipe( size( { title: "styles before: " } ) )
.pipe( cleancss() )
.pipe( size( { title: "styles: " } ) )
.pipe( gulp.dest( config.build.target ) );
} );
gulp.task( "scripts", function () {
return gulp
.src( path.join( config.build.source, "**", "*.js" ) )
//.pipe( uglify() )
.pipe( size( { title: "scripts: " } ) )
.pipe( gulp.dest( config.build.target ) );
} );
gulp.task( "html", function ( cb ) {
pump(
[
gulp.src( [
path.join( config.build.source, "**", "*.html" ),
path.join( config.build.source, "**", "*.xml" ),
path.join( config.build.source, "**", "*.ttf" ),
path.join( config.build.source, "**", "*.eot" ),
path.join( config.build.source, "**", "*.woff2" ),
path.join( config.build.source, "**", "*.woff" ),
path.join( config.build.source, "**", "*.svg" ),
] ),
size( { title: "html: " } ),
gulp.dest( config.build.target )
],
cb );
} );
gulp.task( "critical", function () {
return gulp
.src( [
path.join( config.build.target, "**", "*.html" )
] )
.pipe( critical( {
base: config.build.target,
inline: true,
minify: false,
css: [ path.join( config.build.target, "styles", "style.css" ) ],
dimensions: [ {
height: 498,
width: 280
} ],
ignore: [ "@font-face", "content", "font-family", "clearfix", "@import" ]
} )
.on( "error", console.log )
)
.pipe( size( { title: "html critical css: " } ) )
.pipe( gulp.dest( config.build.target ) );
} );
gulp.task( "minify", function () {
return gulp
.src( path.join( config.build.target, "**", "*.html" ) )
.pipe( htmlmin( {
collapseWhitespace: true,
collapseInlineTagWhitespace: false,
minifyCSS: true,
minifyJS: true,
removeComments: true,
removeEmptyAttributes: false,
removeEmptyElements: false,
removeRedundantAttributes: true,
useShortDoctype: true
} ) )
.pipe( size( { title: "html minified: " } ) )
.pipe( gulp.dest( config.build.target ) );
} );
const build = gulp.series( "clean", gulp.parallel( "images", "styles", "scripts", "html" ), "minify", "critical" );
gulp.task( "build", build );
gulp.task( "default", build );