简体   繁体   English

style-loader 和 css-loader 的工作流程是什么

[英]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-loaderts-loader读取文件传给 ts 编译器,ts 编译器生成 js 代码文件index.js传回ts-loader ,然后ts-loaderindex.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.

相关问题 Webpack样式加载器和CSS加载器 - Webpack style-loader and css-loader Webpack:为什么&#39;style-loader!css-loader!postcss-loader&#39;不处理导入的文件? - Webpack: Why 'style-loader!css-loader!postcss-loader' doesn't process imported files? Webpack style-loader / css-loader: url() path resolution not working - Webpack style-loader / css-loader: url() path resolution not working 尽管已安装模块,但无法解析模块style-loader!css-loader - Cannot resolve module style-loader!css-loader despite already being installed 为什么 webpack4 生产包总是包含 style-loader、css-loader 和 vue-loader 内容? - why webpack4 production bundle will always include style-loader, css-loader and vue-loader content? 从webpack的ExtractTextPlugin和style-loader中缩小css - Minify css from webpack's ExtractTextPlugin and style-loader webpack中css-loader的目的是什么? - What is the purpose of css-loader in webpack 使用style-loader访问webpack插件中生成的css - Access generated css in webpack plugin using style-loader 为什么样式加载器被用作Webpack的ExtractSass插件的后备? - Why is style-loader used as a fallback with Webpack's ExtractSass plugin? 使用 css-loader 时,导入的样式对象为空 - When using css-loader, imported style object is empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM