-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
executable file
·296 lines (281 loc) · 8.44 KB
/
Copy pathgulpfile.js
File metadata and controls
executable file
·296 lines (281 loc) · 8.44 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// all the node modules we need
const gulp = require('gulp'),
sass = require('gulp-sass')(require('sass')),
sourcemaps = require('gulp-sourcemaps'),
autoprefixer = require('gulp-autoprefixer'),
babel = require('gulp-babel'),
changed = require('gulp-changed'),
imagemin = require('gulp-imagemin'),
htmllint = require('gulp-htmllint'),
fancyLog = require('fancy-log'),
colors = require('colors'),
plumber = require('gulp-plumber'),
gulpFn = require('gulp-fn'),
axios = require('axios'),
fs = require('fs'),
path = require('path'),
browserSync = require('browser-sync').create()
const errorHandler = function (err) {
notify.onError({
title: 'Gulp error in ' + err.plugin,
message: err.toString(),
})(err)
return this.emit('end')
}
// This task refreshes the browser if an HTML file changes
const filesForHtmlRefresh = [
'src/js/**/*.js',
'dist/js/**/*.js',
'*.html',
'!node_modules/*.*',
]
gulp.task('html-refresh', function (done) {
return gulp
.src(filesForHtmlRefresh, {
ignoreInitial: false,
})
.pipe(
gulpFn(function (file) {
browserSync.reload()
})
)
//done();
})
// This task checks HTML files for errors
const filesForHtmlCheck = ['*.html', '!node_modules/*.*']
gulp.task('html-check', function (done) {
return gulp
.src(filesForHtmlCheck, {
ignoreInitial: false,
})
.pipe(plumber())
.pipe(htmllint({}, htmllintReporter))
})
var htmllintReporter = function (filepath, issues) {
var filepathSplit = filepath.split('/')
filename = filepathSplit[filepathSplit.length - 1]
if (issues.length > 0) {
issues.forEach(function (issue) {
fancyLog(
'HTML Issue: ' +
filename +
', line ' +
issue.line +
': '.white +
issue.msg.red
)
})
process.exitCode = 1
} else {
console.log('HTML checked for errors: '.cyan)
console.log(filepath)
}
}
// This task takes SASS files in src and compiles them into CSS in dist
const filesForSassCompilation = [path.posix.join('src', 'scss', '**', '*.scss')]
gulp.task('sass-compile', (done) => {
return gulp
.src(filesForSassCompilation, { cwd: process.cwd() }) // run over these files
.pipe(plumber())
.pipe(sourcemaps.init()) // make sourcemaps for chrome devtools
.pipe(
sass({
// convert the sass into plain css
outputStyle: 'expanded', // change to 'expanded' to make it readable
indentType: 'tab',
indentWidth: 1,
})
)
.pipe(autoprefixer())
.pipe(sourcemaps.write('./')) // put the sourcemaps with the css files
.pipe(
gulp.dest((file) =>
file.base
.replace(path.sep + 'src', path.sep + 'dist')
.replace(path.sep + 'scss', path.sep + 'css')
)
) // put the css files here.
.pipe(browserSync.stream()) // tell browsersync to send over the changes
.pipe(
gulpFn(function (file) {
if (file.path.indexOf('.css.map') === -1) {
console.log('SCSS converted to CSS: '.cyan)
console.log(
file.path
.replace(path.sep + 'src', path.sep + 'dist')
.replace(path.sep + 'scss', path.sep + 'css')
)
}
})
)
})
// this task compiles modern (es6+/es2015+) JavaScript files in src and recompiles them
// into older, more broadly compatible (ES5) JavaScript files in dist
const filesForJsCompilation = [path.posix.join('src', 'js', '**', '*.js')]
gulp.task('js-compile', (done) => {
return (
gulp
.src(filesForJsCompilation, { cwd: process.cwd() }) // watch these files
// .pipe(eslint()) // uncomment for full JS styleguide/error checking
// .pipe(eslint.formatEach('pretty')) // uncomment for full JS styleguide/error checking
.pipe(plumber())
.pipe(sourcemaps.init()) // make sourcemaps for chrome devtools
.pipe(babel())
.on('error', function (err) {
console.warn('[JS Babel Error] '.red + err.message)
})
// .pipe(concat('./app.js')) // join all the js files into one // uncomment this line if you want to concatenate all JS files into one.
.pipe(sourcemaps.write('./')) // put the sourcemaps with the js files
.pipe(
gulp.dest((file) =>
file.base.replace(path.sep + 'src', path.sep + 'dist')
)
) // put the js files here.
.pipe(
gulpFn(function (file) {
if (file.path.indexOf('.js.map') === -1) {
console.log('JS generated: '.cyan)
console.log(
file.path.replace(path.sep + 'src', path.sep + 'dist')
)
}
})
)
)
})
// This task takes all images dropped in src and recompresses them, and puts them in dist
const filesForImageCompression = ['src/img/**/*.*']
gulp.task('image-compress', (done) => {
let imgDest = 'dist/img/'
return gulp
.src(filesForImageCompression) // watch these files
.pipe(plumber())
.pipe(changed((file) => file.base.replace('/src', '/dist')))
.pipe(imagemin())
.pipe(gulp.dest((file) => file.base.replace('/src', '/dist'))) // put the image files here.
.pipe(browserSync.stream()) // tell browsersync to send over the changes
.pipe(
gulpFn(function (file) {
console.log('Image compressed and copied to: '.cyan)
console.log(file.path.replace('/src', '/dist'))
})
)
})
// This task initializes the BrowserSync thing, which is what refreshes your page when you save a file
gulp.task('start-browsersync', function (done) {
browserSync.init({
// start the browsersync mini-server
server: './', // on the root of the project
})
done()
})
// This task says hi.
gulp.task('welcome', (done) => {
console.log(
colors.red(
'Starting Circus Starter template gulpfile! Wizz, whirrrrr, bang, pop!'
)
)
done()
})
// This task says make cool shit.
gulp.task('make-cool-shit', (done) => {
setTimeout(() => {
console.log(' ')
console.log(' ================================================ '.red)
console.log(' Welcome to the Circus Starter template. '.blue.bold)
console.log(" You're good to go. ".blue.bold)
console.log(' Make cool 💩. '.blue.bold)
console.log(' - Chris Silich '.blue.dim)
console.log(' ================================================ '.red)
console.log(' ')
done()
}, 500)
})
// This task checks what version of Circus Starter is on Github, and warns you if your version doesn't match.
gulp.task('version', (done) => {
// pay no attention to the man behind the curtain.
fs.readFile('package.json', 'utf8', function (err, data) {
if (err) {
console.warn(`Where's package.json?!?!?!`.red.bold.inverse)
done()
return false
}
let parsedPackage = JSON.parse(data)
let packagefileURL =
'https://raw.githubusercontent.com/CreativeCircus/circus-starter/master/package.json'
axios
.get(packagefileURL, {
responseType: 'json',
})
.then((response) => {
if (parsedPackage.version === response.data.version) {
console.log(`Circus Starter template appears to be up to date.`)
} else {
console.warn(`Local version is ${parsedPackage.version}.`.red.bold)
console.warn(`Remote version is ${response.data.version}.`.red.bold)
console.warn(
`Circus Starter template appears to be out of date.`.red.bold
.inverse
)
console.warn(
`If this is a new project, get a new copy. If it's an old project, consider updating for new gulpy goodness.`
.red.bold.inverse
)
}
done()
})
.catch((error) => {
console.log(`Couldn't fetch circus-starter mod date`, error)
done()
})
})
})
gulp.task(
'build',
gulp.parallel(
'html-refresh',
'html-check',
'image-compress',
'js-compile',
'sass-compile'
)
)
gulp.task('watch', function () {
const opts = {}
gulp.watch(filesForHtmlRefresh, opts, gulp.series(['html-refresh']))
gulp.watch(filesForHtmlCheck, opts, gulp.series(['html-check']))
gulp.watch(filesForJsCompilation, opts, gulp.series(['js-compile']))
gulp.watch(filesForSassCompilation, opts, gulp.series(['sass-compile']))
gulp.watch(filesForImageCompression, opts, gulp.series(['image-compress']))
})
// Running `gulp` runs this task. This task sort of branches off into the others as needed.
gulp.task(
'default',
gulp.series(
'welcome',
'start-browsersync',
'build',
'watch',
'make-cool-shit',
'version'
)
)
// Running `gulp` runs this task. This task sort of branches off into the others as needed.
// This version is just missing the BrowserSync stuff, for projects where that wouldn't work, like
// PHP and Wordpress projects.
gulp.task(
'no-browser-sync',
gulp.series(
'welcome',
gulp.parallel(
'html-refresh',
'html-check',
'image-compress',
'js-compile',
'sass-compile'
),
'make-cool-shit',
'version'
)
)