Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack loader for images to move them to static folder #1825

Closed
pie6k opened this issue Apr 28, 2017 · 3 comments
Closed

Webpack loader for images to move them to static folder #1825

pie6k opened this issue Apr 28, 2017 · 3 comments

Comments

@pie6k
Copy link
Contributor

pie6k commented Apr 28, 2017

I want to be able to import images like:

import image from './image.png; // image variable should have static url

My loader looks like (next.config.js):

module.exports = {
  webpack: config => {
    // handle images
    config.module.rules.push({
      test: /\.(ico|gif|png|jpg|jpeg|svg|webp)$/,
      use: [
        {
          loader: 'emit-file-loader',
          options: {
            name: 'dist/[path][name].[ext]',
          },
        },
        {
          loader: 'file-loader',
          options: {
            context: 'public',
            name: '/static/images/[name]-[hash].[ext]',
            publicPath: '/',
          },
        },
      ],
    });

    return config;
  },
};

Result is almost good.

Image file is inside .next/static/images/image-ef89065b898bb1e3e802c62e4b264417.png

image variable is /static/images/image-ef89065b898bb1e3e802c62e4b264417.png, but when trying to load image with http://localhost:3000/static/images/image-ef89065b898bb1e3e802c62e4b264417.png I get 404 error.

Any ideas?

ps. I don't want to manually move files to static folder. I want images related to some component to be in the same folder as component itself`

@pie6k pie6k changed the title Webpack Loader for images to move them to static folder Webpack loader for images to move them to static folder Apr 28, 2017
@arunoda
Copy link
Contributor

arunoda commented May 2, 2017

Currently this is not possible.
We might do something for this sooner.

@arunoda arunoda closed this as completed May 2, 2017
kpdecker added a commit to healthline/next.js that referenced this issue Jul 11, 2017
This has two fold impact:

1. Allows for arbitrary loaders to run in both node and browser environments (Fixes vercel#1825)
2. Optimizes the build time for modules.

Risks:
- Transitions all harmony code to utilize harmony end to end. Previously this had not happened, which may break users in unexpected ways. (See vercel#2508)
- Require logic changed across the board to utilize node and webpack behaviors where possible, simplifying code, but potentially breaking things in the wild.
@srghma
Copy link

srghma commented Aug 17, 2017

Any news on this?

@srghma
Copy link

srghma commented Aug 18, 2017

For anyone who is interested how to use images in components directory

kpdecker added a commit to healthline/next.js that referenced this issue Aug 19, 2017
This has two fold impact:

1. Allows for arbitrary loaders to run in both node and browser environments (Fixes vercel#1825)
2. Optimizes the build time for modules.

Risks:
- Transitions all harmony code to utilize harmony end to end. Previously this had not happened, which may break users in unexpected ways. (See vercel#2508)
- Require logic changed across the board to utilize node and webpack behaviors where possible, simplifying code, but potentially breaking things in the wild.
kpdecker added a commit to healthline/next.js that referenced this issue Oct 18, 2017
This has two fold impact:

1. Allows for arbitrary loaders to run in both node and browser environments (Fixes vercel#1825)
2. Optimizes the build time for modules.

Risks:
- Transitions all harmony code to utilize harmony end to end. Previously this had not happened, which may break users in unexpected ways. (See vercel#2508)
- Require logic changed across the board to utilize node and webpack behaviors where possible, simplifying code, but potentially breaking things in the wild.
@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants