简体   繁体   English

css loader如何解析webpack中的资源

[英]How does css loader resolve resources in webpack

I was learning about css-loader in webpack, the defination says that我正在学习 webpack 中的 css-loader,定义说

The css-loader interprets @import and url() like import/require() and will resolve them. css-loader 像import/require() ) 一样解释@importurl() ) 并解析它们。 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')所以我的问题是将url('./image.png')转换为require('image.png')

For example in a css file if i have background property as例如,在 css 文件中,如果我的背景属性为

#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.如果这是转换的方式,而不是background:require('./image.png') is not valid css,我的理解是否有问题,可能是我没有得到 css-loader 的实际作用。 I went through the documentation of css-loader.我浏览了 css-loader 的文档。

can anyone explain where i am wrong.谁能解释我错在哪里。

please read about css-loader resolve urls https://webpack.js.org/loaders/css-loader/#url请阅读有关 css-loader 解析网址https://webpack.js.org/loaders/css-loader/#url

if its just about understanding, webpack is fully js based.如果只是理解,webpack 完全基于 js。 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.在 endfile 中,它只是 css 语法,带有“替换/解析”URL。

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

check also https://www.npmjs.com/package/resolve-url-loader还要检查https://www.npmjs.com/package/resolve-url-loader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM