Webpack 和 Sass 正確處理 background: url() 圖像,但是在與 webpack-dev-server 一起使用時找不到它 (Webpack and Sass correctly processes background: url() image but then it's not found when used with webpack-dev-server)


問題描述

Webpack 和 Sass 正確處理 background: url() 圖像,但是在與 webpack‑dev‑server 一起使用時找不到它 (Webpack and Sass correctly processes background: url() image but then it's not found when used with webpack‑dev‑server)

我正在使用 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 } }
    ]
}

這工作得很好,並且在 background: url() 中引用了圖像 由 webpack 處理並替換為類似 background‑somehash.jpg 的樣式,可以通過鍵入 http://localhost:8080/background‑somehash.jpg 訪問此文件</代碼>。當我使用開發人員工具在樣式背景定義中提供整個 url(包括本地主機)時,它也可以工作......

唯一不起作用的是 webpack 生成的原始 css 看起來像 背景:網址(背景‑somehash.jpg)。我還嘗試了各種網址,例如 ./../../.././images/ 來測試 root 的設置是否有所不同。我沒有得到的是該文件在根目錄下很容易獲得......

編輯:extract‑text‑webpack‑plugin 將樣式提取到單獨的真實 styles.css 文件中,它工作得很好。問題是為什麼當從 javascript bundle 提供最終 css 時它不起作用?

澄清:一切都被正確引用,圖像可用,當我使用 extract‑text‑webpack‑plugin 當從 bundle.js 提供完全相同的 css 時,它只是不起作用


參考解法

方法 1:

Things you should check:

Is the referenced image recognized by webpack?

Just delete the image and check if the webpack build fails. If that's the case, it is not an issue with your loader configuration.

Check the requested URL with your browser developer tools

If the request is terminated with a 404:

  • Check if output.publicPath (webpack) / contentBase (webpack‑dev‑server) point to the same location. 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.

(by tomastrajanJohannes Ewald)

參考文件

  1. Webpack and Sass correctly processes background: url() image but then it's not found when used with webpack‑dev‑server (CC BY‑SA 2.5/3.0/4.0)

#Webpack #css #css-loader #sass-loader #SASS






相關問題

Babel 6 轉換運行時:$export 不是函數 (Babel 6 transform-runtime: $export is not a function)

Webpack 和 Sass 正確處理 background: url() 圖像,但是在與 webpack-dev-server 一起使用時找不到它 (Webpack and Sass correctly processes background: url() image but then it's not found when used with webpack-dev-server)

Babel 和 ES6 出現意外的“Uncaught TypeError: XXX is not a constructor”錯誤 (Unexpected "Uncaught TypeError: XXX is not a constructor" errors with Babel and ES6)

Webpack bundle ENOENT:沒有這樣的文件或目錄 fs.readdirSync (Webpack bundle ENOENT: no such file or directory fs.readdirSync)

有沒有辦法通過替換一些資產和組件從單個應用程序構建多個應用程序? (Is there a way to build multiple apps from single one with replacing some assets and components?)

Babel 7 不能正確轉換 index.js (Babel 7 don't convert index.js properly)

使用 Yarn 安裝的軟件包開始出現 sweetalert2 之類的錯誤 (packages installed with Yarn start to give error like sweetalert2)

找不到模塊 RecipeStyle.css (cannot find module RecipeStyle.css)

用於文件、子文件的 Webpack 加載器並將它們添加到跟踪列表 (Webpack loader for file, subfiles and add them to tracking list)

Webpack:沒有加載器來處理 SCSS 是輸入存在 (Webpack: no loader to handle the SCSS is input is present)

Webpack 在初始構建後不構建捆綁包 (Webpack not building bundle after initial build)

從其他機器訪問 webpack DevServer 子 URL 時出現 ERR_SSL_PROTOCOL_ERROR (ERR_SSL_PROTOCOL_ERROR when accessing webpack DevServer sub-URLs from a different machine)







留言討論