Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jaid/modern-image-loader

Webpack loader that loads images as WebP with some neat extras.
https://github.com/jaid/modern-image-loader

dom image img loader picture react react-dom webpack webpack-loader webpack-plugin

Last synced: about 1 month ago
JSON representation

Webpack loader that loads images as WebP with some neat extras.

Awesome Lists containing this project

README

        

# modern-image-loader

License Sponsor modern-image-loader
Build status Commits since v1.1.1 Last commit Issues
Latest version on npm Dependents Downloads

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

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

## Installation

modern-image-loader on npm

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

modern-image-loader on Yarn

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

@jaid/modern-image-loader on GitHub Packages
(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(







, document.body)
```

This will render a DOM like this:

```html





Dog

```

### 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 [email protected]: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 (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.
```