簡體   English   中英

Webpack CSS在構建后更改圖像路徑

[英]Webpack css change image path after build

在開發中,我具有以下結構:

- dist - contain the css bundle
- sass - style.scss
- js
- img
- index.html

在生產中,我具有以下結構:

- img
- some-hash.js
- some-hase.css
- index.html

因此,在開發中,我的圖片網址應與img文件夾相對,如下所示:

background: url('../img/logo.jpg');

但是在生產中應該是這樣的:

background: url('img/logo.jpg');

我們如何使用webpack或一般方式解決此問題?

您需要在webpack配置中包含publicPath。

webpack-howto

 module.exports = { entry: './main.js', output: { path: './build', // This is where images AND js will go publicPath: 'http://mycdn.com/', // This is used to generate URLs to eg images or publicPath: '/' filename: 'bundle.js' }, module: { loaders: [ { test: /\\.less$/, loader: 'style-loader!css-loader!less-loader' }, // use ! to chain loaders { test: /\\.css$/, loader: 'style-loader!css-loader' }, { test: /\\.(png|jpg)$/, loader: 'url-loader?limit=8192' } // inline base64 URLs for <=8k images, direct URLs for the rest ] } }; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM