Skip to content
This repository was archived by the owner on Aug 27, 2026. It is now read-only.
This repository was archived by the owner on Aug 27, 2026. It is now read-only.

Generated image urls *must* be absolute for style!css?sourceMap to work? #55

Closed

Description

@gryzzly

Is there a way to make images load in browser when using style-loader/css-loader?sourceMap without specifying absolute url prefix as publicPath?

With this loader:

loaders.push({
  test: /\.scss$/,
  loader: 'style!css?sourceMap')
});

This works:

output = {
  path: 'build',
  publicPath: 'http://0.0.0.0:8080/'
  filename: 'bundle.js'
};

This doesn't:

output = {
  path: 'build',
  publicPath: '/'
  filename: 'bundle.js'
};

In the CSS modules generated as Blob the paths are relative to chrome:blob or chrome:devtools which doesn't refer to the correct assets.

If my assumption is correct and we always have to set absolute path in output.publicPath I will be happy to add a note about this to the docs.

Activity

  1. sokra commented on Apr 20, 2015

    @sokra
    Member

    If my assumption is correct and we always have to set absolute path in output.publicPath I will be happy to add a note about this to the docs.

    yep. Do you want to add one in a PR?

  2. clessg commented on May 11, 2015

    @clessg

    Same problem here. Bit of a confusing bug to run into.

  3. added a commit that references this issue on Jun 30, 2015
    42060ee
  4. Darkside73 commented on Jan 6, 2016

    @Darkside73

    How to deal with HTTPS? Schema-less protocol does not work

  5. tizmagik commented on Jan 14, 2016

    @tizmagik

    Same question for https

  6. Darkside73 commented on Jan 14, 2016

    @Darkside73

    I've figured out how to solve HTTPS issue. You can use your own dev server (take a look for https: true option) to serve assets. Here is the example https://gist.github.com/Darkside73/e3b70d72443e0f72b27b

  7. tizmagik commented on Jan 14, 2016

    @tizmagik

    Ah interesting. Thanks for sharing! I'll try that out.

    For now, I actually ended up going about it slightly differently. Since my main issue was being able to use url('/images/sample.png') paths in CSS I defined ?root query string parameter for the css-loader and then specified ?name=[path][name].[ext] for the url-loader. This seemed to do the trick without having to specify the full URL in publicPath.

  8. pixelcure commented on Jan 16, 2016

    @pixelcure

    @tizmagik is this how you resolved the disappearing images? was this in webpack.config.js?

  9. tizmagik commented on Jan 16, 2016

    @tizmagik

    Yes, it seems to work fine even though using root is not recommended according to the docs for whatever reason.

  10. valerymercury commented on Jan 25, 2016

    @valerymercury

    @tizmagik can you provide an example of working config, please?

  11. creeperyang commented on Jan 31, 2016

    @creeperyang

    @tizmagik It will be helpful if you can provide a sample webpack.config.js or just loaders. Thanks in advance. @valerymercury set output.publicPath is a workround.

  12. valerymercury commented on Jan 31, 2016

    @valerymercury

    @creeperyang output.publicPath isn't working for me. As @tizmagik said root option helped me to fix bug with background images in css. I resolved my problem with the following config:

    var cssRoot = env === 'development' ?
        'root=' + path.join(__dirname, 'app/assets') + '&' : ''
    
    loaders: [
        {
            test: /\.scss$/,
            include: path.resolve(__dirname, 'app/styles'),
            loader: ExtractTextPlugin.extract('style', 'css?'+ cssRoot +
                'sourceMap!postcss!sass?sourceComments&' +
                'includePaths[]=' + (path.resolve(__dirname, './bower_components/compass-mixins/lib/'))
            )
        }
    ]
    
  13. tizmagik commented on Feb 1, 2016

    @tizmagik

    @creeperyang sorry for the late reply, just saw this.

    @valerymercury yeap, that's basically what I have as well. Here's mine if it's any more helpful:

    const rootPublic = path.resolve('./public');
    const LOCAL_IDENT_NAME = 'localIdentName=[name]_[local]_[hash:base64:5]';
    const CSS_LOADER = `css?sourceMap&${LOCAL_IDENT_NAME}&root=${rootPublic}` + (DEBUG ? '&minimize' : '');
    
    // ... loaders:
    
    loaders: [
      {
        test: /\.css$/,
        loader: DEBUG ? `style!${CSS_LOADER}` : ExtractTextPlugin.extract('style', CSS_LOADER)
      }, // ...
    ]
  14. davidhouweling commented on Feb 10, 2016

    @davidhouweling

    Has this been tested with relative pathed images (e.g. url(image.png))? It seems the solution provided is for absolute path images (e.g. url(/image.png)). I can't seem to use either solution suggested.

  15. 3 remaining items

  16. Strate commented on Mar 30, 2016

    @Strate
  17. mbritton commented on Aug 15, 2016

    @mbritton

    +1

  18. simshanith commented on Oct 14, 2016

    @simshanith

    Still not working for me with an absolute public path, e.g. /build/. Even including the full public URL e.g. http://example.com/build/ does not seem to work.

  19. added a commit that references this issue on Dec 6, 2016
  20. Pines-Cheng commented on Jan 14, 2017

    @Pines-Cheng

    meet the same question, just working for me with an absolute public path.

  21. muralim4242 commented on Jan 27, 2017

    @muralim4242

    url(${require('app/images/Rectangle-9.jpg')})

  22. olieidel commented on Apr 1, 2017

    @olieidel

    just stumbled over this. if setting publicPath is not working for you, you probably are making the same mistake as I was by setting publicPath in the devServerconfig. it should be set in the webpack root config object in output. have a look:

    doesn't work:

    // your webpack config
    {
      devServer: {
        publicPath: 'http://127.0.0.1:8888/'
       // ... more devServer config
      }
    // ... more webpack config
    }

    works for me:

    // your webpack config
    {
      output: {
        publicPath: 'http://127.0.0.1:8888/'
        // ... more webpack output config
      }
      devServer: {
       // ... your devServer config
      }
    // ... more webpack config
    }

    be sure to only set it in dev, who knows what this would do to your production build. good luck!

  23. bohdanly commented on Apr 6, 2017

    @bohdanly

    @simshanith to make it work specify absolute publicPath for file-loader to your local server

    {
        test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2|json|xml|ico)$/,
        loader: 'file-loader',
        query: {
            outputPath: 'assets/',
            publicPath: 'http://localhost:8080/', <- here
            emitFile: true
        }
    }

    it will change URL in your CSS from relative to absolute

  24. urikphytech commented on Jun 3, 2017

    @urikphytech

    Hi! I've tried every combination I could possibly think of and I can't understand why my images aren't found...
    Here is my configuration:

    output: {
        path: distPath,
        // Set proper base URL for serving resources
        publicPath: '/',
        // The output filename of the entry chunk, relative to `path`
        // [name] - Will be set per each key name in `entry`
        filename: 'bundle.[hash].js'
      },
    
    .....
    
    // SCSS
          {
            test: /\.(css|scss)$/,
            loaders: [
              'style-loader',
              'css-loader?root=' + encodeURIComponent(appPath),
              'postcss-loader',
              'sass-loader?includePaths[]=' + encodeURIComponent(appPath)
            ]
          },
    
    {
            test: /\.(jpe?g|png|gif|svg)(\?.*)?$/i,
            loader: 'file-loader',
            options: {
              name: '[path][name].[hash].[ext]',
              publicPath: 'http://localhost:8080/', --> Added this because suggested here (in webpack 1 it worked without this
            },
          }
    

    And i'm requiring the images via scss:
    background-image: url('/assets/images/login/background_4.jpg');
    Webpack output is:
    screen shot 2017-06-03 at 7 58 07

    Which is strange, because it used to be: '/assets/images/.....' without the '/Users/uriklar/' part

    Any idea what i'm missing here??
    Thanks!
    Uri

  25. uriklar commented on Jun 4, 2017

    @uriklar

    Ok, so my issue wasn't related (I'll delete my comment if maintainers prefer), but in case someone does get here with the same problem as me, what fixes it for me was adding the: context option to file-loader (i'm actually using url-loader) with the base url that assets need to be loaded from.

  26. xyzdata commented on Jan 3, 2018

    @xyzdata
  27. robbie-hinman commented on Jan 16, 2018

    @robbie-hinman

    It seems with css sourceMaps enabled, even with publicPath set, styles (including bg images) are not applied. But what is odd is that I can see them in the inspector. If I turn the style off and on again then the style is applied.

    The below shows the page on initial load, showing a body background of paleturquoise in the dev console but the displayed background is white.
    image
    Unchecking and checking a style will then show the correct styles. If the dev console is not open you still have the issue.

    repo here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions