1.1.1 • Published 4 years ago

modern-image-loader v1.1.1

Weekly downloads
20
License
-
Repository
github
Last release
4 years ago

modern-image-loader



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

See w3schools - <picture> tag to get an idea of when this loader can be useful.

Installation

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

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


(if configured properly)

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

Usage

Add the loader to your Webpack config:

webpack.config.js

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

Now you can import corresponding files in your entry script:

index.js

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

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:

<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

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

fallbackPath

"dog.jpeg"

paths

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

Options

OptionDefaultDescription
altValue for alt tag
exportdomPropertiesFormat 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.
inputMimeautoMime 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 options
pngquant{strip: true}imagemin-pngquant options
publicPathas defined in Webpack configPublic path that is added to the front of file names in returned JavaScript value.
webp{quality: 95, nearLossless: 50, sharpness: 5}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:

git clone git@github.com:jaid/modern-image-loader.git
cd modern-image-loader
npm install

Testing:

npm run test:dev

Testing in production environment:

npm run test

License

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.