How does the style-loader and css-loader workflow function?

As a newcomer to webpack, I'm still grappling with how loaders interact with this tool. Consider the following TypeScript file index.ts:

//index.ts

import "bootstrap/dist/css/bootstrap.css";

...
// typescript code

Accompanying this is the webpack configuration file:

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"] }
        ]
    }
};

The process of webpack interacting with loaders, according to my understanding, goes as follows:

Step 1- Webpack encounters index.ts, passes it to ts-loader, which then reads and sends it to the ts compiler. The ts compiler generates a js code file index.js, which is passed back to ts-loader before being handed over to webpack.

Step 2- Upon reading index.js, webpack needs to resolve the css file. It delegates this task to css-loader, which interprets the css file as a string and hands off the job to style-loader. The latter creates js code that can be injected into <style> tags in the index.html file.

Step 3- With bundle.js prepared, the client requests index.html, fetching the associated bundle.js to introduce all css styles within <style> tags.

If my understanding is accurate, could you please clarify the following questions:

Q1- After style-loader generates the js code, does it transmit this code back to

css-loader</code, and then onto webpack? Or does <code>style-loader
directly pass the generated js code to webpack?

Q2- Regarding the order in the webpack configuration file:

...
{ test: /\.css$/, use: ["style-loader", "css-loader"] }
...

It appears that style-loader precedes css-loader (I tested this approach successfully, although unsure why). Shouldn't css-loader initiate its work before style-loader as shown below:

...
{ test: /\.css$/, use: ["css-loader", "style-loader"] }
...

Answer №1

Is my understanding correct based on the information provided above?

Yes, your understanding is correct.

In the process, does style-loader send the generated JavaScript code to css-loader and then css-loader passes it to webpack? Or does style-loader directly send the generated JavaScript code to webpack?

Answer: The style-loader sends the generated JavaScript code directly to webpack.

It appears that style-loader is used before css-loader in the sequence,

It may seem confusing at first glance, but it is important to refer to the documentation. The order of processing is indicated at the beginning of the array. Personally, I believe that switching the order would not necessarily be more intuitive.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Express, Node, and Angular to transmit audio files to the frontend

I am currently working on serving audio files from a Node/Express backend to an Angular frontend. On the server side, the code looks like this: var file = "/filepath.wav"; res.download(file, 'testAudio.wav'); And here's the client side c ...

Combining an external class with a namespace in TypeScript

Valid Script Code: class InputField { } namespace InputField { export enum Types { text = "text", number = "number" } } export { InputField }; Invalid Script Code: import InputField from "@Controls/InputFields/Inp ...

Are there any solutions available for implementing react-scrollable-picker in ReactJS using TypeScript?

I have implemented a react-scrollable-picker component from the website Unfortunately, I encountered an error message stating: "Could not find a declaration file for module 'react-scrollable-picker'. '/directory/node_modules/react-scrollab ...

Leveraging jQuery to detect an AJAX load that does not involve the use of jQuery's AJAX method

Hi all, I have a challenging issue that I'm struggling with in terms of jQuery/JavaScript. I am fetching data through standard AJAX without using any frameworks like jQuery. Now, the dilemma is that once the page has loaded, I need to implement a jQu ...

Tips for choosing a dynamically generated ajax element

Question for you: I have a snippet of HTML code that looks like this: <li class='entry'> <div class='entryContent'> <p class='entryText'>Entry text></p> <a href="#" c ...

Having trouble with transferring image blob to AWS S3 with node.js & angular

I'm having trouble uploading my images to AWS S3 { [InvalidParameterType: Expected params.Body to be a string, Buffer, Stream, Blob, or typed array object] message: 'Expected params.Body to be a string, Buffer, Stream, Blob, or typed arr ...

Enabling specific special characters for validation in Angular applications

How can we create a regex pattern that allows letters, numbers, and certain special characters (- and .) while disallowing others? #Code private _createModelForm(): FormGroup { return this.formBuilder.group({ propertyId: this.data.propertyId, ...

Tips for executing jsfiddle code within Joomla 3.2

I'm having trouble executing this code on my Joomla website. I have Joomla 3.2 installed with the JCK Editor, but the HTML tags are not functioning correctly. Can someone please assist me in running the following code: $("#text10").keyup(functio ...

What is the best way to cut and combine multiple array elements in the right positions?

let result = response.data; console.log(result); const newTemp = result.ssps_with_scated.splice(5,1).concat(result.ps_with_ed.splice(2,1))[0]; result.ps_with_ed.push(newTemp); After multiple attempts, I finally achieved my goal. However, the array values ...

Working with streams in Node.js by utilizing piping with both AJAX and the HTTP module

Running an express app with AJAX calls to remote endpoints has been a smooth process so far. However, I'm facing a unique challenge with one particular POST method that requires a query parameter named code and a body of type Array[int]. Surprisingly, ...

Exploring the orderBy feature in the react-firebase-hooks library with NextJS

Recently, I've been utilizing the React Firebase Hooks package from this GitHub repository. The code snippet below has been functioning smoothly for me. const [posts, loading, error] = useCollection( firebase .firestore() .collection(& ...

The type definition file for 'jest' cannot be located, despite the fact that jest has been successfully installed

SOLUTION STRATEGY: If you encounter a similar issue and are looking for a more comprehensive solution rather than quick fixes, consider recreating the repository. While it involves more effort initially, it can prevent future issues. In my case, the repos ...

"Incorporating JSON and Ajax to dynamically populate one select based on the selection in another select

I need to display data from a JSON file in select options, and here is the structure of my JSON file: [{ "vehicle": "car1", "type": [ {"name" : "BMW", "details" : [{ "color" : "red", "price" : "50000" }, ...

Regular expression to enclose non-quoted values (numbers and boolean) within quotes

I have a JSON file that is valid, but I need to add quotes around the number values and boolean values in order to send this JSON to a wix repeater that requires quotes around them. If anyone can assist me with this, I would greatly appreciate it. Exampl ...

Issue with jQuery fadeIn() and fadeOut() functions on IE versions 7 and 8

I have a project in Ruby on Rails that showcases illustrations. The top of the page features category links that fade out the current illustrations, replace them with new content, and then fade back in. Currently, I am utilizing jQuery version 1.6.2 for t ...

Colorbox: Display a specific section from a separate HTML page

Currently, I am facing a challenge where I need to open just one specific part of an external HTML page in a colorbox. I attempted the following approach, however it is opening the entire page instead of just the specified part (at the div with id="conten ...

validation for grouped radio inputs

I'm having a small issue with appending error states. I have 7 radio inputs and after submitting, I see 7 error states under the group. Can someone assist me in modifying the code? <form class="register-form"> <div class="co ...

Create copies of a single array containing objects in various new arrays

I need to create duplicates of an array of objects multiple times, as I require each one for different uses. if(!this.tempLookups){ for (let count = 0; count < this.dates.length; count++) { this.tempLookups[count] = this.lookups[key]; } ...

Transferring UTM parameters to a different page via a button click

Is there a way to extract parameters from a URL after the "?" and add them to a button's href in order to redirect to another landing page? I want to transfer UTM parameters to another page using JavaScript within the button. Original Homepage: Dest ...

Tips on streamlining two similar TypeScript interfaces with distinct key names

Presented here are two different formats for the same interface: a JSON format with keys separated by low dash, and a JavaScript camelCase format: JSON format: interface MyJsonInterface { key_one: string; key_two: number; } interface MyInterface { ...