This repository has been archived by the owner on Mar 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.js
78 lines (68 loc) · 2.13 KB
/
images.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
import BaseRecipe from './baseRecipe'
import BrowserSync from 'browser-sync'
import debug from 'gulp-debug'
import extend from 'extend'
import gulpif from 'gulp-if'
import changed from 'gulp-changed'
import imagemin from 'gulp-imagemin'
import merge from 'merge-stream'
import path from 'path'
export const Default = {
debug: false,
presetType: 'images',
task: {
name: 'images'
},
watch: {
glob: '**',
options: {
//cwd: ** resolved from preset **
}
},
source: {
// baseDirectories: [] ** resolved from preset **
glob: '**',
options: {
//cwd: ** resolved from preset **
}
},
options: {}
}
const Images = class extends BaseRecipe {
/**
*
* @param gulp - gulp instance
* @param preset - base preset configuration - either one from preset.js or a custom hash
* @param configs - customized overrides for this recipe
*/
constructor(gulp, preset, ...configs) {
super(gulp, preset, Default, ...configs)
this.browserSync = BrowserSync.create()
}
createDescription() {
return `Minifies change images from ${this.config.source.options.cwd}/${this.config.source.glob}`
}
run(done, watching = false) {
var tasks = this.config.baseDirectories.map((baseDirectory) => {
// join the base dir with the relative cwd
return this.runOne(done, path.join(baseDirectory, this.config.source.options.cwd), watching)
})
return merge(tasks);
}
runOne(done, cwd, watching) {
// setup a run with a single cwd a.k.a base directory FIXME: perhaps this could be in the base recipe? or not?
let options = extend(true, {}, this.config.source.options)
options.cwd = cwd
this.debug(`src: ${cwd}/${this.config.source.glob}`)
return this.gulp.src(this.config.source.glob, options)
.pipe(changed(this.config.dest)) // ignore unchanged files
.pipe(gulpif(this.config.debug, debug(this.debugOptions())))
.pipe(imagemin(this.config.options))
.on('error', (error) => {
this.notifyError(error, done, watching)
})
.pipe(this.gulp.dest(this.config.dest))
.pipe(this.browserSync.stream())
}
}
export default Images