Unable to resolve absolute url() paths for background images in CSS with Webpack

后端 未结 2 642
梦谈多话
梦谈多话 2021-02-09 08:52

I have the following Webpack config (roughly, it has been simplified for this post):

const rootPublic = path.resolve(\'./public\');
const LOCAL_IDENT_NAME = \'lo         


        
2条回答
  •  伪装坚强ぢ
    2021-02-09 09:42

    With webpack 2:

    In your .scss files use ~ before the path.

    .yourClass {
          background: url('~img/wallpaper.png');
    }
    

    Make use of the resolve root from webpack, add this to your webpack.config.js:

    resolve: {
            modules: [
                path.resolve(root),
                'node_modules'
            ]
        },
    

    It should work also for the @import, eg @import "~otherfile.scss"

提交回复
热议问题