@rundik/responsive-json-webpack-plugin
v1.5.3
Published
Resize images and combine responsive image info with static text in json.
Downloads
4
Maintainers
Readme
responsive-json-webpack-plugin
Set resizing instructions in JSON, can also output combined JSON with responsive image attributes (picture, sources, srcset, sizes, media) included where desired. Uses sharp. 100% Test Coverage! Requires Webpack 4.
Install
npm install responsive-json-webpack-plugin
Note for Windows users: You may need to run npm install --global --production windows-build-tools
as an Adminstrator first to properly install node-gyp, a dependency of sharp. This will also install Python 2.7.
webpack.config.js (Webpack 4)
const ResponsiveJSONWebpackPlugin = require("responsive-json-webpack-plugin")
module.exports = {
plugins: [
new ResponsiveJSONWebpackPlugin({
sourceTemplates: "src/assets/templates", //default
sourceImages: "src/assets/images", //default
outputFolder: "assets" //default
})
]
}
As JSON copier
If you just want to directly copy a json file to your build folder without having to/ require it in your javascript via a separate loader, put it in a folder called "raw" in your templates. It will be minimized and put in the outputFolder/data with the same name.
Basic Image Resize Usage
The following will create an optimized and compressed 16px width "sample.png" and 32px width "sample.jpg" in "assets/images" in the output build folder, given that the appropriate source files are in "src/assets/images".
src/assets/templates/images.json
[
{
"files": [
{
"src": "sample.png",
"size": 16
},
{
"src": "sample.jpg",
"size": 32
}
]
}
]
The following will create a 16px helloworld.png, a 36px width "helloworld-huge.png", and an 8px width "helloworld-8.png" in "assets/images" in the output build folder. Note that [name]
and [size]
will be replaced appropriately.
[
{
"files": [
{
"src": "folder/sample-2.png",
"dest": "helloworld",
"size": 16
}
],
"alternates": [
{
"size": 36,
"dest": "[name]-huge"
},
{
"size": 8,
"dest": "[name]-[size]"
}
]
}
]
You can skip copying the original file by omitting the size
property of the source file, or by entering the source file path as a string instead of in an object. The following will just create a 36px sample-2-huge.png.
[
{
"files": ["folder/sample-2.png"],
"alternates": [
{
"size": 36,
"dest": "[name]-huge"
}
]
}
]
For a more fine tuned image resize, your size can be that of sharp's options:
[
{
"files": ["folder/sample-2.png"],
"alternates": [
{
"dest": "[name]-huge",
"size": {
"width": 16,
"height": 16,
"fit": "contain|cover|fill|inside|outside",
"position": "top | right top | right | right bottom | bottom | left bottom | left | left top | north | northeast | east | southeast | south | southwest | west | northwest | center | centre | cover",
"background": { "r": 0, "g": 0, "b": 0, "alpha": 0 },
"kernel": "nearest|cubic|lanczos2|lanczos3",
"withoutEnlargement": false,
"fastShrinkOnLoad": true
}
}
]
}
]
JSON Image Injection Usage
JSON injection uses the same webpack options, but requires a folder structure where the first subdirectory in the template folder is the output json file name. That folder must contain the folders "data" and "images", with matching filenames for those which belong to the same key. E.g.:
In "templates/index/data/sample.json"
{
"text": "This is sample text.",
"section": {
"title": "Sample Section Title"
}
}
In "templates/index/data/other.json"
{
"text": "This is other sample text."
}
In "templates/index/images/sample.json"
[
{
"path": "image",
"alt": "standard sample image",
"files": [
{
"src": "sample-3.png",
"size": 16
}
]
},
{
"path": "section.image",
"files": [
{
"src": "sample-3.png",
"dest": "section",
"size": 24
}
]
}
]
Will output a 16px width "sample-3.png" and a 24px with "section.png" along with a file at index.json:
{
"sample": {
"text": "This is sample text.",
"image": {
"src": "sample-3.png",
"alt": "standard sample image",
"size": 16
},
"section": {
"title": "Sample Section Title",
"image": {
"src": "section.png",
"size": 24
}
}
},
"other": {
"text": "This is other sample text."
}
}
You can also output an array at the path by giving multiple files in an image template file.
[
{
"path": "multiple",
"files": [
{
"src": "sample-2.png",
"size": 16
},
{
"src": "sample-3.png",
"size": 16
}
]
}
]
It is possible to inject an image at each item in an exisiting an array by using the following syntax.
The []
will be replaced by the index of the item in the set. The index does not need to match the exisiting array in length, but does need a valid path. Set destination file names will replace [name]
[size]
and [index]
appropriately.
{
"path": "array.[].image",
"set": [
{
"alt": "array item",
"files": [
{
"src": "sample-arr.png",
"size": 16,
"dest": "array-image-[index]"
}
]
}
]
}
This will output:
{
"array": [
{
"text": "existing text in data json",
"image": {
"alt": "array item 1",
"src": "arr-image-1.png",
"size": 16
}
}
]
}
Responsive Image JSON Injection Usage
Here is a full responsive image example that utilizes both resolution switching and art direction! If your source file has a "dest", that will be the basis of [name]
. Your files
images are always copied, even if never explicitly used, to serve as a backup in case a browser doesn't support the picture
element or srcset
.
Keep in mind that most fields can be safely omitted, but are recommended if you are using this with the included React file.
[
{
"path": "responsive",
"alt": "Sample ALT",
"files": [
{
"src": "sample-8.png",
"size": 16
}
],
"imageTemplate": {
"img": {
"sizes": "(max-width: 56.25em) 20vw, (max-width: 37.5em) 30vw, 300px",
"srcset": [
{
"dest": "[name]x8",
"size": 8
}
]
},
"sources": [
{
"media": "(max-width: 37.5em)",
"sizes": "20vw",
"srcset": [
{
"src": "sample-9.png",
"dest": "potato",
"size": 16
},
{
"src": "sample-10.png",
"dest": "[name]-x[size]",
"size": 16
}
]
}
]
}
}
]
Which will output 4 images and the following json:
{
"src": "sample-8.png",
"alt": "Sample ALT",
"size": 16,
"sizes": "(max-width: 56.25em) 20vw, (max-width: 37.5em) 30vw, 300px",
"srcset": [
{
"src": "sample-8x8.png",
"size": 8
}
],
"sources": [
{
"media": "(max-width: 37.5em)",
"sizes": "20vw",
"srcset": [
{
"src": "potato.png",
"size": 16
},
{
"src": "sample-10-x16.png",
"size": 16
}
]
}
]
}
React
You can use the resulting json directly with React.
import ResponsiveImage from 'responsive-json-webpack-plugin/react'
<ResponsiveImage image={outputJSON.imagePath} className="" alt="" />