-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
55 lines (46 loc) · 1.69 KB
/
main.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
const loaderUtils = require('loader-utils');
var ffmpeg = require('fluent-ffmpeg');
var through2 = require('through2');
var concat = require('concat-stream');
module.exports = function(content) {
const options = loaderUtils.getOptions(this);
const context = options.context || this.options.context;
var outputPattern = options.name || "[name]-[hash].[ext]";
this.cacheable();
var callback = this.async();
// stream to buffer
var makeOutput = (format, resolve)=>{
// arrow functions to preserve "this"
return concat((buf)=>{
var filename = loaderUtils.interpolateName(this, outputPattern.replace('[ext]', format), {context: context, content: content});
this.emitFile(filename, buf);
var stringToReturn = "__webpack_public_path__ + " + JSON.stringify(filename);
resolve(stringToReturn);
});
}
var doFfmpeg = (format) => {
return new Promise((resolve, reject)=>{
var input = through2();
input.end(content);
input.isStream = true; // hack, but seems to work
ffmpeg()
.on('error', reject)
.input(input)
.audioBitrate('128k')
.audioFrequency(44100)
.audioChannels(1)
.format(format)
.output(makeOutput(format, resolve))
.run();
});
}
Promise.all([doFfmpeg('mp3'), doFfmpeg('ogg')]).then(outputs=>{
var finaloutput = "module.exports = [" + outputs.join(',') + "];";
callback(null, finaloutput);
}).catch(error=>{
callback(error);
});
return;
};
module.exports.sourceMap = false;
module.exports.raw = true;