Skip to content
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

Use imagemin@5 pluggable configuration #8

Merged
merged 1 commit into from
Jun 3, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
language: node_js
node_js:
- "0.12"
- 4

sudo: false

Expand Down
27 changes: 12 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ Define options to be passed directly to `broccoli-imagemin`.
```js
var app = new EmberApp({
imagemin: {
interlaced: true,
optimizationLevel: 3,
progressive: true,
lossyPNG: false
plugins: [
require('imagemin-jpegtran')({ progressive: true }),
require('imagemin-optipng')({ optimizationLevel: 3 }),
require('imagemin-svgo')()
]
}
});
```

Read more about the options you may pass in on the [broccoli-imagemin](https://github.com/Xulai/broccoli-imagemin) page.
Note that with no plugins specified, nothing will be processed, disabling this addon.

Read more about the options you may pass in on the [broccoli--imagemin](https://github.com/kanongil/broccoli-imagemin) page.

### Enabled

Expand All @@ -42,18 +45,12 @@ Enable minification of images. Defaults to `true` in production environment, oth
```js
var app = new EmberApp({
imagemin: {
enabled: true
enabled: true,
plugins: [
require('imagemin-jpegtran')(),
]
}
});
```

Alternatively, you may simply set the `imagemin` key to a `Boolean` value as a shortcut to enable/disable. E.g.

```js
// Enable with default options
var app = new EmberApp({
imagemin: true
});
```

For more information on using ember-cli, visit [http://www.ember-cli.com/](http://www.ember-cli.com/).
8 changes: 7 additions & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ module.exports = function(defaults) {
var app = new EmberAddon(defaults, {
// Add options here
imagemin: {
enabled: true
enabled: true,
plugins: [
require('imagemin-gifsicle')(),
require('imagemin-jpegtran')(),
require('imagemin-optipng')(),
require('imagemin-svgo')()
]
}
});

Expand Down
28 changes: 10 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,22 @@ module.exports = {
included: function() {
this._super.included.apply(this, arguments);

// Default options
var defaultOptions = {
enabled: this.app.env === 'production'
};

// If the imagemin options key is set to `false` instead of
// an options object, disable the addon
if (typeof this.app.options.imagemin === 'boolean') {
this.options = this.app.options.imagemin = { enabled: this.app.options.imagemin };
} else {
this.options = this.app.options.imagemin = this.app.options.imagemin || {};
this.enabled = this.app.env === 'production'
this.options = this.app.options.imagemin = this.app.options.imagemin || {};

if ('enabled' in this.options) {
this.enabled = this.options.enabled;
delete this.options.enabled;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that rather than deleting the enabled option here, we should be explicit about which keys get passed to the imagemin constructor in the postprocessTree hook (line 27 below). This way, it's not an issue what people put in their ember-cli-build.js files, we always know imagemin will get passed valid options.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be redundant. Validation is already handled by broccoli-imagemin, which will throw an error if you pass any value in the enabled property.

}

// Merge the default options with the passed in options
for (var option in defaultOptions) {
if (!this.options.hasOwnProperty(option)) {
this.options[option] = defaultOptions[option];
}
if (!this.options.plugins || this.options.plugins.length === 0) {
this.enabled = false;
}
},

postprocessTree: function(type, tree) {
if (this.options.enabled) {
tree = imagemin(tree, this.options);
if (this.enabled) {
tree = new imagemin(tree, this.options);
}

return tree;
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"url": "https://github.com/andybluntish/ember-cli-imagemin/issues"
},
"engines": {
"node": ">= 0.10.0"
"node": ">= 4.0.0"
},
"author": "Andy Stanford-Bluntish <andy@bluntish.net>",
"license": "MIT",
Expand All @@ -43,6 +43,10 @@
"ember-load-initializers": "^0.5.0",
"ember-resolver": "^2.0.3",
"ember-try": "^0.1.2",
"imagemin-gifsicle": "^5.1.0",
"imagemin-jpegtran": "^5.0.2",
"imagemin-optipng": "^5.2.1",
"imagemin-svgo": "^5.2.0",
"loader.js": "^4.0.0"
},
"keywords": [
Expand All @@ -59,7 +63,7 @@
"svg"
],
"dependencies": {
"broccoli-imagemin": "^0.2.1",
"broccoli-imagemin": "^1.0.0",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! This was on my todo list—thanks!

"ember-cli-babel": "^5.1.5"
},
"ember-addon": {
Expand Down