简体   繁体   English

Webpack CSS在构建后更改图像路径

[英]Webpack css change image path after build

In development, i have this structure: 在开发中,我具有以下结构:

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

In production, i have this structure: 在生产中,我具有以下结构:

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

So in development my image url should be relative to the img folder like this: 因此,在开发中,我的图片网址应与img文件夹相对,如下所示:

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

But in production it should be like this: 但是在生产中应该是这样的:

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

How we solve this problem with webpack or in general? 我们如何使用webpack或一般方式解决此问题?

You need have publicPath in webpack config. 您需要在webpack配置中包含publicPath。

webpack-howto 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