Skip to content

Commit 25772b4

Browse files
committed
Allow opt-in use of zopfli for compression
See https://en.wikipedia.org/wiki/Zopfli
1 parent debe59b commit 25772b4

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ The Array of built assets.
3636

3737
_Default:_ the `distFiles` property of the deployment context
3838

39+
### zopfli
40+
41+
Use node-zopfli for compression (better than regular gzip
42+
compression, but slower). If you set this to `true`, you
43+
will need to `npm install node-zopfli --save-dev` in your app.
44+
45+
_Default:_ false
46+
3947
## Prequisites
4048

4149
The default configuration of this plugin expects the deployment context to have `distDir` and `distFiles` properties. These are conveniently created by the [ember-cli-deploy-build](https://github.com/zapnito/ember-cli-deploy-build) plugin so will work out of the box if you are using that plugin.

index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ module.exports = {
1515
name: 'ember-cli-deploy-gzip',
1616

1717
createDeployPlugin: function(options) {
18-
var zlib = require('zlib');
1918
var fs = require('fs');
2019

2120
var DeployPlugin = DeployPluginBase.extend({
2221
name: options.name,
2322
defaultConfig: {
2423
filePattern: '**/*.{js,css,png,gif,jpg,map,xml,txt,svg,eot,ttf,woff,woff2}',
24+
zopfli: false,
2525
distDir: function(context){
2626
return context.distDir;
2727
},
@@ -30,6 +30,16 @@ module.exports = {
3030
}
3131
},
3232

33+
configure: function(context) {
34+
this._super.configure.call(this, context);
35+
if (this.readConfig('zopfli')) {
36+
this.log("Using zopfli for compression")
37+
this.gzipLibrary = this.project.require('node-zopfli');
38+
} else {
39+
this.gzipLibrary = require('zlib');
40+
}
41+
},
42+
3343
willUpload: function(context) {
3444
var self = this;
3545

@@ -45,15 +55,15 @@ module.exports = {
4555
})
4656
.catch(this._errorMessage.bind(this));
4757
},
48-
_gzipFiles: function(distDir, distFiles, filePattern) {
58+
_gzipFiles: function(distDir, distFiles, filePattern, gzipLibrary) {
4959
var filesToGzip = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
5060
return Promise.map(filesToGzip, this._gzipFileInPlace.bind(this, distDir));
5161
},
5262
_gzipFileInPlace: function(distDir, filePath) {
5363
var self = this;
5464
var fullPath = path.join(distDir, filePath);
5565
return new Promise(function(resolve, reject) {
56-
var gzip = zlib.createGzip();
66+
var gzip = self.gzipLibrary.createGzip({ format: 'gzip' });
5767
var inp = fs.createReadStream(fullPath);
5868
var out = fs.createWriteStream(fullPath + '.gz');
5969

tests/unit/index-nodetest.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,23 @@ describe('gzip plugin', function() {
6161
return previous;
6262
}, []);
6363

64-
assert.equal(messages.length, 3);
64+
assert.equal(messages.length, 4);
6565
});
6666

6767
it('adds default config to the config object', function() {
6868
plugin.configure(context);
6969
assert.isDefined(config.gzip.filePattern);
7070
assert.isDefined(config.gzip.distDir);
7171
assert.isDefined(config.gzip.distFiles);
72+
assert.isDefined(config.gzip.zopfli);
7273
});
7374
});
74-
describe('with a filePattern, distDir, and distFiles provided', function () {
75+
describe('with a filePattern, zopfli, distDir, and distFiles provided', function () {
7576
beforeEach(function() {
7677
config = {
7778
gzip: {
7879
filePattern: '**/*.*',
80+
zopfli: false,
7981
distDir: 'tmp/dist-deploy',
8082
distFiles: []
8183
}
@@ -87,6 +89,7 @@ describe('gzip plugin', function() {
8789
ui: mockUi,
8890
config: config
8991
};
92+
debugger;
9093
plugin.beforeHook(context);
9194
});
9295
it('does not warn about missing optional config', function() {
@@ -98,6 +101,7 @@ describe('gzip plugin', function() {
98101

99102
return previous;
100103
}, []);
104+
console.log(messages);
101105
assert.equal(messages.length, 0);
102106
});
103107
});
@@ -134,6 +138,7 @@ describe('gzip plugin', function() {
134138
fs.writeFileSync(path.join(context.distDir, context.distFiles[0]), 'alert("Hello foo world!");', 'utf8');
135139
fs.writeFileSync(path.join(context.distDir, context.distFiles[1]), 'alert("Hello bar world!");', 'utf8');
136140
plugin.beforeHook(context);
141+
plugin.gzipLibrary = require('zlib');
137142
});
138143

139144
afterEach(function(){

0 commit comments

Comments
 (0)