Coder Social home page Coder Social logo

cyrilwanner / next-optimized-images Goto Github PK

View Code? Open in Web Editor NEW
2.2K 16.0 95.0 7.9 MB

🌅 next-optimized-images automatically optimizes images used in next.js projects (jpeg, png, svg, webp and gif).

License: MIT License

JavaScript 100.00%
nextjs plugin image optimization react

next-optimized-images's Introduction

🌅 next-optimized-images npm version license downloads

💡 Version 3 is coming! It introduces a complete rewrite with many new features and bugfixes. If you want to help developing and testing the upcoming major version, please check out the canary branch for installation instructions and more information about the new features. (RFC issue)


Automatically optimize images used in next.js projects (jpeg, png, svg, webp and gif).

Image sizes can often get reduced between 20-60%, but this is not the only thing next-optimized-images does:

  • Reduces image size by optimizing images during build
  • Improves loading speed by providing progressive images (for formats that support it)
  • Inlines small images to save HTTP requests and additional roundtrips
  • Adds a content hash to the file name so images can get cached on CDN level and in the browser for a long time
  • Same image URLs over multiple builds for long time caching
  • Provides query params for file-specific handling/settings
  • jpeg/png images can be converted to webp on the fly for an even smaller size
  • Provides the possibility to use SVG sprites for a better performance when using the same icons multiple times (e.g. in a list)
  • Can resize images or generate different placeholders while lazy loading images: low quality images, dominant colors or image outlines

Table of contents

Installation

npm install next-optimized-images

Node >= 8 is required for version 2. If you need to support older node versions, you can still use version 1 of next-optimized-images.

Enable the plugin in your Next.js configuration file:

// next.config.js
const withPlugins = require('next-compose-plugins');
const optimizedImages = require('next-optimized-images');

module.exports = withPlugins([
  [optimizedImages, {
    /* config for next-optimized-images */
  }],

  // your other plugins here

]);

See the configuration section for all available options.

⚠️ From version 2 on, images won't get optimized out of the box anymore. You have to install the optimization packages you really need in addition to this plugin. This doesn't force you to download big optimization libraries you don't even use. Please check out the table of all optional optimization packages.

The example above uses next-compose-plugins for a cleaner API when using many plugins, see its readme for a more detailed example. next-optimized-images also works with the standard plugin api:

// next.config.js
const withOptimizedImages = require('next-optimized-images');

module.exports = withOptimizedImages({
  /* config for next-optimized-images */

  // your config for other plugins or the general next.js here...
});

Optimization Packages

Starting from version 2, you have to install the optimization packages you need in your project in addition to this plugin. next-optimized-images then detects all the supported packages and uses them.

So you only have to install these packages with npm, there is no additional step needed after that.

The following optimization packages are available and supported:

Optimization Package Description Project Link
imagemin-mozjpeg Optimizes JPEG images. Link
imagemin-optipng Optimizes PNG images. Link
imagemin-pngquant Alternative for optimizing PNG images. Link
imagemin-gifsicle Optimizes GIF images. Link
imagemin-svgo Optimizes SVG images and icons. Link
svg-sprite-loader Adds the possibility to use svg sprites for a better performance. Read the sprite section for more information. Link
webp-loader Optimizes WebP images and can convert JPEG/PNG images to WebP on the fly (webp resource query). Link
lqip-loader Generates low quality image placeholders and can extract the dominant colors of an image (lqip resource query) Link
responsive-loader Can resize images on the fly and create multiple versions of it for a srcset.
Important: You need to additionally install either jimp (node implementation, slower) or sharp (binary, faster)
Link
image-trace-loader Generates SVG image outlines which can be used as a placeholder while loading the original image (trace resource query). Link

Example: If you have JPG, PNG, and SVG images in your project, you would then need to run

npm install imagemin-mozjpeg imagemin-optipng imagemin-svgo

To install all optional packages, run:

npm install imagemin-mozjpeg imagemin-optipng imagemin-gifsicle imagemin-svgo svg-sprite-loader webp-loader lqip-loader responsive-loader jimp image-trace-loader

⚠️ Please note that by default, images are only optimized for production builds, not development builds. However, this can get changed with the optimizeImagesInDev config.

💡 Depending on your build/deployment setup, it is also possibile to install these as devDependencies. Just make sure that the packages are available when you build your project.

ℹ️ Since version 2.5, ico files are also optionally supported but need to be enabled in the handleImages config.

Usage

You can now import or require your images directly in your react components:

import React from 'react';

export default () => (
  <div>
    <img src={require('./images/my-image.jpg')} />
    <img src={require('./images/my-small-image.png')} />
    <img src={require('./images/my-icon.svg')} />
  </div>
);

/**
 * Results in:
 *
 * <div>
 *   <img src="/_next/static/images/my-image-5216de428a8e8bd01a4aa3673d2d1391.jpg" />
 *   <img src="data:image/png;base64,..." />
 *   <img src="/_next/static/images/my-icon-572812a2b04ed76f93f05bf57563c35d.svg" />
 * </div>
 */

Please be aware that images only get optimized in production by default to reduce the build time in your development environment.

If you are using CSS modules, this package also detects images and optimized them in url() values in your css/sass/less files:

.Header {
  background-image: url('./images/my-image.jpg');
}

/**
 * Results in:
 *
 * .Header {
 *   background-image: url('/_next/static/images/my-image-5216de428a8e8bd01a4aa3673d2d1391.jpg');
 * }
 */

If the file is below the limit for inlining images, the require(...) will return a base64 data-uri (data:image/jpeg;base64,...).

Otherwise, next-optimized-images will copy your image into the static folder of next.js and the require(...) returns the path to your image in this case (/_next/static/images/my-image-5216de428a8e8bd01a4aa3673d2d1391.jpg).

You can use both variants directly on an image in the src attribute or in your CSS file inside an url() value.

Query params

If you are using flow or eslint-plugin-import and are experiencing some issues with query params, check out the solution posted by @eleith.

There are some cases where you don't want to reference a file or get a base64 data-uri but you actually want to include the raw file directly into your HTML. Especially for SVGs because you can't style them with CSS if they are in an src attribute on an image.

So there are additional options you can specify as query params when you import the images.

  • ?include: Include the raw file directly (useful for SVG icons)
  • ?webp: Convert a JPEG/PNG image to WebP on the fly
  • ?inline: Force inlining an image (data-uri)
  • ?url: Force an URL for a small image (instead of data-uri)
  • ?original: Use the original image and do not optimize it
  • ?lqip: Generate a low quality image placeholder
  • ?lqip-colors: Extract the dominant colors of an image
  • ?trace: Use traced outlines as loading placeholder
  • ?resize: Resize an image
  • ?sprite: Use SVG sprites

?include

The image will now directly be included in your HTML without a data-uri or a reference to your file.

As described above, this is useful for SVGs so you can style them with CSS.

import React from 'react';

export default () => (
  <div dangerouslySetInnerHTML={{__html: require('./images/my-icon.svg?include')}} />
);

/**
 * Results in:
 *
 * <div>
 *   <svg width="16" height="16" xmlns="http://www.w3.org/2000/svg">
 *     <path d="M8 0C3.589 0 0 3.589 0 8s3.589 ..." style="filled-opacity:1" fill-rule="evenodd">
 *     </path>
 *   </svg>
 * </div>
 */

The image will still get optimized, even if it is directly included in your content (but by default only in production).

?webp

Requires the optional optimization package webp-loader (npm install webp-loader)

WebP is an even better and smaller image format but it is still not that common yet and developers often only receive jpeg/png images.

If this ?webp query parameter is specified, next-optimized-images automatically converts a JPEG/PNG image to the new WebP format.

For browsers that don't yet support WebP, you can also provide a fallback using the <picture> tag:

import React from 'react';

export default () => (
  <picture>
    <source srcSet={require('./images/my-image.jpg?webp')} type="image/webp" />
    <source srcSet={require('./images/my-image.jpg')} type="image/jpeg" />
    <img src={require('./images/my-image.jpg')} />
  </picture>
);

/**
 * Results in:
 * <picture>
 *   <source srcset="/_next/static/images/my-image-d6816ecc28862cf6f725b29b1d6aab5e.jpg.webp" type="image/webp" />
 *   <source srcset="/_next/static/images/my-image-5216de428a8e8bd01a4aa3673d2d1391.jpg" type="image/jpeg" />
 *   <img src="/_next/static/images/my-image-5216de428a8e8bd01a4aa3673d2d1391.jpg" />
 * </picture>
 */

?inline

You can specify a limit for inlining images which will include it as a data-uri directly in your content instead of referencing a file if the file size is below that limit.

You usually don't want to specify a too high limit but there may be cases where you still want to inline larger images.

In this case, you don't have to set the global limit to a higher value but you can add an exception for a single image using the ?inline query options.

import React from 'react';

export default () => (
  <img src={require('./images/my-image.jpg?inline')} />
);

/**
 * Results in:
 *
 * <img src="data:image/png;base64,..." />
 *
 * Even if the image size is above the defined limit.
 */

The inlining will only get applied to exactly this import, so if you import the image a second time without the ?inline option, it will then get normally referenced as a file if it is above your limit.

?url

When you have an image smaller than your defined limit for inlining, it normally gets inlined automatically. If you don't want a specific small file to get inlined, you can use the ?url query param to always get back an image URL, regardless of the inline limit.

If you are using this option a lot, it could also make sense to disable the inlining completely and use the ?inline param for single files.

import React from 'react';

export default () => (
  <img src={require('./images/my-image.jpg?url')} />
);

/**
 * Results in:
 *
 * <img src="/_next/static/images/my-image-5216de428a8e8bd01a4aa3673d2d1391.jpg" />
 *
 * Even if the image size is below the defined inlining limit.
 */

The inlining will only get disabled for exactly this import, so if you import the image a second time without the ?url option, it will then get inlined again if it is below your limit.

?original

The image won't get optimized and used as it is. It makes sense to use this query param if you know an image already got optimized (e.g. during export) so it doesn't get optimized again a second time.

import React from 'react';

export default () => (
  <img src={require('./images/my-image.jpg?original')} />
);

This can also be combined with the ?url or ?inline resource query (e.g. ?original&inline).

?lqip

Requires the optional package lqip-loader (npm install lqip-loader)

When using this resource query, a very small (about 10x7 pixel) image gets created. You can then display this image as a placeholder until the real (big) image has loaded.

You will normally stretch this tiny image to the same size as the real image is, like medium.com does. To make the stretched image look better in chrome, check out this solution and add a blur filter to your image.

import React from 'react';

export default () => (
  <img src={require('./images/my-image.jpg?lqip')} />
);

/**
 * Replaces the src with a tiny image in base64.
 */

?lqip-colors

Requires the optional package lqip-loader (npm install lqip-loader)

This resource query returns you an array with hex values of the dominant colors of an image. You can also use this as a placeholder until the real image has loaded (e.g. as a background) like the Google Picture Search does.

The number of colors returned can vary and depends on how many different colors your image has.

import React from 'react';

export default () => (
  <div style={{ backgroundColor: require('./images/my-image.jpg?lqip-colors')[0] }}>...</div>
);

/**
 * require('./images/my-image.jpg?lqip-colors')
 *
 * returns for example
 *
 * ['#0e648d', '#5f94b5', '#a7bbcb', '#223240', '#a4c3dc', '#1b6c9c']
 */

?trace

Requires the optional package image-trace-loader (npm install image-trace-loader)

With the ?trace resource query, you can generate SVG image outlines which can be used as a placeholder while loading the original image.

import React from 'react';
import MyImage from './images/my-image.jpg?trace';

export default () => (
  <div>
    <img src={MyImage.trace} />   {/* <-- SVG trace */}
    <img src={MyImage.src} />     {/* <-- Normal image which you want to lazy load */}
  </div>
);

/**
 * Results in:
 *
 * <div>
 *  <img src="data:image/svg+xml,...">
 *  <img src="/_next/static/images/image-trace-85bf5c58ce3d91fbbf54aa03c44ab747.jpg">
 * </div>
 */

require('./images/my-image.jpg?trace') returns an object containing the trace (trace) as an inlined SVG and the normal image (src) which also gets optimized.

The trace will have exactly the same width and height as your normal image.

Options for the loader can be set in the plugin configuration.

?resize

Requires the optional package responsive-loader (npm install responsive-loader) and either jimp (node implementation, slower) or sharp (binary, faster)

After the ?resize resource query, you can add any other query of the responsive-loader which allows you to resize images and create whole source sets.

import React from 'react';

const oneSize = require('./images/my-image.jpg?resize&size=300');
const multipleSizes = require('./images/my-image.jpg?resize&sizes[]=300&sizes[]=600&sizes[]=1000');

export default () => (
  <div>
    {/* Single image: */}
    <img src={oneSize.src} />

    {/* Source set with multiple sizes: */}
    <img srcSet={multipleSizes.srcSet} src={multipleSizes.src} />
  </div>
);

If only the size or sizes param is used, the ?resize param can also be omitted (e.g. my-image.jpg?size=300). But it is required for all other parameters of responsive-loader.

You can also set global configs in the responsive property (in the next.config.js file) and define, for example, default sizes which will get generated when you don't specify one for an image (e.g. only my-image.jpg?resize).

?sprite

Requires the optional optimization packages imagemin-svgo and svg-sprite-loader (npm install imagemin-svgo svg-sprite-loader)

If you need to style or animate your SVGs ?include might be the wrong option, because that ends up in a lot of DOM elements, especially when using the SVG in list-items etc. In that case, you can use ?sprite which uses svg-sprite-loader to render and inject an SVG sprite in the page automatically.

import React from 'react';
import MyIcon from './icons/my-icon.svg?sprite';

export default () => (
  <div>
    my page..
    <MyIcon />
  </div>
);

All props passed to the imported sprite will get applied to the <svg> element, so you can add a class normally with <MyIcon className="icon-class" />.

The svg-sprite-loader object also gets exposed if you want to build your own component:

import React from 'react';
import icon from './icons/icon.svg?sprite';

export default () => (
  <div>
    my page..
    <svg viewBox={icon.viewBox}>
      <use xlinkHref={`#${icon.id}`} />
    </svg>
  </div>
);

To also make this work for server-side rendering, you need to add these changes to your _document.jsx file (read here if you don't have this file yet):

// ./pages/_document.js
import Document, { Head, Main, NextScript } from 'next/document';
import sprite from 'svg-sprite-loader/runtime/sprite.build';

export default class MyDocument extends Document {
  static async getInitialProps(ctx) {
    const initialProps = await Document.getInitialProps(ctx);
    const spriteContent = sprite.stringify();

    return {
      spriteContent,
      ...initialProps,
    };
  }

  render() {
    return (
      <html>
        <Head>{/* your head if needed */}</Head>
        <body>
          <div dangerouslySetInnerHTML={{ __html: this.props.spriteContent }} />
          <Main />
          <NextScript />
        </body>
      </html>
    );
  }
}

Configuration

This plugin uses img-loader under the hood which is based on mozjpeg, optipng, gifsicle and svgo.

The default options for these optimizers should be enough in most cases, but you can overwrite every available option if you want to.

handleImages

Type: string[]
Default: ['jpeg', 'png', 'svg', 'webp', 'gif']

next-optimized-images registers the webpack loader for all these file types. If you don't want one of these handled by next-optimized-images because you, for example, have another plugin or custom loader rule, simply remove it from the array.

Please note that an image being handled does not mean it also gets automatically optimized. The required optimization package for that image also has to be installed. Please read the optimization packages section for more information.

If an image gets handled but not optimized, it means that the original image will get used and copied for the build.

ℹ️ Since version 2.5, ico files are also supported but for backwards compatibility, they need to be manually enabled. By adding 'ico' to the handleImages array, the plugin will also handle ico files.

inlineImageLimit

Type: number
Default: 8192

Smaller files will get inlined with a data-uri by url-loader. This number defines the maximum file size (in bytes) for images to get inlined. If an image is bigger, it will get copied to the static folder of next.

Images will get optimized in both cases.

To completely disable image inlining, set this value to -1. You will then always get back an image URL.

imagesFolder

Type: string
Default: 'images'

Folder name inside /static/ in which the images will get copied to during build.

imagesPublicPath

Type: string
Default: `/_next/static/${imagesFolder}/`

The public path that should be used for image URLs. This can be used to serve the optimized image from a cloud storage service like S3.

From version 2 on, next-optimized-images uses the assetPrefx config of next.js by default, but you can overwrite it with imagesPublicPath specially for images.

imagesOutputPath

Type: string
Default: `static/${imagesFolder}/`

The output path that should be used for images. This can be used to have a custom output folder.

imagesName

Type: string
Default: '[name]-[hash].[ext]'

The filename of the optimized images. Make sure you keep the [hash] part so they receive a new filename if the content changes.

removeOriginalExtension

Type: boolean
Default: false

When images converted to WebP on the fly, .webp was append to the filename. For example, test.png became test.png.webp. If you want to have only one filename extension like test.webp, you can set this option to true.

optimizeImagesInDev

Type: boolean
Default: false

For faster development builds and HMR, images will not get optimized by default when running in development mode. In production, images will always get optimized, regardless of this setting.

mozjpeg

Requires the optional optimization package imagemin-mozjpeg (npm install imagemin-mozjpeg)

Type: object
Default: {}

mozjpeg is used for optimizing jpeg images. You can specify the options for it here. The default options of mozjpeg are used if you omit this option.

optipng

Requires the optional optimization package imagemin-optipng (npm install imagemin-optipng)

Type: object
Default: {}

optipng is used for optimizing png images by default. You can specify the options for it here. The default options of optipng are used if you omit this option.

pngquant

Requires the optional optimization package imagemin-pngquant (npm install imagemin-pngquant)

Type: object
Default: {}

pngquant is an alternative way for optimizing png images. The default options of pngquant are used if you omit this option.

gifsicle

Requires the optional optimization package imagemin-gifsicle (npm install imagemin-gifsicle)

Type: object
Default:

{
    interlaced: true,
    optimizationLevel: 3,
}

gifsicle is used for optimizing gif images. You can specify the options for it here. The default options of gifsicle are used if you omit this option.

svgo

Requires the optional optimization package imagemin-svgo (npm install imagemin-svgo)

Type: object
Default: {}

svgo is used for optimizing svg images and icons. You can specify the options for it here. The default options of svgo are used if you omit this option.

Single svgo plugins can get disabled/enabled in the plugins array:

{
  svgo: {
    plugins: [
      { removeComments: false }
    ]
  }
}

svgSpriteLoader

Requires the optional optimization packages imagemin-svgo and svg-sprite-loader (npm install imagemin-svgo svg-sprite-loader)

Type: object
Default:

{
  runtimeGenerator: require.resolve(path.resolve('node_modules', 'next-optimized-images', 'svg-runtime-generator.js')),
}

When using the svg sprite option, svg-sprite-loader gets used internally. You can overwrite the configuration passed to this loader here.

webp

Requires the optional optimization package webp-loader (npm install webp-loader)

Type: object
Default: {}

imagemin-webp is used for optimizing webp images and converting other formats to webp. You can specify the options for it here. The default options of imagemin-webp are used if you omit this option.

imageTrace

Requires the optional package image-trace-loader (npm install image-trace-loader)

Type: object
Default: {}

When using image-trace-loader for the ?trace resource query, you can define all options for the image trace loader in this object. The default options of image-trace-loader are used if you omit this option.

responsive

Requires the optional optimization package responsive-loader (npm install responsive-loader)

Type: object
Default: {}

The configuration for the responsive-loader can be defined here.

defaultImageLoader

Requires the optional optimization package responsive-loader (npm install responsive-loader)

Type: string
Default: 'img-loader'

By default, img-loader handles most of the requests. However, if you use the responsive-loader a lot and don't want to add the ?resize query param to every require, you can set this value to 'responsive-loader'.

After that, responsive-loader will handle all JPEG and PNG images per default, even without an additional query param. Just be aware that you can't use any of the query params next-optimized-images provides anymore on these images because the request just gets forwarded and not modified anymore. All other formats (SVG, WEBP and GIF) still work as before with the img-loader and so have all query params available.

optimizeImages

Type: boolean
Default: true

If you don't have any optimization package installed, no image will get optimized. In this case, a warning gets written to the console during build to inform you about a possible misconfiguration. If this config is intended and you indeed don't want the images to be optimized, you can set this value to false and you won't get the warning anymore.

Example

The options specified here are the default values.

So if they are good enough for your use-case, you don't have to specify them to have a shorter and cleaner next.config.js file.

// next.config.js
const withPlugins = require('next-compose-plugins');
const optimizedImages = require('next-optimized-images');

module.exports = withPlugins([
  [optimizedImages, {
    // these are the default values so you don't have to provide them if they are good enough for your use-case.
    // but you can overwrite them here with any valid value you want.
    inlineImageLimit: 8192,
    imagesFolder: 'images',
    imagesName: '[name]-[hash].[ext]',
    handleImages: ['jpeg', 'png', 'svg', 'webp', 'gif'],
    removeOriginalExtension: false,
    optimizeImages: true,
    optimizeImagesInDev: false,
    mozjpeg: {
      quality: 80,
    },
    optipng: {
      optimizationLevel: 3,
    },
    pngquant: false,
    gifsicle: {
      interlaced: true,
      optimizationLevel: 3,
    },
    svgo: {
      // enable/disable svgo plugins here
    },
    webp: {
      preset: 'default',
      quality: 75,
    },
  }],
]);

See also

  • next-images if you just want images and not optimize them
  • next-compose-plugins for a cleaner plugins API when you have many plugins in your next.config.js file
  • next-plugins for a list of official and community made plugins

License

MIT © Cyril Wanner

next-optimized-images's People

Contributors

aantthony avatar andysaman avatar angelmmiguel avatar aszx87410 avatar brianlovin avatar christinecha avatar cyrilwanner avatar dependabot[bot] avatar eleith avatar fandy avatar fdiskas avatar formatlos avatar greenkeeper[bot] avatar pcardune avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

next-optimized-images's Issues

responsive-loader with sharp error

I get this error when I use the responsive-loader with sharp, but it works well with the jimp module.

Error: Cannot find module '...node_modules\responsive-loader\lib\index.js/sharp'

-Windows OS

Inline image not working

I use next-optimized-images v2 with next.js v7.0.2 and I also install imagemin-mozjpeg imagemin-optipng imagemin-svgo as README said. But my svg (4kb) or png (27kb) image doesn't be inlined, It's wired. Here's the detail:

  • HTML tag:
<Link route="/">
  <a>
    <img
      className="main-header__logo"
      src="../../static/images/beats.png"
      alt="J:COM logo"
    />
  </a>
</Link>
  • next config:
const webpack = require('webpack');
const withPlugins = require('next-compose-plugins');
const sass = require('@zeit/next-sass');
const bundleAnalyzer = require('@zeit/next-bundle-analyzer');
const optimizedImages = require('next-optimized-images');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');

// Next.js configuration
const nextConfig = {
  webpack(config) {
    const { plugins } = config;

    plugins.push(
      new webpack.EnvironmentPlugin({
        NODE_ENV: process.env.NODE_ENV || 'development',
        INSTANCE_ENV: process.env.INSTANCE_ENV || 'development'
      }),
      new LodashModuleReplacementPlugin({ shorthands: true, collections: true })
    );

    return config;
  }
};

module.exports = withPlugins(
  [
    sass,
    [
      bundleAnalyzer,
      {
        analyzeServer: ['server', 'both'].includes(process.env.BUNDLE_ANALYZE),
        analyzeBrowser: ['client', 'both'].includes(process.env.BUNDLE_ANALYZE),
        bundleAnalyzerConfig: {
          server: {
            analyzerMode: 'static',
            reportFilename: '../../bundles/server.html'
          },
          browser: {
            analyzerMode: 'static',
            reportFilename: '../bundles/client.html'
          }
        }
      }
    ],
    [optimizedImages, { optimizeImagesInDev: true }]
  ],
  nextConfig
);
  • package.json:
{
  "name": "app",
  "version": "1.0.0",
  "description": "app",
  "main": "app/index.js",
  "keywords": [],
  "scripts": {
    "dev": "nodemon app/server --exec babel-node",
    "build": "next build app",
    "start": "NODE_ENV=production node app",
    "start:pre-test": "yarn install && yarn build && rimraf node_modules && yarn install --prod && yarn start",
    "test": "jest --coverage",
    "test:watch": "yarn test --watch",
    "test:update-snapshot": "yarn test -u",
    "remote-test": "node tests/startRemoteRunner.js",
    "lint": "npm-run-all lint:js lint:style",
    "lint:js": "eslint --fix .",
    "lint:style": "stylelint --fix \"**/*.css, **/*.scss\"",
    "clean": "npm-run-all clean:build clean:test clean:analyze",
    "clean:build": "rimraf app/.next",
    "clean:test": "rimraf coverage",
    "clean:analyze": "rimraf app/bundles",
    "analyze": "BUNDLE_ANALYZE=both yarn build",
    "analyze:client": "BUNDLE_ANALYZE=client yarn build",
    "analyze:server": "BUNDLE_ANALYZE=server yarn build",
    "integration": "testcafe chrome tests/feature --hostname localhost",
    "jenkins-integration": "testcafe ${BROWSER} tests/feature --hostname localhost --reporter spec,xunit:reports/integration-report-${BROWSER}.xml"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "pre-push": "yarn test"
    }
  },
  "lint-staged": {
    "*.js": [
      "eslint --fix",
      "git add"
    ],
    "*.css": "stylelint",
    "*.scss": "stylelint --syntax=scss",
    "*.{json,md}": [
      "prettier --write",
      "git add"
    ]
  },
  "babel": {
    "presets": [
      "@babel/preset-env",
      "next/babel"
    ],
    "plugins": [
      "lodash",
      "@babel/plugin-proposal-export-default-from",
      "@babel/plugin-proposal-export-namespace-from"
    ],
    "env": {
      "production": {
        "plugins": [
          "transform-remove-console",
          "transform-react-remove-prop-types"
        ]
      }
    }
  },
  "eslintConfig": {
    "parser": "babel-eslint",
    "extends": [
      "plugin:lodash/recommended",
      "airbnb",
      "prettier",
      "prettier/react"
    ],
    "plugins": [
      "lodash",
      "react",
      "prettier"
    ],
    "env": {
      "browser": true,
      "node": true,
      "es6": true,
      "jest": true
    },
    "rules": {
      "global-require": "off",
      "camelcase": "off",
      "no-console": "off",
      "no-multi-assign": "off",
      "no-underscore-dangle": "off",
      "no-param-reassign": "off",
      "no-script-url": "off",
      "react/no-array-index-key": "off",
      "react/forbid-prop-types": "off",
      "react/no-danger": "off",
      "react/jsx-filename-extension": [
        "error",
        {
          "extensions": [
            ".js"
          ]
        }
      ],
      "jsx-a11y/anchor-is-valid": "off",
      "jsx-a11y/html-has-lang": "off",
      "import/named": "off",
      "import/no-dynamic-require": "off",
      "import/no-extraneous-dependencies": "off",
      "lodash/prefer-includes": "off",
      "lodash/prefer-lodash-method": "off",
      "lodash/prefer-lodash-typecheck": "off",
      "lodash/prefer-constant": "off",
      "lodash/import-scope": "off",
      "lodash/prefer-noop": "off",
      "prettier/prettier": [
        "error",
        {
          "singleQuote": true
        }
      ]
    }
  },
  "eslintIgnore": [
    "app/.next",
    "app/**/__snapshots__",
    "coverage"
  ],
  "stylelint": {
    "extends": [
      "stylelint-config-standard",
      "stylelint-config-recommended-scss",
      "stylelint-config-prettier"
    ],
    "plugins": [
      "stylelint-scss"
    ],
    "rules": {
      "string-quotes": "single"
    }
  },
  "nodemonConfig": {
    "watch": [
      "app/server",
      "app/langs"
    ]
  },
  "browserslist": [
    "> 1%",
    "last 2 versions"
  ],
  "jest": {
    "setupTestFrameworkScriptFile": "<rootDir>/jest/setup.js",
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/file.js",
      "\\.(css|scss)$": "<rootDir>/__mocks__/style.js"
    },
    "collectCoverageFrom": [
      "app/components/**/*.js",
      "!app/components/index.js",
      "!app/components/Head/index.js",
      "app/pages/*.js",
      "!app/pages/_document.js",
      "!app/pages/_app.js",
      "!app/pages/_error.js",
      "!app/pages/incapable.js",
      "!app/pages/megaPack.js"
    ]
  },
  "dependencies": {
    "@babel/plugin-proposal-export-default-from": "7.2.0",
    "@babel/plugin-proposal-export-namespace-from": "7.2.0",
    "@babel/register": "7.0.0",
    "@zeit/next-bundle-analyzer": "^0.1.2",
    "@zeit/next-sass": "^1.0.1",
    "autoprefixer": "^9.4.2",
    "axios": "^0.18.0",
    "babel-plugin-lodash": "^3.3.4",
    "babel-plugin-transform-react-remove-prop-types": "^0.4.21",
    "babel-plugin-transform-remove-console": "^6.9.4",
    "classnames": "^2.2.6",
    "compression": "^1.7.3",
    "cookie-parser": "^1.4.3",
    "express": "^4.16.4",
    "glob": "^7.1.3",
    "helmet": "^3.15.0",
    "imagemin-mozjpeg": "^8.0.0",
    "imagemin-optipng": "^6.0.0",
    "imagemin-svgo": "^7.0.0",
    "intl": "^1.2.5",
    "js-cookie": "^2.2.0",
    "kks-react-components": "git+ssh://[email protected]:kkstream/kks-react-components.git#v0.1.13",
    "lodash": "^4.17.11",
    "lodash-webpack-plugin": "^0.11.5",
    "moment": "^2.22.2",
    "negotiator": "^0.6.1",
    "next": "^7.0.2",
    "next-compose-plugins": "^2.1.1",
    "next-optimized-images": "^2.0.1",
    "next-redux-wrapper": "^2.0.0",
    "next-routes": "^1.4.2",
    "prop-types": "^15.6.2",
    "query-string": "^6.2.0",
    "react": "^16.5.2",
    "react-dom": "^16.5.2",
    "react-intl": "^2.7.2",
    "react-redux": "^6.0.0",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0",
    "ua-parser-js": "^0.7.19",
    "uuid": "^3.3.2",
    "yapi-adapter-ui-react": "git+ssh://[email protected]/kkstream/yapi-adapter-ui-react.git#1.0.14"
  },
  "devDependencies": {
    "@babel/node": "^7.2.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^23.6.0",
    "chai": "^4.2.0",
    "enzyme": "^3.8.0",
    "enzyme-adapter-react-16": "^1.7.1",
    "eslint": "^5.8.0",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-config-prettier": "^3.3.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jsx-a11y": "^6.1.1",
    "eslint-plugin-lodash": "^5.0.0",
    "eslint-plugin-prettier": "^3.0.0",
    "eslint-plugin-react": "^7.11.0",
    "husky": "^1.2.0",
    "jest": "^23.6.0",
    "lint-staged": "^8.1.0",
    "nock": "^10.0.4",
    "node-sass": "^4.11.0",
    "nodemon": "^1.18.8",
    "normalize.css": "^8.0.1",
    "npm-run-all": "^4.1.5",
    "postcss-easy-import": "^3.0.0",
    "postcss-url": "^8.0.0",
    "prettier": "^1.15.3",
    "react-test-renderer": "^16.5.2",
    "rimraf": "^2.6.2",
    "stylelint": "^9.9.0",
    "stylelint-config-prettier": "^4.0.0",
    "stylelint-config-recommended-scss": "^3.2.0",
    "stylelint-config-standard": "^18.2.0",
    "stylelint-scss": "^3.4.0",
    "testcafe": "^0.23.2",
    "testcafe-reporter-xunit": "^2.1.0"
  }
}

