Laravel-mix: Is there a way to optimize images not referenced in css files?

Created on 9 Jun 2017  路  4Comments  路  Source: JeffreyWay/laravel-mix

I have a bunch of images that are not referenced in any css files, hence don't go through the optimization pipeline. Is there a way to trigger image optimization on them (svgo, mozjpeg etc.)?

---img
------image1.jpeg // referenced in css
------image2.svg // referenced in css
------image3.png // not referenced in css
---scss
------app.scss
---js
------app.js

dist
---img
------image1.jpeg
------image2.svg
app.css
app.js

Most helpful comment

You can import images not referenced in css like this:

In your resources/assets/js/app.js:

// import all the images in [resources/assets/images], including subdirectories

require.context('../../images', true, /\.(png|jpe?g|gif)$/);

Now webpack will process all images through file-loeader and img-loader.

Note: I'm not including svg in the regex because currently laravel mix stores them in the fonts directory.

All 4 comments

Also have this question.

Use relative paths for your images, then the respective loaders will take care of them.

@panda-madness I do this by running a custom function.

You can import images not referenced in css like this:

In your resources/assets/js/app.js:

// import all the images in [resources/assets/images], including subdirectories

require.context('../../images', true, /\.(png|jpe?g|gif)$/);

Now webpack will process all images through file-loeader and img-loader.

Note: I'm not including svg in the regex because currently laravel mix stores them in the fonts directory.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jpmurray picture jpmurray  路  3Comments

Micaso picture Micaso  路  3Comments

RomainGoncalves picture RomainGoncalves  路  3Comments

pixieaka picture pixieaka  路  3Comments

mementoneli picture mementoneli  路  3Comments