How does css loader resolve resources in webpack

不想你离开。 提交于 2020-06-29 04:08:16

问题


I was learning about css-loader in webpack, the defination says that

The css-loader interprets @import and url() like import/require()and will resolve them. What does it mean, In an example in the documentaion there is

url(image.png) => require('./image.png')

So my question is will it convert url('./image.png') to require('image.png')

For example in a css file if i have background property as

#selector{
   background:url('./image.png'); //this is a vlid css property
}

will the above style converted like

#selector{
   background:require('./image.png'); // this is not a valid css property
}

if this is how the conversion take place than background:require('./image.png') is not valid css, is there something wrong in my understanding, may be i'm not getting what css-loader does actually. I went through the documentation of css-loader.

can anyone explain where i am wrong.


回答1:


please read about css-loader resolve urls https://webpack.js.org/loaders/css-loader/#url

if its just about understanding, webpack is fully js based. that means there have to be a way to check does the file exist, copy file or step in further actions. in the endfile it's simply css syntax with 'replaced/resolved' URL.

background:url('wp-content/themes/yours/assets/image.png');

check also https://www.npmjs.com/package/resolve-url-loader



来源:https://stackoverflow.com/questions/62285513/how-does-css-loader-resolve-resources-in-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!