Cleanup the plugin

The plugin grew quickly which is very nice but it should now be cleaned up/refactored a bit so it still is easy to understand, to maintain and to add more functionality.

  • Split the index.js file into multiple smaller files, maybe one per loader and their options
  • Use babel to use modern js features while still supporting node 6 and to make the code in svg-runtime-generator.js a bit less verbose (use jsx and don't write React.createElement code)
  • Add tests for the newly added functionalities (the new config options, svg sprites and the runtime generator)
  • Add contributing guidelines, issue & pr templates
  • Add code linting

Some feature improvements:

  • if outputPath is specified and relative, make sure it uses the same path on the server & client
  • svg sprites contain all sprites of all pages in the memory of the server, we should only include the sprites on the current page
  • provide a component for the webp fallback

Unable to configure imagesName and use webp

👋 I'm using next-optimized-images with the following config:

{ imagesName: "[name].[ext]?h=[hash]" }

All works well and I'm getting images paths like http://localhost:3000/_next/static/images/something.jpg?h=aacf4dd62f3618484aa2e017041bb9f2. Having hash as a GET param is allowing users to avoid 404 pages if the images change and the app is rebuilt. For instance, if google indexes logo.jpg?h=abc and then the URL becomes logo.jpg?h=def, the logo in the search results is still a valid image. The only thing I have to worry about with this config is the uniqueness of paths, but that's a different story.

After trying webp-loader with ?webp / <picture> / <source> / <image> I've noticed an odd glitch.

Instead of getting something.jpg?h=xyz and something.webp?h=zyx for require("./something.jpg") / require("./something.jpg?webp"), the output paths are something.jpg?h=xyz and something.jpg?h=zyx.webp.

Unsetting imagesName in the config solves the issue by turning paths into something-xyz.jpg and something-zyx.webp, but that's not quite what I'm after.

What makes the URL be something.jpg?h=zyx.webp and not something.webp?h=zyx? How to overcome this?

SCSS absolute image urls are not transformed

I'm really having a hard time making this plugin work with my SASS files, so to make sure it's not my own complex configuration that is in the way, I created a new project from scratch just to test the plugin and try and make it work with CSS url.

The simple project

// pages/styles.scss
.test {
  background-image: url('/static/test.jpg'); // this is not transformed
}
// pages/index.js
import React from 'react';
import './styles.scss';

export default () => <div className="test">Welcome to next.js!</div>
// next.config.js
const withSass = require('@zeit/next-sass');
const withOptimizedImages = require('next-optimized-images');

module.exports = withOptimizedImages(withSass({
  assetPrefix: 'http://localhost:3000/',
}));

And the package.json

{
  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "next": "^8.0.0",
    "next-optimized-images": "^2.3.3",
    "node-sass": "^4.11.0",
    "react": "^16.8.1",
    "react-dom": "^16.8.1"
  },
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start"
  }
}

Note that our other project is on Next v7 (if that changes anything).

Expected behaviour

Since I'm using the assetPrefix Next config option, I'm hoping to see my image URL within the compiled CSS to change to something like this: http://localhost:3000/_next/static/test-hashHere.jpg.

Current behaviour

It's not changing at all. I'm seeing the image since the assetPrefix is the same as my default dev server and Next is correctly parsing the image since if I change the path, it fails to compile.

Adding ?sprite to the module doesn't work

Hello,

My ESLint is complaining when I try to import a file as an svg sprite as follow:

import idea from '../static/images/icons/idea.svg?sprite';

I added comments to ignore the error and it worked. When I tried to test the component with jest it failed to import the file.

eslint + flow support

my project uses eslint + flow

importing images generally works because they can be found on the file system, but when adding query params, this throws off both eslint and flow.

my workaround right now is to update .flowconfig to strip the query

[ignore]

[include]

[libs]

[lints]

[options]
module.name_mapper='^\(.+\.\(png\|gif\|jpg\|webp\)\)\?\(url\|original\|include\|inline\|webp\|sprite\)$' -> '\1'

[strict]

and to add // eslint-disable-line import/no-unresolved on the import line when i do use a query.

not sure what the best way to fix this. either some extra settings for eslint-plugin-import or to have a different api for how to trigger special actions that don't involve changing up the import line.

either way, this might not be a bug for this repo, but filing it here, in case others run into this. feel free to close.

Error on require image

Hi :)

After install your module, i try to require an png image :

const src = require('./glouman-company-homepage.png');

But i've an error :

 error  in ./src/client/components/HomeBanner/glouman-company-homepage.png

Module build failed (from /home/project-name/node_modules/url-loader/index.js):
TypeError: Cannot read property 'context' of undefined
    at Object.loader (/home/project-name/node_modules/file-loader/dist/index.js:34:49)
    at Object.module.exports (/home/project-name/node_modules/url-loader/index.js:37:25)

I've been create next.config.js file on same path level pages.

I use latest [email protected]. Anyone have idea ?

Compatible with "next export"?

I wonder if this plugin applies when exporting using next export command, which generates a static build. I believe it should since all the operations are doing at the webpack level (AFAIK), but I could be wrong.

Cannot find module "../static/icons/my-icon"

Hi,

when I try to use any of the query params like "?url" I get an error Cannot find module "../static/icons/my-icon"

so this works:
<img src={require('../static/icons/my-icon.svg')} width="100%" height="100%"/>
... but this doesn't:
<img src={require('../static/icons/my-icon.svg?url')} width="100%" height="100%"/>

(I'm using Typescript)

Thanks in advance!

feature request: lqip

using something like lqip, i think it would be useful to import and image and request a low quality image placeholder to be generated (or to just extract the dominant color instead).

you likely could use these loaders independently, but i like the decision of this library to use url query params to drive signaling of intent on how to handle the image and i could imagine a ?lqip or ?lqip-dominant-color as extensions of this module.

for now, i'm just generating these myself, but in the spirit of DRY, this might be a useful direction to consider. i'ld be interested if you felt the same way or had previously considered and rejected the idea.

strange inline data uris with next 6

Hi @cyrilwanner this is a great idea, but having some issues getting it working. My inline images are being generated with data uris like this:

data:image/png;base64,bW9kdWxlLmV4cG9ydHMgPSBfX3dlYnBhY2tfcHVibGljX3BhdGhfXyArICJkMzI1YWI2YzI3ZWEyYzg5NzRmNGMyYjQ5MDI2NGE4YS5wbmciOw==

which, when decoded, contains text content like this:

module.exports = __webpack_public_path__ + "d325ab6c27ea2c8974f4c2b490264a8a.png";

which clearly isn't an image. Is this an incompatibility with next 6 situation I'm encountering here?

Any help appreciated!

fyi. just using imagesFolder and imagesPublicPath config keys.

Always create webp version

How can I configure this plugin to always create a webp version when requiring an image (I.e without having to require twice )?

E.g so require('./images/someimage.jpg') should create two output files, one optimised .jpg and one optimised .webp

An in-range update of webpack is breaking the build 🚨

The devDependency webpack was updated from 4.18.0 to 4.18.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

webpack is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 7 commits.

  • c51a1ba 4.18.1
  • c79c1de Merge pull request #8018 from webpack/ci/azure-windows
  • 37046a7 Add windows to azure
  • 814b85b Merge pull request #8012 from webpack/ci/azure
  • 474a9ac Add simple azure pipeline
  • 7b3a297 Merge pull request #8015 from webpack/deps/upgrade-tapable
  • 35015dd Upgrade tapable version

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Struggling with SVGO options

Hi, i'm having trouble getting the svgo options to work.
I tried the following:

module.exports = withPlugins(
  [
    withFonts,
    [withOptimizedImages, {
      svgo: {
        plugins: [
          { removeTitle: true },
          { convertPathData: false },
          { removeUselessStrokeAndFill: true },
          { removeAttrs: { attrs: '(stroke|fill)' } },
        ]
      }
    }]
  ],
  nextConfig
);

I then use the ?icon as described in the docs:

import CartIconSVG from '~/assets/icons/cart.svg?sprite';

The SVG displays fine, the sprite is added to the DOM, but titles are not stripped from the resulting SVGs.

Any help is appreciated!

Error when trying to load png

The package works great when I run it locally, but when I run it an alpine Docker container I get the following error only for png images:

> Failed to build
{ Error: (client) ./static/iphone.png 1:0
Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
[...]

My next.config.js looks like this:

const withPlugins = require('next-compose-plugins');
const optimizedImages = require('next-optimized-images');

module.exports = withPlugins([
  [optimizedImages, {
    inlineImageLimit: -1,
    optimizeImagesInDev: true
  }]
]);

And I'm importing the image like this:
<img src={require('../../static/iphone.png')} className="iphone"/>

Any ideas why this would happen?
I've already installed all these

Resizing images usage

Expectation

I should be able to resize images in development after installing responsive-loader and sharp, with no additional configuration in next.config.js.

Result

Images are not optimized in dev when I do:

<img src={`${src}?resize&size=300`} />

or using require gives me Cannot find module error:

<img src={require(`${src}?resize&size=300`)} />

Problem with SVG sprite in 2.0

When importing my SVG as sprites like this:

import React from 'react';
import MyIcon from './icons/my-icon.svg?sprite';

export default () => (
  <div>
    my page..
    <MyIcon />
  </div>
);

i run into the following error:

 ERROR  Failed to compile with 1 errors                                                                     17:01:33

 error  in ./assets/icons/search.svg?sprite

Module build failed (from ./node_modules/svg-sprite-loader/lib/loader.js):
InvalidSvg: svg-sprite-loader exception.

module.exports = "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHN2ZyB3aWR0aD0iMjRweCIgaGVpZ2h0PSIyNHB4IiB2aWV3Qm94PSIwIDAgMjQgMjQiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+CiAgICA8IS0tIEdlbmVyYXRvcjogU2tldGNoIDUyLjMgKDY3Mjk3KSAtIGh0dHA6Ly93d3cuYm9oZW1pYW5jb2RpbmcuY29tL3NrZXRjaCAtLT4KICAgIDx0aXRsZT5pY29uLXNlYXJjaDwvdGl0bGU+CiAgICA8ZGVzYz5DcmVhdGVkIHdpdGggU2tldGNoLjwvZGVzYz4KICAgIDxnIGlkPSJpY29uLXNlYXJjaCIgc3Ryb2tlPSJub25lIiBzdHJva2Utd2lkdGg9IjEiIGZpbGw9Im5vbmUiIGZpbGwtcnVsZT0iZXZlbm9kZCI+CiAgICAgICAgPHBhdGggZD0iTTE1LjMxMzQ2MTUsMTQuNDQzMTUzOCBDMTYuMjIyOTIzMSwxMy4zMzk5MjMxIDE2Ljc2OTIzMDgsMTEuOTI2MDc2OSAxNi43NjkyMzA4LDEwLjM4NDYxNTQgQzE2Ljc2OTIzMDgsNi44NTg1IDEzLjkxMDczMDgsNCAxMC4zODQ2MTU0LDQgQzYuODU4NSw0IDQsNi44NTg1IDQsMTAuMzg0NjE1NCBDNCwxMy45MTA3MzA4IDYuODU4NSwxNi43NjkyMzA4IDEwLjM4NDYxNTQsMTYuNzY5MjMwOCBDMTEuOTI2MDc2OSwxNi43NjkyMzA4IDEzLjMzOTg4NDYsMTYuMjIyOTIzMSAxNC40NDMxNTM4LDE1LjMxMzQ2MTUgTDE5LjEwMzMwNzcsMTkuOTczNjE1NCBMMTkuOTczNjE1NCwxOS4xMDMzNDYyIEwxNS4zMTM0NjE1LDE0LjQ0MzE1MzggWiBNNS4yMzA3NjkyMywxMC4zODQ2MTU0IEM1LjIzMDc2OTIzLDkuMDA3OTYxNTQgNS43NjY4NDYxNSw3LjcxMzczMDc3IDYuNzQwMzA3NjksNi43NDAzMDc2OSBDNy43MTM3MzA3Nyw1Ljc2Njg4NDYyIDkuMDA3OTYxNTQsNS4yMzA3NjkyMyAxMC4zODQ2MTU0LDUuMjMwNzY5MjMgQzExLjc2MTI2OTIsNS4yMzA3NjkyMyAxMy4wNTU1LDUuNzY2ODg0NjIgMTQuMDI4OTIzMSw2Ljc0MDMwNzY5IEMxNS4wMDIzNDYyLDcuNzEzNzMwNzcgMTUuNTM4NDYxNSw5LjAwNzk2MTU0IDE1LjUzODQ2MTUsMTAuMzg0NjE1NCBDMTUuNTM4NDYxNSwxMS43NjEyNjkyIDE1LjAwMjM0NjIsMTMuMDU1NSAxNC4wMjg5MjMxLDE0LjAyODkyMzEgQzEzLjA1NTUsMTUuMDAyMzg0NiAxMS43NjEyNjkyLDE1LjUzODQ2MTUgMTAuMzg0NjE1NCwxNS41Mzg0NjE1IEM5LjAwNzk2MTU0LDE1LjUzODQ2MTUgNy43MTM3MzA3NywxNS4wMDIzODQ2IDYuNzQwMzA3NjksMTQuMDI4OTIzMSBDNS43NjY4NDYxNSwxMy4wNTU1IDUuMjMwNzY5MjMsMTEuNzYxMjY5MiA1LjIzMDc2OTIzLDEwLjM4NDYxNTQgWiIgaWQ9IkltcG9ydGVkLUxheWVycyIgZmlsbD0iIzAwMDAwMCI+PC9wYXRoPgogICAgPC9nPgo8L3N2Zz4="
    at Object.loader (/Users/tillhinrichs/Documents/_DEVELOPER/HTML/CleanCleanShop/cc-next2/node_modules/svg-sprite-loader/lib/loader.js:29:11)

 @ ./components/header/searchBox/index.js 5:0-58 6:24-27
 @ ./components/header/index.js
 @ ./pages/_app.js
 @ multi ./pages/_app.js

The source of this error is the (rather smallish) SVG being converted to Base64 before handed over to svg-sprite-loader. I think it would help to always turn of BASE64 conversion for elements having the ?sprite suffix. Is that possible?

JPG largely not working

JPG files seem to be broken in version 1.4.1. I have found that some files work fine, but for example 5 entirely random jpg files which I tested from unsplash.com failed to display. The image does get copied to the .next/images folder as per the default config, but the image is malformed and unreadable, both in the browser and on my local system.

I have tried the same setup in windows 10 as well as mac high sierra, using both nextjs 5.1 and 6.1.

My base next.config.js is as follows:

const { withPlugins, optional } = require('next-compose-plugins');
const images = require('next-optimized-images');

module.exports = withPlugins([
  [images, {
    optimizeImagesInDev: true,
  }],
], {
  webpack: (config) => {
    // Add rules for styled-components linting, this will allow
    // us to lint styles as part of the build
    config.module.rules.push({
      test: /\.js?/,
      loader: 'stylelint-custom-processor-loader',
      exclude: /node_modules/,
      options: {
        emitWarning: false
      },
    });

    // IMPORTANT: the config must be returned
    return config;
  }
});

Test Case:

import React from 'react';
import Icon from '../static/high_low_big.svg?sprite';
import Image from '../static/nathan-peterson-792622-unsplash.jpg?original';

export default () => {
  const imgStyle = {
    maxWidth: '100%',
  };
  return (
    <div>
      <Icon/> // SVG works as expected
      <img style={imgStyle} src={Image} /> // Image does not render, makes no difference if I change `?original` to `?include`, or remove it at all.
    </div>
  );
};

The path which I am referencing is correctly being updated to point to /_next/static/images/nathan-peterson-792622-unsplash-b7e78930f997020df3638a54a25421f8.jpg, but this image is malformed and not displaying.

Styled-jsx ignored

Hello all,

I'm facing an issue regarding <style jsx>{``}</style> in my components.
When I want to put an image inline through SCSS files or with import / require statements in JS files it's working fine.
But not with style jsx, so I'm wondering if this plugin supports parsing <style jsx> ?

Someone has something working with style jsx ?

I'm using '[email protected]' + '@zeit/[email protected]' in the project.

Thank you !

Memory Exposure

┌───────────────┬──────────────────────────────────────────────────────────────┐
│ Moderate      │ Memory Exposure                                              │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package       │ tunnel-agent                                                 │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in    │ >=0.6.0                                                      │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ next-optimized-images [dev]                                  │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path          │ next-optimized-images > img-loader > imagemin-gifsicle >     │
│               │ gifsicle > bin-wrapper > download > caw > tunnel-agent       │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info     │ https://nodesecurity.io/advisories/598                       │
└───────────────┴──────────────────────────────────────────────────────────────┘

Dependency versions:
https://unpkg.com/img-loader/package.json
https://unpkg.com/imagemin-gifsicle/package.json
https://unpkg.com/gifsicle/package.json
https://unpkg.com/bin-wrapper/package.json
https://unpkg.com/download/package.json
https://unpkg.com/caw/package.json
https://unpkg.com/tunnel-agent/package.json

The plugin is trying to load ai files

For some reason since including the plugin again, I'm getting an error where it's trying to load .ai files from the directory. Is this intended effect?

Failed to compile --> svg?sprite

Hi Cyril,

thanks for the great package.
I followed the steps to import svgs and use them as components as described.
But i get the following error:
error

I expected that i can import my svg file from any location and use it as a react component.

Below you can find my setup. I used yarn for everything instead of npm.

For Example:

import SVGLogo from "components/atoms/Icon/svg/icon.svg?sprite";
const Logo = props => <SVGLogo {...props} />;

My package.json:

"next-compose-plugins": "^2.1.1",
"next-optimized-images": "^2.4.0",
"imagemin-svgo": "^7.0.0",
"svg-sprite-loader": "^4.1.3"

My next.config.js:

const path = require("path");
const withPlugins = require("next-compose-plugins");
const optimizedImages = require("next-optimized-images");

module.exports = withPlugins([
  optimizedImages,
  {
    webpack(config) {
      config.resolve.alias["components"] = path.join(
        __dirname,
        "src/components"
      );
      config.resolve.alias["containers"] = path.join(
        __dirname,
        "src/containers"
      );
      config.resolve.alias["helpers"] = path.join(__dirname, "src/helpers");
      config.resolve.alias["api"] = path.join(__dirname, "src/api");
      config.resolve.alias["store"] = path.join(__dirname, "src/store");
      config.resolve.alias["routes"] = path.join(__dirname, "src/routes");
      config.resolve.alias["config"] = path.join(__dirname, "src/config");
      config.resolve.alias["theme"] = path.join(__dirname, "src/theme");
      return config;
    }
  }
]);

The svg:

<?xml version="1.0" encoding="UTF-8"?>
<svg width="16px" height="16px" viewBox="0 0 16 16" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <!-- Generator: Sketch 50.2 (55047) - http://www.bohemiancoding.com/sketch -->
    <title>checkmark_icon_yellow_16x16</title>
    <desc>Created with Sketch.</desc>
    <defs></defs>
    <g id="checkmark_icon_yellow_16x16" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
        <path d="M4.42713499,14 C4.58635551,14 5.11377272,12.9303514 5.65183073,12.0857392 C6.18988874,11.241127 6.36004148,11.0812998 7.45024032,9.68415037 C8.54043915,8.2870009 9.91312561,6.86509848 10.5727949,6.15566296 C11.2324641,5.44622744 11.9442662,4.6606632 12.777217,3.90199133 C13.6101677,3.14331945 14.4401795,2.32140257 14.7217339,2.08679596 C15.0032882,1.85218935 15.7681535,1.25794974 15.9805153,1.0520364 C16.192877,0.846123065 14.6028586,1.31369365 14.4401795,1.37412704 C14.2775005,1.43456043 13.672807,1.66763038 13.0151744,2.08679596 C12.3575418,2.50596154 12.0047149,2.78625982 11.1562836,3.45057535 C10.3078524,4.11489087 9.38002723,4.97141877 8.21828419,6.15566296 C7.05654115,7.33990715 6.12186959,8.27217794 5.51055283,9.01063837 C4.89923608,9.7490988 4.61834264,10.2797941 4.42713499,10.2797941 C4.23592733,10.2797941 3.59268814,9.32926711 3.25545504,9.01063837 C2.91822194,8.69200964 2.4414883,8.35566069 1.80824018,8.27217794 C1.43656573,8.22317912 0.84577424,8.2623438 0.0358657135,8.38967198 L0.0358627677,8.38965324 C0.0132618708,8.39320639 -0.00217937452,8.41440844 0.00137378095,8.43700934 C0.00436327546,8.4560249 0.0200574201,8.47047572 0.0392545824,8.47188914 C0.880603107,8.87432406 1.22572926,9.14536677 1.42022458,9.31046185 C1.71196755,9.55810447 2.09175193,10.009021 2.40165647,10.448581 C2.71156101,10.888141 2.97486489,11.2928475 3.41587546,12.1752739 C3.85688603,13.0577003 4.26791446,14 4.42713499,14 Z" id="Path-179" fill="#000000"></path>
    </g>
</svg>

I also adjusted the _document.js and added:

const spriteContent = sprite.stringify();
...
return {
        ...initialProps,
        spriteContent,
        styles: (
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>
        )
      };
...
<div dangerouslySetInnerHTML={{ __html: this.props.spriteContent }} />

Deployment error with now 2.0

Hi,

I'm getting the following error for all my images when running the now command (dev mode and prod builds are fine).
In my TypeScript code I include the images via require("../static/icons/logo.svg?include") or similar.

ModuleNotFoundError: Module not found: Error: Can't resolve '../static/icons/logo.svg?include' in '/tmp/48d87981/components'
    at factory.create (/tmp/48d87981/node_modules/webpack/lib/Compilation.js:821:10)
    at factory (/tmp/48d87981/node_modules/webpack/lib/NormalModuleFactory.js:397:22)
    at resolver (/tmp/48d87981/node_modules/webpack/lib/NormalModuleFactory.js:130:21)
    at asyncLib.parallel (/tmp/48d87981/node_modules/webpack/lib/NormalModuleFactory.js:224:22)
    at /tmp/48d87981/node_modules/neo-async/async.js:2825:7
    at /tmp/48d87981/node_modules/neo-async/async.js:6886:13
    at normalResolver.resolve (/tmp/48d87981/node_modules/webpack/lib/NormalModuleFactory.js:214:25)
    at doResolve (/tmp/48d87981/node_modules/enhanced-resolve/lib/Resolver.js:184:12)
    at hook.callAsync (/tmp/48d87981/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/tmp/48d87981/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:15:1)
    at resolver.doResolve (/tmp/48d87981/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:37:5)
    at hook.callAsync (/tmp/48d87981/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/tmp/48d87981/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:15:1)
    at hook.callAsync (/tmp/48d87981/node_modules/enhanced-resolve/lib/Resolver.js:238:5)
    at _fn0 (eval at create (/tmp/48d87981/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:12:1)
    at resolver.doResolve (/tmp/48d87981/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:42:38)
resolve '../static/icons/logo.svg?include' in '/tmp/48d87981/components'
  using description file: /tmp/48d87981/package.json (relative path: ./components)
    Field 'browser' doesn't contain a valid alias configuration
    using description file: /tmp/48d87981/package.json (relative path: ./static/icons/logo.svg)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        /tmp/48d87981/static/icons/logo.svg doesn't exist
      .wasm
        Field 'browser' doesn't contain a valid alias configuration
        /tmp/48d87981/static/icons/logo.svg.wasm doesn't exist
      .mjs
        Field 'browser' doesn't contain a valid alias configuration
        /tmp/48d87981/static/icons/logo.svg.mjs doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        /tmp/48d87981/static/icons/logo.svg.js doesn't exist
      .jsx
        Field 'browser' doesn't contain a valid alias configuration
        /tmp/48d87981/static/icons/logo.svg.jsx doesn't exist
      .json
        Field 'browser' doesn't contain a valid alias configuration
        /tmp/48d87981/static/icons/logo.svg.json doesn't exist
      .ts
        Field 'browser' doesn't contain a valid alias configuration
        /tmp/48d87981/static/icons/logo.svg.ts doesn't exist
      .tsx
        Field 'browser' doesn't contain a valid alias configuration
        /tmp/48d87981/static/icons/logo.svg.tsx doesn't exist
      as directory
        /tmp/48d87981/static/icons/logo.svg doesn't exist

(next 8 canary, next-optimized-images 2.3.3, now 2.0)

Thanks in advance

Can't import directly from absolute `static` path.

In my app I pass routes to image files to have them displayed in many different components. How can I get next-optimized-routes to work with absolute routes? The documentation mentions only a relative import: ./images/img.jpg.

This is an import that works fine with regular next.js:

<img src="/static/img/logo.png" />

next-optimized-images route that also works:

<img src={require("../../static/img/logo.png")} />

next-optomized-images that doesn't work but should:

<img src={require("/static/img/logo.png")} />

Error message:

./src/components/Component1.js
Module not found: Can't resolve '/static/img/logo.png' in '/Users/???/Projects/react-projects/???/???/src/components'

Optimize webp images

Currently, jpeg/png images can get converted to webp with the ?webp query param but you can't require a .webp image.

This should be possible and the same optimization options should get applied.

Stylus support

How could one makes this plugin work with stylus url()?
It is being ignored.

webp for background-image

How can I achieve something similar to:

'<source srcSet={require('./images/my-image.jpg?webp')} type="image/webp" /><source srcSet={require('./images/my-image.jpg')} type="image/jpeg" /><img src={require('./images/my-image.jpg')} />'

for a background-image? Is it currently possible?

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Unable to configure hash after upgrading to 2.0

Hi again @cyrilwanner 👋

I'm curious how difficult would it be to customise hash, similar to how it used to work in v1. Back then my config looked like so, according to what file-loader accepts:

{ imagesName: "[name].[ext]?h=[sha512:hash:base64:4]" }

The trick with the hash formatting stopped working after upgrading next-optimized-images to v2, so my config had to become:

{ imagesName: "[name].[ext]?h=[hash]" }

Otherwise, the following error shows up:

Module build failed (from ../node_modules/url-loader/dist/cjs.js):
Error: Digest method not supported
    at new Hash (internal/crypto/hash.js:33:18)
    at Object.createHash (crypto.js:101:10)
    at getHashDigest (/path/to/project/node_modules/loader-utils/lib/getHashDigest.js:46:34)
    at url.replace (/path/to/project/node_modules/loader-utils/lib/interpolateName.js:96:11)
    at String.replace (<anonymous>)
    at Object.interpolateName (/path/to/project/node_modules/loader-utils/lib/interpolateName.js:93:8)
    at Object.loader (/path/to/project/node_modules/file-loader/dist/index.js:24:36)
    at Object.loader (/path/to/project/node_modules/url-loader/dist/index.js:76:19)

I'm wondering if hash could be made customisable again, which should help shorten asset paths if desired. WDYT?

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

Cannot use SVG-Files

Hi, thanks for your plugin...

when i try this:

img alt="logo" src={require('../static/logo_white.svg')} className="welcomePageWelcomeInnerLogo" />

i get this issue:

Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
> <?xml version="1.0" encoding="UTF-8"?>
| <svg width="1254px" height="279px" viewBox="0 0 1254 279" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
|  <!-- Generated by Pixelmator Pro 1.1.4 -->

How can i solve the issue? My next.config.js looks like this:

//const withImages = require('next-images');
const withCSS = require('@zeit/next-css');
const withPlugins = require('next-compose-plugins');
const optimizedImages = require('next-optimized-images');

module.exports = withPlugins([
    [optimizedImages, {
      /* config for next-optimized-images */
    }],
    withCSS()
  ]);

Another question... i have a backgroundImage as .jpg file. I set this Image as BackgroundImage with CSS like this:

const setbgImage = {
    backgroundImage: `url('../static/background.jpg')`
};

How can i use this with the fowolling example to use webp?

picture>
    <source srcSet={require('./images/my-image.jpg?webp')} type="image/webp" />
    <source srcSet={require('./images/my-image.jpg')} type="image/jpeg" />
    <img src={require('./images/my-image.jpg')} />
  </picture>

Now deploy error

Hello. I get this error when I deploy.

now

'(server) ./static/images/google-plus-icon.svg\nModule build failed: TypeError: csstree.translate is not a function\n

next.config.js

const withSass = require('@zeit/next-sass')
const withImages = require('next-images');
const withOptimizedImages = require('next-optimized-images');
module.exports = withSass(withOptimizedImages());

iOS <=8 Safari throws error

Dependencies used:

Since svg-runtime-generator.js is not being transpiled by babel we get a lot of errors in older iOS Safari versions regarding inappropriate usage of const declaration.

Transpiled bundle part which fails on older Safaris looks as the following:

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "./node_modules/react/index.js");
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var svg_baker_runtime_browser_symbol__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! svg-baker-runtime/browser-symbol */ "./node_modules/svg-baker-runtime/browser-symbol.js");
/* harmony import */ var svg_baker_runtime_browser_symbol__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(svg_baker_runtime_browser_symbol__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var svg_sprite_loader_runtime_browser_sprite_build__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! svg-sprite-loader/runtime/browser-sprite.build */ "./node_modules/svg-sprite-loader/runtime/browser-sprite.build.js");
/* harmony import */ var svg_sprite_loader_runtime_browser_sprite_build__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(svg_sprite_loader_runtime_browser_sprite_build__WEBPACK_IMPORTED_MODULE_2__);

    const symbol = new svg_baker_runtime_browser_symbol__WEBPACK_IMPORTED_MODULE_1___default.a({
  "id": "sample--sprite",
  "use": "sample--sprite-usage",
  "viewBox": "0 0 13 13",
  "content": "<symbol ... /></symbol>"
});
    svg_sprite_loader_runtime_browser_sprite_build__WEBPACK_IMPORTED_MODULE_2___default.a.add(symbol);

    const SvgSpriteIcon = function SvgSpriteIcon(props) {
      return react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement(
        'svg',
        Object.assign({
          viewBox: symbol.viewBox,
        }, props),
        react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement(
          'use',
          {
            xlinkHref: '#' + symbol.id,
          }
        )
      );
    };

    SvgSpriteIcon.viewBox = symbol.viewBox;
    SvgSpriteIcon.id = symbol.id;
    SvgSpriteIcon.content = symbol.content;
    SvgSpriteIcon.url = symbol.url;
    SvgSpriteIcon.toString = symbol.toString;

    /* harmony default export */ __webpack_exports__["default"] = (SvgSpriteIcon);
/***/ }),

Do we need to make a manual transpilation during runtime? Any suggestions are welcome. Thank you.

Need update dependencies

Hello.
Could you please update dependencies of this plugin?

After installing this plugin I got alert from npm:
found 9 moderate severity vulnerabilities in 21838 scanned packages 9 vulnerabilities require manual review. See the full report for details.

I will very appreciate to you.

example?

it would be very helpful to have a demo project to see how this all works in the wild

Resizing images

Would you consider resizing images to be a part of the design goals of this module? It would be very convenient to keep original images in the project directory and in the importing code specify require('./images/my-photo.jpg?resize=1000px')}. From my point of view, resizing images down is a similar semantically operation as minifying with mozjpeg. What do you think?

img-loader as peer dependency

Hi there,

When I use a a custom webpack config with Next.js I need to also img-loader for this plugin to work. This would mean that in node_modules there is img-load at the root and also inside node_modules of next-optimized-images.

It feels like having img-loader as a peer dependency of next-optimized makes sense?

Problem on install

Environment

OS: Ubuntu:18
npm: 5.6.0
node: 8.11.2

install command: npm install --save next-optimized-images

I got following log:

npm WARN deprecated [email protected]: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5

[email protected] postinstall /media/bipin/052ca934-ac82-4b85-b36a-f161177524d5/workspace/murcul-org/client-juven-frontend/node_modules/pngquant-bin
node lib/install.js

⚠ The /media/bipin/052ca934-ac82-4b85-b36a-f161177524d5/workspace/murcul-org/client-juven-frontend/node_modules/pngquant-bin/vendor/pngquant binary doesn't seem to work correctly
⚠ pngquant pre-build test failed
ℹ compiling from source
✔ pngquant pre-build test passed successfully
✖ Error: pngquant failed to build, make sure that libpng-dev is installed
at Promise.all.then.arr (/media/bipin/052ca934-ac82-4b85-b36a-f161177524d5/workspace/murcul-org/client-juven-frontend/node_modules/pngquant-bin/node_modules/bin-build/node_modules/execa/index.js:231:11)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
npm WARN [email protected] requires a peer of react@^0.14.0 || ^15.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of stylis@^3.5.0 but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] No repository field.
npm WARN [email protected] No license field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] postinstall: node lib/install.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Plugin crashing docker build with node-alpine image

While trying to create a docker image taking node-alpine 8.11.0 as base image, the install steps of the deps of this plugin are crashing the build process. Here is relevant portion of build log:

[email protected] postinstall /frontend/node_modules/cwebp-bin

node lib/install.js

⚠ spawn /frontend/node_modules/cwebp-bin/vendor/cwebp ENOENT
⚠ cwebp pre-build test failed
ℹ compiling from source
✖ Error: ./configure --disable-shared --prefix="/frontend/node_modules/cwebp-bin/vendor" --bindir="/frontend/node_modules/cwebp-bin/vendor" && make && make install
Command failed: ./configure --disable-shared --prefix="/frontend/node_modules/cwebp-bin/vendor" --bindir="/frontend/node_modules/cwebp-bin/vendor"
configure: error: in /frontend/node_modules/cwebp-bin/1781a0ab-70da-4226-aea4-05978656b745': configure: error: no acceptable C compiler found in $PATH See config.log' for more details

at ChildProcess.exithandler (child_process.js:275:12)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at maybeClose (internal/child_process.js:925:16)
at Socket.stream.socket.on (internal/child_process.js:346:11)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at Pipe._handle.close [as _onclose] (net.js:567:12)

[email protected] postinstall /frontend/node_modules/gifsicle
node lib/install.js

⚠ spawn /frontend/node_modules/gifsicle/vendor/gifsicle ENOENT
⚠ gifsicle pre-build test failed
ℹ compiling from source
✖ Error: autoreconf -ivf && ./configure --disable-gifview --disable-gifdiff --prefix="/frontend/node_modules/gifsicle/vendor" --bindir="/frontend/node_modules/gifsicle/vendor" &&
make install
Command failed: autoreconf -ivf
/bin/sh: autoreconf: not found

at ChildProcess.exithandler (child_process.js:275:12)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at maybeClose (internal/child_process.js:925:16)
at Socket.stream.socket.on (internal/child_process.js:346:11)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at Pipe._handle.close [as _onclose] (net.js:567:12)

[email protected] postinstall /frontend/node_modules/mozjpeg
node lib/install.js

⚠ spawn /frontend/node_modules/mozjpeg/vendor/cjpeg ENOENT
⚠ mozjpeg pre-build test failed
ℹ compiling from source
✖ Error: autoreconf -fiv && ./configure --disable-shared --disable-dependency-tracking --with-jpeg8 --prefix="/frontend/node_modules/mozjpeg/vendor" --bindir="/frontend/node_modules/mozjpeg/vendor" --libdir="/frontend/node_modules/mozjpeg/vendor" && make -j2 && make install -j2
Command failed: autoreconf -fiv
/bin/sh: autoreconf: not found

at ChildProcess.exithandler (child_process.js:275:12)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at maybeClose (internal/child_process.js:925:16)
at Socket.stream.socket.on (internal/child_process.js:346:11)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at Pipe._handle.close [as _onclose] (net.js:567:12)

[email protected] postinstall /frontend/node_modules/optipng-bin
node lib/install.js

⚠ spawn /frontend/node_modules/optipng-bin/vendor/optipng ENOENT
⚠ optipng pre-build test failed
ℹ compiling from source
✖ Error: ./configure --with-system-zlib --prefix="/frontend/node_modules/optipng-bin/vendor" --bindir="/frontend/node_modules/optipng-bin/vendor" && make install
Command failed: ./configure --with-system-zlib --prefix="/frontend/node_modules/optipng-bin/vendor" --bindir="/frontend/node_modules/optipng-bin/vendor"

at ChildProcess.exithandler (child_process.js:275:12)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at maybeClose (internal/child_process.js:925:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)

[email protected] postinstall /frontend/node_modules/pngquant-bin
node lib/install.js

⚠ spawn /frontend/node_modules/pngquant-bin/vendor/pngquant ENOENT
⚠ pngquant pre-build test failed
ℹ compiling from source
✔ pngquant pre-build test passed successfully
✖ Error: pngquant failed to build, make sure that libpng-dev is installed
at Promise.all.then.arr (/frontend/node_modules/pngquant-bin/node_modules/bin-build/node_modules/execa/index.js:231:11)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
npm WARN [email protected] No description
npm WARN [email protected] No repository field.
npm WARN [email protected] No license field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] postinstall: node lib/install.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-04-02T11_13_47_451Z-debug.log

Are there any clean ways to skip pre and post build steps of those deps in the plugin itself to avoid any issues in containerized environment?

How to set imagesPublicPath in runtime

Currently I can use

app.setAssetPrefix('http://cdn.com/myapp')

to set the assetPrefix for next.js files in runtime, that way I can reuse my build file for both staging and production environment

Is it possible to set imagesPublicPath in runtime too?

support node v6

nextjs currently supports node v6, but using next-optimized-images fails when imported with the following

./node_modules/next-optimized-images/index.js:93
          ),
          ^

SyntaxError: Unexpected token )

i believe this is because node v6 doesn't support trailing commas. there might be other issues, this is just the first error i hit. i'm exploring now if v6 is fully unsupported or if there are existing workarounds.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.