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

problem with background url #256

Closed
jonathandion opened this issue Apr 9, 2016 · 28 comments
Closed

problem with background url #256

jonathandion opened this issue Apr 9, 2016 · 28 comments

Comments

@jonathandion
Copy link

Hi,

I'm having a problem when loading a image with url(). I would like to have background: url(../build/images/brew.jpg); but in app.css I have background: url(/images/bg.jpg);

I tried the 'root' attribute from the doc but it's doesn't work...

What I'm doing wrong ? 😢

base.scss :
background: url('../images/bg.jpg');

app.css
background: url(/images/bg.jpg);

There is my src folder :

├── src
|   ├── stylesheets
|   |    └──  base..scss
|   ├── images
|   |    └── bg.png
|   └── javascripts
|        └── index.js
.

There is my build folder :

├── src
|   ├── images
|   |    └── bg.png
|    - app.js
|    - app.css

my webpack config

const autoprefixer = require('autoprefixer')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const path = require('path')

 const sassLoaders = [
  'css-loader',
  'postcss-loader',
  'sass-loader?includePaths[]=' + path.resolve(__dirname, './src')
]

const config = {
  entry: {
    app: ['./src/index']
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loaders: ['babel-loader']
      },
      {
        test: /\.scss$/,
     loader: ExtractTextPlugin.extract('style-loader', sassLoaders.join('!'))
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        loaders: [
            'file?hash=sha512&digest=hex&name=images/[name].[ext]',
            'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
        ]
      }
    ]
  },
  output: {
    filename: '[name].js',
    path: path.join(__dirname, './build/'),
    publicPath: '/'
  },
  plugins: [
    new ExtractTextPlugin('[name].css')
  ],
  postcss: [
    autoprefixer({
      browsers: ['last 2 versions']
    })
  ],
  resolve: {
    extensions: ['', '.js', '.scss'],
    modulesDirectories: ['src', 'node_modules']
  }
}

module.exports = config

Thanks !

@psimyn
Copy link

psimyn commented Apr 14, 2016

I think one of the following should work:

background: url('src/images/bg.jpg');
background: url('images/bg.jpg');

(this one may need webpack resolve.root set to 'src')

I will doublecheck a bit later

@kevinSuttle
Copy link

Same issue here. Tried for hours to tweak config settings, and nothing worked. This seems to be happening quite a bit with this loader. The system should be more forgiving.

@satish-pokala
Copy link

+1

@fera2k
Copy link

fera2k commented Sep 13, 2016

Same here... it seems the browser doesn't even request the url

@nthonymiller
Copy link

nthonymiller commented Oct 5, 2016

👍 Same here - issue is that the url doesn't work when using an external image, but does work when the image is inlined base64 encoded.

@kumarharsh
Copy link

@psimyn I tried the solutions you've given, but nothing works. Interestingly, prefixing the url with http://localhost does load the image. So I think browsers are not loading any image in the URL when it starts with /

@x4080
Copy link

x4080 commented Dec 7, 2016

Is there any solution yet?

@psimyn
Copy link

psimyn commented Dec 7, 2016

@kumarharsh have you set publicPath in webpack config? I've used leading slash when setting publicPath and it works ok

@kumarharsh
Copy link

@psimyn yes, I'm using publicPath as /. but no luck yet. What is your final URL (in the url() property in CSS) being output as?

@mhfowler
Copy link

mhfowler commented Feb 4, 2017

I am having the same issue. Have tried different publicPath and setting root for css-loader with no luck. It works when sourceMap is disabled. But doesn't work when sourceMap is not disabled.

here is my webpack config, any help is appreciated: https://github.com/mhfowler/Webpack-Example/blob/master/webpack.config.babel.js

@silen-z
Copy link

silen-z commented Feb 9, 2017

Same problem here. Tried to use background: url(/background.png) and @font-face: src(/something.woff) with css-loader and file-loader. Only browser that works with this is IE11. I eventually switched to using url-loader for these files. I only tried serving this with webpack-dev-server

@kumarharsh
Copy link

Hey, it was I found similar problems here:
http://stackoverflow.com/questions/37288886/webpack-background-images-not-loading

Ref1: #232
Ref2: webpack-contrib/style-loader#55

In my case, it did get fixed when I removed ?sourceMap from css-loader

@ndcunningham
Copy link

You can also try using
~ before you path to the image

Notably: background-url: (~assets/image/myimage) should resolve the path

@onepixelcitizen
Copy link

Add ?url=false to your css-loader, that will disable url handling :

e.g: ['css-loader?url=false', 'sass-loader'] ...

@joshwiens joshwiens removed the question label May 1, 2017
@JacobWay
Copy link

If you try to use ~ before your path to the image. Like this: background-url: (~assets/image/myimage). You should config resolve property in your webpack like this:

resolve: {
  alias: {
    'assets': resolve('your/path/to/assets')
  }
}

@Harti
Copy link

Harti commented May 30, 2017

@JacobWay could you please specify where exactly said line has to go? The only place where I find the token resolve in my webpack.config.js is for exported modules, i.e. it'll start looking in node_modules.

@kumarharsh
Copy link

@Harti resolve is a first-level key on the webpack.config object.

wepack.config = {
  resolve: {
    ...
  }
}

@dev-johnny-gh
Copy link

@onepixelcitizen awesome man, you save my life.

@YutHelloWorld
Copy link

@onepixelcitizen it works!

@volkanunsal
Copy link

This started happening again today.

@volkanunsal
Copy link

In my case, the background image is inserted as an [object Object]. Is it the same issue?

@alexander-akait
Copy link
Member

@volkanunsal downgrade file-loader (1.0.0 is still beta)

@volkanunsal
Copy link

@evilebottnawi Thank you!

@oyeanuj
Copy link

oyeanuj commented Nov 5, 2017

@evilebottnawi @d3viant0ne Is there a recommended solution for this problem given that the thread has many different possible solutions? What is the recommended way of using local images as background image urls?

@egeersoz
Copy link

I don't think this should have been closed. There is lots of confusion around the issue still, and no official recommended way to resolve it.

@lndj
Copy link

lndj commented Dec 23, 2017

Solution: vuejs/vue-loader#481

@talss89
Copy link

talss89 commented Feb 5, 2018

@egeersoz I agree. @lndj - thanks for the link, it worked for me. For anyone put off by an unfamiliar character set:

if (options.extract) {
      return ExtractTextPlugin.extract({
        use: loaders,
        publicPath: '../../',         // The magic smoke
        fallback: 'vue-style-loader'
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }

@alexander-akait
Copy link
Member

Closing due to inactivity. Also we have many difference problem here, please create new issue if you have problem (with minimum reproducible test repo). Thanks!

@webpack-contrib webpack-contrib locked as resolved and limited conversation to collaborators Feb 13, 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