简体   繁体   English

Webpack和Sass正确处理后台:url()图像但是当与webpack-dev-server一起使用时找不到它

[英]Webpack and Sass correctly processes background: url() image but then it's not found when used with webpack-dev-server

I am using Webpack 2 and webpack-dev-server together with Sass loader, actual configuration: 我正在使用Webpack 2和webpack-dev-server以及Sass加载器,实际配置:

{
    test: /\.scss/,
    loaders: [
        "style",
        { loader: "css", query: { modules: false, sourceMap: true } },
        { loader: "resolve-url" },
        { loader: "sass", query: { sourceMap: true } }
    ]
}

This works quite well and the image referenced in background: url() is processed by webpack and also replaced in the style for something like background-somehash.jpg , this file can be accessed by typing http://localhost:8080/background-somehash.jpg . 这非常有效, background: url()引用的图像background: url()由webpack处理,并且在样式中替换为background-somehash.jpg ,可以通过键入http://localhost:8080/background-somehash.jpg来访问此文件。 http://localhost:8080/background-somehash.jpg It also works when I provide whole url (including localhost) in a style background definition using developer tools... 当我使用开发人员工具在样式背景定义中提供整个URL(包括localhost)时,它也有效...

The only thing that don't work is the original css produced by webpack which looks like background: url(background-somehash.jpg) . 唯一不起作用的是webpack生成的原始css,它看起来像background: url(background-somehash.jpg) I also tried various urls like ./ , ../ , ../../ or ./images/ to try out if root was set somehow differently. 我也尝试过各种URL一样./../../.././images/尝试,如果根被设置在某种程度上有所不同。 What I don't get is that the file is readily available at the root... 我没有得到的是该文件在根目录下可用...

EDIT: When used together with extract-text-webpack-plugin which extracts styles into separate real styles.css file it works just fine. 编辑:当与extract-text-webpack-plugin一起使用时,它将样式提取到单独的真实styles.css文件中,它可以正常工作。 The question is why it doesn't work when final css is being served from javascript bundle ? 问题是为什么当从javascript包提供最终的css时它不起作用?

CLARIFICATION: Everything is referenced correctly, the image is available, it all works when I extract css into separate file using extract-text-webpack-plugin it just doesn't work when the completely same css is served from bundle.js which is then referenced in the index.html like <link href="blob:..." rel="stylesheet"> 澄清:一切都被正确引用,图像可用,当我使用extract-text-webpack-plugin将css提取到单独的文件中时,它只是在从bundle.js完全相同的css时bundle.jsindex.html引用,如<link href="blob:..." rel="stylesheet">

Things you should check: 你应该检查的事情:

Is the referenced image recognized by webpack? 引用的图像是否被webpack识别?

Just delete the image and check if the webpack build fails. 只需删除图像并检查webpack构建是否失败。 If that's the case, it is not an issue with your loader configuration. 如果是这种情况,那么您的加载程序配置不是问题。

Check the requested URL with your browser developer tools 使用浏览器开发人员工具检查请求的URL

If the request is terminated with a 404: 如果请求以404终止:

  • Check if output.publicPath (webpack) / contentBase (webpack-dev-server) point to the same location. 检查output.publicPath (webpack)/ contentBase (webpack-dev-server)是否指向同一位置。 This is from the browser's perspective (=no absolute file paths) 这是从浏览器的角度来看(=没有绝对文件路径)

  • If you're using a <base> -tag, you need to ensure that it does replace the base URL correctly. 如果您使用的是<base> -tag,则需要确保它确实正确替换了基本URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM