-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
112 lines (103 loc) · 4.07 KB
/
index.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
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
/*eslint-env node*/
'use strict';
const RSVP = require('rsvp');
const fs = require('fs');
const path = require('path');
const minimatch = require('minimatch');
const zlib = require('zlib');
const denodeify = require('rsvp').denodeify;
const renameFile = denodeify(fs.rename);
const DeployPluginBase = require('ember-cli-deploy-plugin');
module.exports = {
name: require('./package').name,
createDeployPlugin: function(options) {
var fs = require('fs');
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
filePattern: '**/*.{js,css,json,ico,map,xml,txt,svg,eot,ttf,woff,woff2}',
ignorePattern: null,
keep: false,
distDir: function(context){
return context.distDir;
},
distFiles: function(context){
return context.distFiles;
},
compressionQuality: 11
},
willUpload: function() {
var self = this;
var filePattern = this.readConfig('filePattern');
var ignorePattern = this.readConfig('ignorePattern');
var distDir = this.readConfig('distDir');
var distFiles = this.readConfig('distFiles') || [];
var keep = this.readConfig('keep');
var compressionQuality = this.readConfig('compressionQuality');
this.log('Compressing with brotli `' + filePattern + '`', { verbose: true });
this.log('ignoring `' + ignorePattern + '`', { verbose: true });
return this._compressedFiles(distDir, distFiles, filePattern, ignorePattern, keep, compressionQuality)
.then(function(brotliCompressedFiles) {
self.log('Compressed with brotli ' + brotliCompressedFiles.length + ' files ok', { verbose: true });
if (keep) {
self.log('keep is enabled, added brotli-compressed files to `context.distFiles`', { verbose: true });
return {
distFiles: [].concat(brotliCompressedFiles), // needs to be a copy
brotliCompressedFiles
};
} else {
return { brotliCompressedFiles };
}
})
.catch(this._errorMessage.bind(this));
},
_compressedFiles: function(distDir, distFiles, filePattern, ignorePattern, keep, compressionQuality) {
var filesToCompress = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
if (ignorePattern != null) {
filesToCompress = filesToCompress.filter(function(path){
return !minimatch(path, ignorePattern, { matchBase: true });
});
}
return RSVP.map(filesToCompress, this._compressFile.bind(this, distDir, keep, compressionQuality));
},
_compressFile: function(distDir, keep, compressionQuality, filePath) {
var self = this;
var fullPath = path.join(distDir, filePath);
var outFilePath = fullPath + '.br';
return new RSVP.Promise(function(resolve, reject) {
const brotli = zlib.createBrotliCompress({ params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: compressionQuality,
} });
var inp = fs.createReadStream(fullPath);
var out = fs.createWriteStream(outFilePath);
inp.pipe(brotli).pipe(out);
inp.on('error', function(err){
reject(err);
});
out.on('error', function(err){
reject(err);
});
out.on('finish', function(){
resolve(filePath + '.br');
});
}).then(function(){
if (!keep) {
return renameFile(fullPath + '.br', fullPath).then(function () {
return filePath;
});
} else {
return filePath + '.br';
}
}).then(function(outFilePath){
self.log('✔ ' + outFilePath, { verbose: true });
return outFilePath;
});
},
_errorMessage: function(error) {
this.log(error, { color: 'red' });
return RSVP.reject(error);
}
});
return new DeployPlugin();
}
};