繁体   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