Newer
Older
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
208
209
210
'use strict';
var importOnce = require('node-sass-import-once'),
path = require('path');
var options = {};
// #############################
// Edit these paths and options.
// #############################
// The root paths are used to construct all the other paths in this
// configuration. The "project" root path is where this gulpfile.js is located.
options.rootPath = {
project : __dirname + '/',
app : __dirname + '/opentech/',
theme : __dirname + '/opentech/static_src/'
};
options.theme = {
root : options.rootPath.theme,
sass : options.rootPath.theme + 'src/sass/',
js : options.rootPath.theme + 'src/javascript/',
img : options.rootPath.theme + 'src/images/',
font : options.rootPath.theme + 'src/fonts/',
dest : options.rootPath.app + 'static_compiled/',
css : options.rootPath.app + 'static_compiled/css/',
js_dest : options.rootPath.app + 'static_compiled/js/',
img_dest : options.rootPath.app + 'static_compiled/images/',
font_dest : options.rootPath.app + 'static_compiled/fonts/'
};
// Define the node-sass configuration. The includePaths is critical!
options.sass = {
importer: importOnce,
includePaths: [
options.theme.sass,
],
outputStyle: 'expanded'
};
// Define the paths to the JS files to lint.
options.eslint = {
files : [
options.theme.js + '**/*.js',
'!' + options.theme.js + '**/*.min.js'
]
};
// If your files are on a network share, you may want to turn on polling for
// Gulp watch. Since polling is less efficient, we disable polling by default.
options.gulpWatchOptions = {interval: 600};
// options.gulpWatchOptions = {interval: 1000, mode: 'poll'};
// Load Gulp and tools we will use.
var gulp = require('gulp'),
$ = require('gulp-load-plugins')(),
del = require('del'),
// gulp-load-plugins will report "undefined" error unless you load gulp-sass manually.
sass = require('gulp-sass'),
cleanCSS = require('gulp-clean-css');
// The sass files to process.
var sassFiles = [
options.theme.sass + '**/*.scss',
// Do not open Sass partials as they will be included as needed.
'!' + options.theme.sass + '**/_*.scss'
];
// Clean CSS files.
gulp.task('clean:css', function clean () {
return del([
options.theme.css + '**/*.css',
options.theme.css + '**/*.map'
], {force: true});
});
// Clean JavaScript files.
gulp.task('clean:js', function clean () {
return del([
options.theme.js_dest + '**/*.js',
options.theme.js_dest + '**/*.map'
], {force: true});
});
// Clean all directories.
gulp.task('clean', gulp.parallel('clean:css', 'clean:js'));
// Lint JavaScript.
gulp.task('lint:js', function lint () {
return gulp.src(options.eslint.files)
.pipe($.eslint())
.pipe($.eslint.format());
});
// Lint JavaScript and throw an error for a CI to catch.
gulp.task('lint:js-with-fail', function lint () {
return gulp.src(options.eslint.files)
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.eslint.failOnError());
});
// Lint Sass.
gulp.task('lint:sass', function lint () {
return gulp.src(options.theme.sass + '**/*.scss')
.pipe($.sassLint())
.pipe($.sassLint.format());
});
// Lint Sass and throw an error for a CI to catch.
gulp.task('lint:sass-with-fail', function lint () {
return gulp.src(options.theme.sass + '**/*.scss')
.pipe($.sassLint())
.pipe($.sassLint.format())
.pipe($.sassLint.failOnError());
});
// Lint Sass and JavaScript.
gulp.task('lint', gulp.parallel('lint:sass', 'lint:js'));
// Build CSS.
gulp.task('styles', gulp.series('clean:css', function css () {
return gulp.src(sassFiles)
.pipe($.sourcemaps.init())
.pipe(sass(options.sass).on('error', sass.logError))
.pipe($.size({showFiles: true}))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(options.theme.css));
}));
gulp.task('styles:production', gulp.series('clean:css', function css () {
return gulp.src(sassFiles)
.pipe(sass(options.sass).on('error', sass.logError))
.pipe(cleanCSS({rebase: false}))
.pipe($.size({showFiles: true}))
.pipe(gulp.dest(options.theme.css));
}));
// Build JavaScript.
gulp.task('scripts', gulp.series('clean:js', function js () {
return gulp.src(options.theme.js + '**/*.js')
.pipe($.sourcemaps.init())
.pipe($.babel({presets: ['@babel/env']}))
.pipe($.size({showFiles: true}))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(options.theme.js_dest));
}));
// Build JavaScript.
gulp.task('scripts:production', gulp.series('clean:js', function js () {
return gulp.src(options.theme.js + '**/*.js')
.pipe($.babel({presets: ['@babel/env']}))
.pipe($.uglify())
.pipe($.size({showFiles: true}))
.pipe(gulp.dest(options.theme.js_dest));
}));
// Copy images.
gulp.task('images', function copy () {
return gulp.src(options.theme.img + '**/*.*').pipe(gulp.dest(options.theme.img_dest));
});
// Copy fonts.
gulp.task('fonts', function copy () {
return gulp.src(options.theme.font + '**/*.*').pipe(gulp.dest(options.theme.font_dest));
});
// Watch for changes and rebuild.
gulp.task('watch:css', gulp.series('styles', function watch () {
return gulp.watch(options.theme.sass + '**/*.scss', options.gulpWatchOptions, gulp.series('styles'));
}));
gulp.task('watch:lint:sass', gulp.series('lint:sass', function watch () {
return gulp.watch(options.theme.sass + '**/*.scss', options.gulpWatchOptions, gulp.series('lint:sass'));
}));
gulp.task('watch:lint:js', gulp.series('lint:js', function watch () {
return gulp.watch(options.eslint.files, options.gulpWatchOptions, gulp.series('lint:js'));
}));
gulp.task('watch:js', gulp.series('scripts', function watch () {
return gulp.watch(options.eslint.files, options.gulpWatchOptions, gulp.series('scripts'));
}));
gulp.task('watch:images', gulp.series('images', function watch () {
return gulp.watch(options.theme.img + '**/*.*', options.gulpWatchOptions, gulp.series('images'));
}));
gulp.task('watch:fonts', gulp.series('fonts', function watch () {
return gulp.watch(options.theme.font + '**/*.*', options.gulpWatchOptions, gulp.series('fonts'));
}));
gulp.task('watch', gulp.parallel('watch:css', 'watch:lint:sass', 'watch:js', 'watch:lint:js', 'watch:images', 'watch:fonts'));
// Build everything.
gulp.task('build', gulp.parallel('styles:production', 'scripts:production', 'images', 'fonts', 'lint'));
// Deploy everything.
gulp.task('deploy', gulp.parallel('styles:production', 'scripts:production', 'images', 'fonts'));
// The default task.
gulp.task('default', gulp.series('build'));
// Resources used to create this gulpfile.js:
// - https://github.com/google/web-starter-kit/blob/master/gulpfile.babel.js
// - https://github.com/dlmanning/gulp-sass/blob/master/README.md