๐ This package has been deprecated. This package has been deprecated and recommend to use @svgr/webpack instead.
import { element as LogoSVG } from "./logo.svg";
import photoImagePath from "./photo.jpg";
export default (props) => (
<div>
<LogoSVG width="230" height="140" fill={props.color} />
<img src={photoImagePath} alt="flower" />
</div>
);
- Table of Contents
- Features
- Quick Start
- Usage
- Options
- Recipes
- Contributing to next-image-element
- License
FEATURES | WHAT YOU CAN DO |
---|---|
โ๏ธ Designed for JSX | Import images as React element like <MySVG fill={color} /> |
๐จ Designed for CSS | Import images as some URL in not JavaScript files |
โจ Exported as plain image | Import image paths or as inline image (Base64) |
๐ฉ Type Safe | You can use with TypeScript |
๐ง Built for Next.js | It's very easy to setup |
- npm or Yarn
- Next.js 5.0.0 or higher
$ npm install -D next-image-element
If you are using Yarn, use the following command.
$ yarn add -D next-image-element
Firstly setup your Next.js settings.
// next.config.js
const withImageElement = require("next-image-element");
module.exports = withImageElement();
All you need is the above!
This package depends on react-image-element-loader, for more detail, see here.
You can import file path or React element from images. It's possible to pass props such as HTMLAttributes, but src
will be ignored.
import { element as LogoSVG } from "./logo.svg";
import photoImagePath from "./photo.jpg";
export default (props) => (
<div>
<LogoSVG width="230" height="140" fill={props.color} />
<img src={photoImagePath} alt="flower" />
</div>
);
Also when you import images from not JavaScript files such as CSS, next-image-element imports them as same as using default export, so it will be actual URL or inline image (Base64).
.box {
background-image: url("./icon.png");
}
next-image-element supports PNG (.png), JPEG (.jpg), GIF (.gif), and SVG (.svg).
Type: Number
Default: undefined
This option is to specify the maximum size of image file in bytes.
If an image is greater than the limit or sizeLimit
option specified undefined
, path
will be an actual URL. In that case,
file-loader will be used and all query parameters are passed to it.
If an image is smaller than the limit, path
will be Base64 encoded URL.
module.exports = withImageElement({
imageElementOptions: {
sizeLimit: 10240, // 10kB
},
});
The limit can be specified via loader options and defaults to no limit.
Type: String
Default: "/_next/static/images/"
This option is to specify published image path used as actual URL. When you use next-image-element in Next.js projects, you
should start with "/_next/static/"
.
module.exports = withImageElement({
imageElementOptions: {
publicPath: "/static/images/",
},
});
Type: String
or Function
Default: (isServer) => `${isServer ? "../" : ""}static/images/
This option is to specify output image path. If you give string as this option, next-image-element will just use it. If you give
function as this option, next-image-element will call it with isServer
boolean value as the first argument, so you have to
give function which returns string in this case.
module.exports = withImageElement({
imageElementOptions: {
outputPath: "/static/images/",
},
});
Type: String
Default: "[name]-[hash].[ext]"
This option is to specify a pattern of images' file name. For more detail, please check this.
module.exports = withImageElement({
imageElementOptions: {
outputFileNamePattern: "[hash].[ext]",
},
});
For more detail, see here.
If you want to enable type checking in TypeScript for images, you should add the following to next-env.d.ts
file.
/// <reference types="next" />
/// <reference types="next/types/global" />
+ /// <reference types="next-image-element" />
Libraries such as Storybook outside Next.js does not load _next
directory automatically, but next-image-element uses _next
as published image path by default. So if you change the public path, the library can load images.
// storybook/webpack.config.js
module.exports = async ({ config }) => {
withImageElement({ imageElementOptions: { publicPath: "/static/images/" }}).webpack(config, { isServer: false });
return config;
};
Bug reports and pull requests are welcome on GitHub at https://github.com/jagaapple/next-image-element. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Please read Contributing Guidelines before development and contributing.
The library is available as open source under the terms of the MIT License.
Copyright 2021 Jaga Apple. All rights reserved.