-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
192 lines (172 loc) · 5.9 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
/* ---------------------------------------------------------------------------
SETUP
-----------------------------------------------------------------------------*/
var gulp = require( 'gulp' ),
sass = require( 'gulp-sass' ),
del = require( 'del' ),
imagemin = require( 'gulp-imagemin' ),
replace = require( 'gulp-replace' ),
concat = require( 'gulp-concat' ),
gutil = require( 'gulp-util' ),
lazypipe = require( 'lazypipe' ),
panini = require( 'panini' ),
sassJson = require( 'gulp-sass-json' ),
browser = require( 'browser-sync' ).create(),
// Optionally change the default path to the html files
default_html_file_path = './build/',
current_date = new Date().toLocaleString()
/* ---------------------------------------------------------------------------
GULP TASKS
-----------------------------------------------------------------------------*/
gulp.task( 'cleanBuild',
function() {
return clean( 'build' );
}
);
gulp.task( 'cleanBuild' ).description = "Clean old files from build folder";
gulp.task( 'cleanDist',
function() {
return clean( 'dist' );
}
);
gulp.task( 'cleanDist' ).description = "Clean old files from dist folder";
gulp.task( 'build',
gulp.series(
'cleanBuild',
json,
ie,
js,
pages,
scss,
images
)
);
gulp.task( 'build' ).description = "Build all files needed to serve up to the browser";
gulp.task( 'dist',
gulp.series(
'cleanDist',
json,
ie,
pagesDist,
scssDist,
imagesDist
)
);
gulp.task( 'dist' ).description = "Build all files for distribution and handoff";
gulp.task( 'default',
gulp.series(
'build',
server,
watch
)
);
gulp.task( 'default' ).description = "Build all files, start the server and watch for changes";
/* ---------------------------------------------------------------------------
REUSABLE FUNCTIONS
-----------------------------------------------------------------------------*/
// Reusable function to clean up files in build or dist folders
function clean( folder ) {
if( folder == 'dist' ) {
console.log( gutil.colors.red( 'Cleaning the ' + folder + ' folder...' ) );
return del([
'dist/**/*'
// Negate the pattern to ignore files (add a comma above)
//'!build/js/jquery.js.min'
]);
} else if( folder == 'build' ) {
console.log( gutil.colors.red( 'Cleaning the ' + folder + ' folder...' ) );
return del([
'build/**/*'
// Negate the pattern to ignore files (add a comma above)
//'!build/js/jquery.js.min'
]);
} else {
console.log( gutil.colors.red( 'Cannot clean the ' + folder + ' folder, permission denied.' ) );
// Need to figure out how to signal that the task is done in this case...
}
}
// Compile the SCSS for the build folder
function scss() {
return gulp.src( 'src/assets/scss/all.scss' )
.pipe( concat( 'style.scss/' ) )
.pipe( sass().on( 'error', sass.logError ) )
.pipe( gulp.dest( './build/assets/css/' ) );
};
// Compile the SCSS for the distribution folder
function scssDist() {
return gulp.src( 'src/assets/scss/all.scss' )
.pipe( concat( 'style.scss/' ) )
.pipe( sass().on( 'error', sass.logError ) )
.pipe( gulp.dest( './dist/assets/css/' ) );
};
// Convert Sass settings to JSON variables for Panini templates
function json() {
return gulp.src( 'src/assets/scss/settings.scss' )
.pipe( sassJson() )
.pipe( gulp.dest( 'src/data' ) );
}
// Move JavaScript iles from asset folder to build
function js() {
return gulp.src( 'src/assets/js/*' )
.pipe( gulp.dest( './build/assets/js/' ) );
}
// Move files from IE asset folder to build
function ie() {
return gulp.src( 'src/assets/ie/*' )
.pipe( gulp.dest( './build/assets/ie/' ) );
}
// Refresh Panini's cache of layouts and partials
function refresh( done ) {
panini.refresh();
done();
}
// Compile layouts, pages, and partials into flat HTML files with Panini
function pages() {
return gulp.src( 'src/pages/**/*.html' )
.pipe( panini({
root: 'src/pages',
layouts: 'src/layouts',
partials: 'src/partials',
helpers: 'src/helpers',
data: 'src/data'
}))
.pipe( gulp.dest( 'build' ) );
}
// Compile Panini templates into HTML + CSS handoff files
function pagesDist() {
return gulp.src( 'src/pages/**/*.html' )
.pipe( panini({
root: 'src/pages',
layouts: 'src/layouts',
partials: 'src/partials',
helpers: 'src/helpers',
data: 'src/data'
}))
.pipe( gulp.dest( 'dist' ) );
}
// Start a server with BrowserSync to preview the site
function server( done ) {
browser.init({
server: 'build'
});
done();
}
// Copy images from src to the build folder for BrowserSync server
function images() {
return gulp.src( 'src/assets/img/**/*' )
.pipe( gulp.dest( './build/assets/img' ) );
}
// Copy images from src to distribution folder and minify them
function imagesDist() {
return gulp.src( 'src/assets/img/**/*' )
.pipe( imagemin() )
.pipe( gulp.dest( './dist/assets/img' ) );
}
// Watch for file changes while BrowserSync is running
function watch() {
gulp.watch( 'src/pages/**/*.html' ).on( 'change', gulp.series( pages, browser.reload ) );
gulp.watch( ['src/layouts/**/*', 'src/partials/**/*'] ).on( 'change', gulp.series( refresh, pages, browser.reload ) );
gulp.watch( ['../scss/**/*.scss', 'src/assets/scss/**/*.scss'] ).on( 'change', gulp.series( refresh, json, scss, pages, browser.reload ) );
gulp.watch( 'src/assets/js/**/*' ).on( 'change', gulp.series( js, browser.reload ) );
gulp.watch( 'src/assets/img/**/*' ).on( 'change', gulp.series( images, browser.reload ) );
}