Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.

(Webpack) Fail to load PNG (or any other) images #41

Closed
sslash opened this issue May 26, 2015 · 15 comments
Closed

(Webpack) Fail to load PNG (or any other) images #41

sslash opened this issue May 26, 2015 · 15 comments
Assignees
Labels

Comments

@sslash
Copy link

sslash commented May 26, 2015

Not sure what I am doing something wrong, but my webpack config is pretty straight forward:

module:  { loaders: [ {test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192'} ]}

in a .js file:

let logo = require('./logo.png');

The error I am getting:

/Users/micgunnu/code/sap/routes/routes-rewrite/react-isomorphic-starterkit/node_modules/babel-core/lib/babel/transformation/file/index.js:604
      throw err;
            ^
SyntaxError: /Users/micgunnu/code/sap/routes/routes-rewrite/react-isomorphic-starterkit/src/shared/pages/logo.png: Unexpected character '�' (1:0)
> 1 | �PNG
    | ^
  2 |
  3 |
@RickWong
Copy link
Owner

Did you change that config in both webpack files?

@sslash
Copy link
Author

sslash commented May 26, 2015

yes

@RickWong RickWong changed the title Fail to load PNG (or any other) images (Webpack) Fail to load PNG (or any other) images May 26, 2015
@RickWong RickWong added the bug label May 26, 2015
@Semigradsky
Copy link
Contributor

Looks like babel/register expects correct js file in require

@RickWong
Copy link
Owner

Ah... it makes sense. As it currently works, babel/register does not read from the config files. You'll need to configure it in babel.server.js, or use the require("url?...") syntax.

Right now I'm investigating the option to go back to webpack-built server builds with hot-reloading, instead of using babel/register. Should make this problem go away.

@RickWong RickWong added question and removed bug labels May 27, 2015
@RickWong RickWong self-assigned this May 27, 2015
@sslash
Copy link
Author

sslash commented May 27, 2015

I see. That was the reason I wanted to check out this repo in the first place though. So I could avoid building a huge React bundle and running that from Node (babel drops optimisation when files get too big, for instance)

@RickWong
Copy link
Owner

@sslash I'm using a technique by James Long @jlongster to exclude node_modules from the server build, and let node require them at runtime.

@sslash
Copy link
Author

sslash commented May 27, 2015

Yes. Still these bundles tend to be quite large. However, using your current setup in this repo, I am still not able to require in png files. I am using require("url?./logo.png") as you propose, but it doesnt seem to locate the file. I am certain it is in the same directory and that I have included the url-loader properly. Are you able to make it work @RickWong?

@RickWong
Copy link
Owner

RickWong commented Jun 1, 2015

Haven't had a lot of time lately to release this. But the basic builds I've got aren't large at all. I'm talking about roughly 20 KB taking 1100 ms to build from a cold cache. Still having some issues with hot-reloading though...

@ericfong
Copy link

ericfong commented Jun 2, 2015

I try to remove babel/register in my project.
ES6 vs fast-reload
I go for fast-reload

@goldensunliu
Copy link

any update on this?

@Traviskn
Copy link

the webpack isomorphic tools package may help with loading images, although it will require a bit of setup. It was made to solve the problem of require() 'ing images and css on the server: https://github.com/halt-hammerzeit/webpack-isomorphic-tools

@calvinl
Copy link

calvinl commented Dec 8, 2015

@sslash: I'm having the same issue with babel-core/register on the server side. Did you ever find a solution?

@sslash
Copy link
Author

sslash commented Dec 9, 2015

nah haven't looked into this issue sry

@stkrzysiak
Copy link

When I ran into this, I was running tests from the CLI. The solution that I found was to use require.extensions via a custom compiler. Not sure if this is of any help @calvinl or others, but I thought I would share. Here is an issue that outlines the fix in more detail: bruderstein/unexpected-react#1

@RickWong
Copy link
Owner

RickWong commented Jan 2, 2016

It took a while but the server-side now uses Webpack too. That means many webpack loaders should work on the server as well including url-loader and css-loader. (Piping is also gone, the server gets instant code updates from Webpack HMR!)

So please try release ^5.0.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants