[英]what's the workflow of style-loader and css-loader
I'm new to webpack, still a little bit confused that how webpack cooperate with loaders.我是 webpack 的新手,对于 webpack 如何与装载机配合仍然有些困惑。 Let's we have below typescript file
index.ts
:让我们在下面有 typescript 文件
index.ts
:
//index.ts
import "bootstrap/dist/css/bootstrap.css";
...
// typescript code
and below is the webpack config file:以下是 webpack 配置文件:
module.exports = {
mode: "development",
entry: "./src/index.ts",
output: { filename: "bundle.js" },
resolve: { extensions: [".ts", ".js", ".css"] },
module: {
rules: [
{ test: /\.ts/, use: "ts-loader", exclude: /node_modules/ },
{ test: /\.css$/, use: ["style-loader", "css-loader"] }
]
}
};
Below is my personal thought on how webpack works with loaders, please correct me if I'm wrong:以下是我个人对 webpack 如何与装载机一起工作的想法,如果我错了,请纠正我:
Step 1-Webpack encounter index.ts
, so it passes this file to ts-loader
, and ts-loader
read the file and pass it to ts compiler, ts compiler generates js code file index.js
and pass back to ts-loader
, then ts-loader
passes index.js
back to webpack. Step 1-Webpack 遇到
index.ts
,于是把这个文件传给ts-loader
, ts-loader
读取文件传给 ts 编译器,ts 编译器生成 js 代码文件index.js
传回ts-loader
,然后ts-loader
将index.js
传回 webpack。
Step 2- Webpack reads index.js
and needs to resolve the css file, so Webpack passes the task to css-loader
, so css-loader
reads the css file as a long long string, then passes the task to style-loader
, which creates js code that can be embedded in tags in the index.html file. Step 2- Webpack reads
index.js
and needs to resolve the css file, so Webpack passes the task to css-loader
, so css-loader
reads the css file as a long long string, then passes the task to style-loader
, which创建可以嵌入到 index.html 文件中的标签中的 js 代码。
Step 3- bundle.js
is ready, and client sends a http request to get index.html
, and the bundle.js
is fetched and create a <style> tags to include all css styles. Step 3-
bundle.js
is ready, and client sends a http request to get index.html
, and the bundle.js
is fetched and create a <style> tags to include all css styles.
Is my above understanding correct?我的上述理解正确吗? If yes, below is my questions:
如果是,以下是我的问题:
Q1-after style-loader
generates js code, does it pass those js code back to css-loader
, then css-loader
passes received js code to webpack? Q1-
style-loader
生成js代码后,是否将这些js代码传回css-loader
,然后css-loader
将收到的js代码传给webpack? or style-loader
pass generated js code to webpack directly?还是
style-loader
直接将生成的 js 代码传递给 webpack?
Q2- in the webpack config file: Q2- 在 webpack 配置文件中:
...
{ test: /\.css$/, use: ["style-loader", "css-loader"] }
...
it seems that the style-loader
is used first, then css-loader
steps in( I have tried this approach, it worked, not sure why it worked)似乎首先使用了
style-loader
,然后是css-loader
(我尝试过这种方法,它有效,不知道为什么有效)
isn't that the css-loader
should start to work first then style-loader
as:是不是
css-loader
应该首先开始工作,然后style-loader
为:
...
{ test: /\.css$/, use: ["css-loader", "style-loader"] }
...
Is my above understanding correct?
我的上述理解正确吗?
Yes是的
Q1-after style-loader generates js code, does it pass those js code back to css-loader, then css-loader passes received js code to webpack?
Q1-style-loader生成js代码后,是否将这些js代码传回css-loader,然后css-loader将收到的js代码传给webpack? or style-loader pass generated js code to webpack directly?
还是 style-loader 直接将生成的 js 代码传递给 webpack?
Answer: style-loader pass generated js code to webpack directly答:style-loader 将生成的js代码直接传给webpack
Q2 it seems that the style-loader is used first, then css-loader steps in,
Q2 好像是先用style-loader,然后css-loader介入,
It can seem wrong.这似乎是错误的。 But its one of those things you need to read the docs for.
但它是您需要阅读文档的那些事情之一。 The last thing to process it is mentioned at the top of the array.
数组顶部提到了处理它的最后一件事。 Personally I don't think the other way around would be any more intuitive.
就我个人而言,我不认为反过来会更直观。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.