# modern-image-loader

> Webpack loader that outputs optimized WebP images with JPEG/PNG fallback and other neat extras.

Latest version **1.1.1** (published 2020-02-04) · 0 weekly downloads

## Install

```sh
npm install modern-image-loader
pnpm add modern-image-loader
yarn add modern-image-loader
bun add modern-image-loader
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2020-02-04 |
| First published | 2020-01-11 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 12.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jaid |
| Maintainers | jaid |

## Links

- npm: https://www.npmjs.com/package/modern-image-loader
- Repository: https://github.com/jaid/modern-image-loader
- Homepage: https://github.com/jaid/modern-image-loader#readme
- Issues: https://github.com/jaid/modern-image-loader/issues
- npm.io page: https://npm.io/package/modern-image-loader

## Dependencies (9)

- [imagemin](https://npm.io/package/imagemin.md) ^7.0.1
- [ensure-end](https://npm.io/package/ensure-end.md) ^1.0.6
- [mime-types](https://npm.io/package/mime-types.md) ^2.1.26
- [loader-utils](https://npm.io/package/loader-utils.md) ^1.2.3
- [schema-utils](https://npm.io/package/schema-utils.md) ^2.6.1
- [imagemin-webp](https://npm.io/package/imagemin-webp.md) ^5.1.0
- [imagemin-optipng](https://npm.io/package/imagemin-optipng.md) ^7.1.0
- [imagemin-jpegtran](https://npm.io/package/imagemin-jpegtran.md) ^6.0.0
- [imagemin-pngquant](https://npm.io/package/imagemin-pngquant.md) ^8.0.0

## Recent versions

- 1.1.1 (latest) — 2020-02-04
- 1.1.0 — 2020-01-11
- 1.0.1 — 2020-01-11
- 1.0.0 — 2020-01-11

## README

# modern-image-loader


<a href="https://raw.githubusercontent.com/jaid/modern-image-loader/master/license.txt"><img src="https://img.shields.io/github/license/jaid/modern-image-loader?style=flat-square" alt="License"/></a> <a href="https://github.com/sponsors/jaid"><img src="https://img.shields.io/badge/<3-Sponsor-FF45F1?style=flat-square" alt="Sponsor modern-image-loader"/></a>  
<a href="https://actions-badge.atrox.dev/jaid/modern-image-loader/goto"><img src="https://img.shields.io/endpoint.svg?style=flat-square&url=https%3A%2F%2Factions-badge.atrox.dev%2Fjaid%2Fmodern-image-loader%2Fbadge" alt="Build status"/></a> <a href="https://github.com/jaid/modern-image-loader/commits"><img src="https://img.shields.io/github/commits-since/jaid/modern-image-loader/v1.1.1?style=flat-square&logo=github" alt="Commits since v1.1.1"/></a> <a href="https://github.com/jaid/modern-image-loader/commits"><img src="https://img.shields.io/github/last-commit/jaid/modern-image-loader?style=flat-square&logo=github" alt="Last commit"/></a> <a href="https://github.com/jaid/modern-image-loader/issues"><img src="https://img.shields.io/github/issues/jaid/modern-image-loader?style=flat-square&logo=github" alt="Issues"/></a>  
<a href="https://npmjs.com/package/modern-image-loader"><img src="https://img.shields.io/npm/v/modern-image-loader?style=flat-square&logo=npm&label=latest%20version" alt="Latest version on npm"/></a> <a href="https://github.com/jaid/modern-image-loader/network/dependents"><img src="https://img.shields.io/librariesio/dependents/npm/modern-image-loader?style=flat-square&logo=npm" alt="Dependents"/></a> <a href="https://npmjs.com/package/modern-image-loader"><img src="https://img.shields.io/npm/dm/modern-image-loader?style=flat-square&logo=npm" alt="Downloads"/></a>

**Webpack loader that outputs optimized WebP images with JPEG/PNG fallback and other neat extras.**


See [w3schools - `<picture>` tag](https://www.w3schools.com/tags/tag_picture.asp) to get an idea of when this loader can be useful.


## Installation

<a href="https://npmjs.com/package/modern-image-loader"><img src="https://img.shields.io/badge/npm-modern--image--loader-C23039?style=flat-square&logo=npm" alt="modern-image-loader on npm"/></a>

```bash
npm install --save-dev modern-image-loader@^1.1.1
```

<a href="https://yarnpkg.com/package/modern-image-loader"><img src="https://img.shields.io/badge/Yarn-modern--image--loader-2F8CB7?style=flat-square&logo=yarn&logoColor=white" alt="modern-image-loader on Yarn"/></a>

```bash
yarn add --dev modern-image-loader@^1.1.1
```

<a href="https://github.com/jaid/modern-image-loader/packages"><img src="https://img.shields.io/badge/GitHub Packages-@jaid/modern--image--loader-24282e?style=flat-square&logo=github" alt="@jaid/modern-image-loader on GitHub Packages"/></a>  
(if [configured properly](https://help.github.com/en/github/managing-packages-with-github-packages/configuring-npm-for-use-with-github-packages))

```bash
npm install --save-dev @jaid/modern-image-loader@^1.1.1
```





## Usage

Add the loader to your Webpack config:

`webpack.config.js`
```javascript
module.exports = {
  module: {
    rules: [
      {
        test: /\.(png|jpg|jpeg)$/,
        use: "modern-image-loader"
      }
    ]
  }
}
```

Now you can import corresponding files in your entry script:

`index.js`
```javascript
import image from "./dog.jpeg"

console.log(image)
```

## Advanced Usage

### React Example

The default returned object makes it easy to be used in frameworks like React.

`index.js`
```javascript
import React from "react"
import ReactDom from "react-dom"

import image from "./dog.jpeg?alt=Dog"

ReactDom.render(<div className="main">
  <picture>
    <source {...image.webp}/>
    <source {...image.fallback}/>
    <img {...image.img}/>
  </picture>
</div>, document.body)
```

This will render a DOM like this:

```html
<body>
  <div class="main">
    <picture>
      <source srcset="dog.webp" type="image/webp" />
      <source srcset="dog.jpeg" type="image/jpeg" />
      <img src="dog.jpeg" alt="Dog" />
    </picture>
  </div>
</body>
```

### Export types

What `image` will be in `import image from "./dog.jpeg"`, depends on the chosen export type. It is `dom` by default.

#### dom

```javascript
{
  webp: {
    srcset: "dog.webp",
    type: "image/webp"
  },
  fallback: {
    srcset: "dog.jpeg",
    type: "image/jpeg"
  },
  img: {
    src: "dog.jpeg",
    alt: "Dog"
  }
}
```

#### fallbackPath

```javascript
"dog.jpeg"
```

#### paths

```javascript
{
  webp: "dog.webp",
  fallback: "dog.jpeg"
}
```

## Options

Option|Default|Description
---|---|---
alt||Value for `alt` tag
export|`domProperties`|Format of returned JavaScript value. Valid formats: `dom`, `fallbackPath`, `paths`
fallbackFileName|`[hash:base64:6]`|Output file name. Can use webpack-specific placeholders. `[ext]` will be based on input file type.
inputMime|auto|Mime type of input file. You probably do not want to set this, as it is automatically determined based on input file extension.
jpegtran|`{}`|[`imagemin-jpegtran`](https://www.npmjs.com/package/imagemin-jpegtran) options
pngquant|`{strip: true}`|[`imagemin-pngquant`](https://www.npmjs.com/package/imagemin-pngquant) options
publicPath|as defined in Webpack config|Public path that is added to the front of file names in returned JavaScript value.
webp|`{quality: 95, nearLossless: 50, sharpness: 5}`|[`imagemin-webp`](https://www.npmjs.com/package/imagemin-webp) options
webpFileName|`[hash:base64:6]`|Output file name. Can use webpack-specific placeholders. `[ext]` will be "webp".

All options can be overwritten in any asset import by appending a query string.







## Development



Setting up:
```bash
git clone git@github.com:jaid/modern-image-loader.git
cd modern-image-loader
npm install
```
Testing:
```bash
npm run test:dev
```
Testing in production environment:
```bash
npm run test
```


## License
```text
MIT License

Copyright © 2020, Jaid <jaid.jsx@gmail.com> (github.com/jaid)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```

---
_Source: https://npm.io/package/modern-image-loader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
