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

Unable to import images from src folder #36

Closed
malonehedges opened this issue Oct 12, 2017 · 6 comments
Closed

Unable to import images from src folder #36

malonehedges opened this issue Oct 12, 2017 · 6 comments
Labels
enhancement 🦄 Changing current behaviour, enhancing what's already there

Comments

@malonehedges
Copy link

Currently trying to import images, but without extra configuration am unable to get it to build.

...
=> [✓] App Bundled: 3521.463ms
=> Exporting Routes...
{ SyntaxError: /Users/malonehedges/projectPath/src/assets/myimage.png: Unexpected character '�' (1:0)
> 1 | �PNG
    | ^
...

"Add example configuring webpack to import images from the src folder"?

@tannerlinsley
Copy link
Contributor

tannerlinsley commented Oct 12, 2017 via email

@tannerlinsley tannerlinsley added the enhancement 🦄 Changing current behaviour, enhancing what's already there label Oct 12, 2017
@whmountains
Copy link
Contributor

I can't import images either, but the symptoms are slightly different:

import photo from '../assets/starry-night.jpg'

photo now returns a base64 data url for the broken image icon. lolwat?

@D1no
Copy link
Contributor

D1no commented Oct 17, 2017

Have you looked at: #24 (comment) ?

@DanWebb
Copy link
Contributor

DanWebb commented Oct 18, 2017

I know an example config is requested but is there a reason this isn't built in? Gatsby enables it by default and actually suggests using this method to import assets for seemingly valid reasons.

Sorry if this is already being worked on it was just something I expected to be able to do, maybe I'm spoilt.

@tannerlinsley
Copy link
Contributor

tannerlinsley commented Oct 18, 2017 via email

@tannerlinsley
Copy link
Contributor

This should be fixed in the latest version. Please upgrade both global and local packages to version 2.0.0

Make sure you import and use the withFileLoader from react-static's plugins in your static.config.js file:

import axios from 'axios'
//
import withCss from 'react-static/lib/plugins/withCssLoader'
import withFiles from 'react-static/lib/plugins/withFileLoader'

export default {
  getSiteProps: () => ({
    title: 'React Static',
  }),
  getRoutes: async () => {
    const { data: posts } = await axios.get('https://jsonplaceholder.typicode.com/posts')
    return [
      {
        path: '/',
        component: 'src/containers/Home',
      },
      {
        path: '/about',
        component: 'src/containers/About',
      },
      {
        path: '/blog',
        component: 'src/containers/Blog',
        getProps: () => ({
          posts,
        }),
        children: posts.map(post => ({
          path: `/post/${post.id}`,
          component: 'src/containers/Post',
          getProps: () => ({
            post,
          }),
        })),
      },
      {
        is404: true,
        component: 'src/containers/404',
      },
    ]
  },
  webpack: [withCss, withFiles],
}

Then you can simply import images in your js

import logo from './logo.png'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 🦄 Changing current behaviour, enhancing what's already there
Projects
None yet
Development

No branches or pull requests

5 participants