Skip to content

Add ignorePattern option for removing specific files #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ Note: image files such as `.png`, `.jpg` and `.gif` should not be gzipped, as th

*Default:* `'\*\*/\*.{js,css,json,ico,map,xml,txt,svg,eot,ttf,woff,woff2}'`

### ignorePattern

Files matching this pattern will *not* be gzipped even if they match filePattern

*Default:* null

### distDir

The root directory where the files matching `filePattern` will be searched for. By default, this option will use the `distDir` property of the deployment context, provided by [ember-cli-deploy-build][2].
Expand Down
20 changes: 14 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = {
name: options.name,
defaultConfig: {
filePattern: '**/*.{js,css,json,ico,map,xml,txt,svg,eot,ttf,woff,woff2}',
ignorePattern: null,
zopfli: false,
keep: false,
distDir: function(context){
Expand All @@ -44,13 +45,15 @@ module.exports = {
willUpload: function(context) {
var self = this;

var filePattern = this.readConfig('filePattern');
var distDir = this.readConfig('distDir');
var distFiles = this.readConfig('distFiles') || [];
var keep = this.readConfig('keep');
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');

this.log('gzipping `' + filePattern + '`', { verbose: true });
return this._gzipFiles(distDir, distFiles, filePattern, keep)
this.log('ignoring `' + ignorePattern + '`', { verbose: true });
return this._gzipFiles(distDir, distFiles, filePattern, ignorePattern, keep)
.then(function(gzippedFiles) {
self.log('gzipped ' + gzippedFiles.length + ' files ok', { verbose: true });
if (keep) {
Expand All @@ -64,8 +67,13 @@ module.exports = {
})
.catch(this._errorMessage.bind(this));
},
_gzipFiles: function(distDir, distFiles, filePattern, keep) {
_gzipFiles: function(distDir, distFiles, filePattern, ignorePattern, keep) {
var filesToGzip = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
if (ignorePattern != null) {
filesToGzip = filesToGzip.filter(function(path){
return !minimatch(path, ignorePattern, { matchBase: true });
});
}
return Promise.map(filesToGzip, this._gzipFile.bind(this, distDir, keep));
},
_gzipFile: function(distDir, keep, filePath) {
Expand Down
11 changes: 8 additions & 3 deletions tests/unit/index-nodetest.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,24 @@ describe('gzip plugin', function() {
return previous;
}, []);

assert.equal(messages.length, 5);
assert.equal(messages.length, 6);
});

it('adds default config to the config object', function() {
plugin.configure(context);
assert.isDefined(config.gzip.filePattern);
assert.isDefined(config.gzip.ignorePattern);
assert.isDefined(config.gzip.distDir);
assert.isDefined(config.gzip.distFiles);
assert.isDefined(config.gzip.zopfli);
});
});
describe('with a filePattern, zopfli, distDir, and distFiles provided', function () {
describe('with a filePattern, ignorePattern, zopfli, distDir, and distFiles provided', function () {
beforeEach(function() {
config = {
gzip: {
filePattern: '**/*.*',
ignorePattern: '**/specific.thing',
zopfli: false,
keep: false,
distDir: 'tmp/dist-deploy',
Expand Down Expand Up @@ -121,12 +123,14 @@ describe('gzip plugin', function() {
distFiles: [
'assets/foo.js',
'assets/bar.notjs',
'assets/ignore.js',
],
ui: mockUi,
project: { name: function() { return 'test-project'; } },
config: {
gzip: {
filePattern: '**/*.js',
ignorePattern: '**/ignore.*',
distDir: function(context){ return context.distDir; },
distFiles: function(context){ return context.distFiles; }
}
Expand All @@ -137,6 +141,7 @@ describe('gzip plugin', function() {
if (!fs.existsSync(path.join(context.distDir, 'assets'))) { fs.mkdirSync(path.join(context.distDir, 'assets')); }
fs.writeFileSync(path.join(context.distDir, context.distFiles[0]), 'alert("Hello foo world!");', 'utf8');
fs.writeFileSync(path.join(context.distDir, context.distFiles[1]), 'alert("Hello bar world!");', 'utf8');
fs.writeFileSync(path.join(context.distDir, context.distFiles[2]), 'alert("Hello ignore world!");', 'utf8');
plugin.beforeHook(context);
plugin.gzipLibrary = require('zlib');
});
Expand All @@ -145,7 +150,7 @@ describe('gzip plugin', function() {
return rimraf(context.distDir);
});

it('gzips the matching files', function(done) {
it('gzips the matching files which are not ignored', function(done) {
return assert.isFulfilled(plugin.willUpload(context))
.then(function(result) {
assert.deepEqual(result, { gzippedFiles: ['assets/foo.js'] });
Expand Down