Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Imagemin middleware #459

Merged
merged 17 commits into from Nov 28, 2017
Merged

Conversation

timkelty
Copy link
Contributor

Started here: #446

This seemed better suited for a middleware, so that we can apply a single set of imagemin options to both loader assets, as well as any assets written from other plugins (eg copy-webpack-plugin).

@@ -0,0 +1,46 @@
const merge = require('deepmerge');

const imageminLoader = require.resolve('imagemin-webpack/imagemin-loader');
Copy link
Member

Choose a reason for hiding this comment

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

Group the requires together, and move the resolve to its own grouping:

const merge = require('deepmerge');
const ImageminWebpackPlugin = require('imagemin-webpack/ImageminWebpackPlugin');
const gifsicle = require('imagemin-gifsicle');
const svgo = require('imagemin-svgo');
const pngquant = require('imagemin-pngquant');
const mozjpeg = require('imagemin-mozjpeg');
const webp = require('imagemin-webp');

const imageminLoader = require.resolve('imagemin-webpack/imagemin-loader');

neutrino.use(chunk);
neutrino.use(minify);

if (options.imagemin) {
Copy link
Member

Choose a reason for hiding this comment

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

Is the goal to switch this to neutrino.options.optimize when it's available?

Copy link
Contributor Author

@timkelty timkelty Nov 21, 2017

Choose a reason for hiding this comment

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

Yes, but also to pass options if desired, or disable imagemin by passing false.

@timkelty
Copy link
Contributor Author

@eliperelman I rebased this off master.

Note: When this is merged into master, there will be a conflict that needs to resolved, as I've moved the .when(process.env.NODE_ENV === 'production', () => { below the .when(neutrino.options.command === 'build', (config) => {.

This is because we need the imagemin plugin to be applied after the @neutrinojs/copy stuff, so those images are optimized as well.

"image",
"imagemin"
],
"author": "Eli Perelman <eli@eliperelman.com>",
Copy link
Member

Choose a reason for hiding this comment

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

Feel free to put your information here since you wrote this package. 😃

@timkelty
Copy link
Contributor Author

Ok, made those change, plus rebased off master again to mitigate the above mentioned conflicts.

],
"author": "Tim Kelty <timkelty@gmail.com>",
"license": "MPL-2.0",
"repository": "https://github.com/mozilla-neutrino/neutrino-dev/tree/master/packages/imagemin",
Copy link
Member

Choose a reason for hiding this comment

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

Does this repo format actually work on npm?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm not sure - I was just following suit with the rest of the middlewares: https://github.com/mozilla-neutrino/neutrino-dev/blob/master/packages/web/package.json#L14

Copy link
Member

Choose a reason for hiding this comment

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

wtf. When I was viewing this diff, it showed a different format...don't mind me.

@edmorley
Copy link
Member

Do we know how deterministic the output images files are after being run though minification? It occurred to me that perhaps the process might not be deterministic and thus cause constant hash churn.

Continuing on from #446:

Should people instead be minimizing images once in the repo rather than at build time?

good question. All my experiences with Neutrino involve gitignoring the build folder, and building as part of the deploy process, so that doesn't really apply for me.

Furthermore, I always want my source assets pre-optimized, so I wouldn't want to commit optimized versions there.

To clarify, I don't mean store the Neutrino build directory in the repo. I mean outside the Neutrino build optimise your source directory assets once and commit them, to save the build time penalty every time. I agree one wouldn't want to lose the original Photoshop document or whatever, but normally that would be separate from the file being minimised anyway?

@edmorley
Copy link
Member

I think this is a great middleware, and I'll probably end up using it myself in certain projects. I'm just trying to decide whether it would be best for it to be opt-in or opt-out. Knowing impact on build times and image sizes would with calculating a cost-benefit.

@timkelty
Copy link
Contributor Author

timkelty commented Nov 21, 2017

good points, @edmorley.

Do we know how deterministic the output images files are after being run though minification? It occurred to me that perhaps the process might not be deterministic and thus cause constant hash churn.

Not certain. In my experiences the hash's have been the same, but I don't know for sure.

I'm just trying to decide whether it would be best for it to be opt-in or opt-out

I'd be fine with defaulting options.imagemin to false. This might also mitigate the potential heft added if people had huge images and weren't expecting it to exponentially affect their build time, and also would not assume that users weren't pre-optimizing their assets.

@eliperelman
Copy link
Member

If we end up making this opt-in instead of opt-out, then there is no value in embedding this into the web middleware. They should just manually add it to their uses:

module.exports = {
  use: [
    '@neutrinojs/react',
    '@neutrinojs/imagemin'
  ]
};

@timkelty
Copy link
Contributor Author

@eliperelman good point…

I'm fine either way. Maybe leave it out of web until we have a better idea how it will impact builds?

@timkelty
Copy link
Contributor Author

timkelty commented Nov 21, 2017

@eliperelman @edmorley my most recent commit on this PR removes the middleware from web.

Reasoning:

  • there are some fairly heavy deps involved (imagemin-mozjpeg, etc), that I could see people being annoyed with having to install if they didn't want neutrino to optimize images
  • The plugin needs to be included AFTER any other plugins that add images to the manifest. Having it separate allows users to manage that themselves.
  • The above mentioned concerns about build times and deterministic file names

@edmorley
Copy link
Member

Sounds good - we can always re-evaluate in the future depending on popularity :-)

module.exports = {
use: [
['@neutrinojs/imagemin', {
imagemin: {},
Copy link
Member

Choose a reason for hiding this comment

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

Indentation here seems off.

## Customization

`@neutrinojs/imagemin` creates some conventions to make overriding the configuration easier once you are
ready to make changes.
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we are missing the table of conventions here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a Rules and Plugins table.

@@ -192,5 +182,15 @@ module.exports = (neutrino, opts = {}) => {
}

config.output.filename('[name].[chunkhash].js');
})
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this was moved without modification, so we should probably reset this file to what is in master.

module.exports = {
use: [
['@neutrinojs/imagemin', {
imagemin: {},
Copy link
Member

Choose a reason for hiding this comment

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

Heh, looks like we may have some mixed tabs in here. Make each indentation here 2 spaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh dear…sorry in the middle of changing-editors-hell 🥇

"homepage": "https://neutrino.js.org",
"bugs": "https://github.com/mozilla-neutrino/neutrino-dev/issues",
"dependencies": {
"deepmerge": "^2.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

Use deepmerge ^1.5.2.

@eliperelman
Copy link
Member

🎉

@eliperelman eliperelman merged commit 5c8fa48 into neutrinojs:master Nov 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants