-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.js
64 lines (54 loc) · 1.5 KB
/
loader.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
const _ = require('lodash')
const imagemin = require('imagemin')
const imageminWebp = require('imagemin-webp')
const validateOptions = require('schema-utils')
const { getOptions, interpolateName } = require('loader-utils')
const mime = require('mime-types')
const schema = {
type: 'object',
properties: {
name: {
type: 'string'
},
imageminWebOptions: {
type: 'object'
}
}
}
module.exports = function(source) {
const defaultOptions = {
name: '[path][name].[ext]',
context: this.context,
imageminWebOptions: {
quality: 90
}
}
const options = _.merge(defaultOptions, getOptions(this))
const callback = this.async()
validateOptions(schema, options, 'progressive-webp-loader')
const nameOptions = {
content: source,
context: options.context
}
const name = interpolateName(this, options.name, nameOptions)
const webpName = interpolateName(this, options.name.replace('[ext]', 'webp'), nameOptions)
const getFileInfo = filename => ({
src: filename,
type: mime.lookup(filename.split('?')[0])
})
imagemin
.buffer(source, {
plugins: imageminWebp(options.imageminWebOptions)
})
.then(webpSource => {
const data = {
original: getFileInfo(name, source),
webp: getFileInfo(webpName, webpSource)
}
this.emitFile(name, source)
this.emitFile(webpName, webpSource)
callback(null, `export default ${JSON.stringify(data)}`)
})
.catch(err => callback(err))
}
module.exports.raw = true