Skip to content

Commit 5723f45

Browse files
committed
docs: clarify webpack loader execution order (right-to-left)
1 parent 0317ee7 commit 5723f45

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/content/guides/asset-management.mdx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,19 @@ npm install --save-dev style-loader css-loader
9696
};
9797
```
9898

99-
Module loaders can be chained. Each loader in the chain applies transformations to the processed resource. A chain is executed in reverse order. The first loader passes its result (resource with applied transformations) to the next one, and so forth. Finally, webpack expects JavaScript to be returned by the last loader in the chain.
99+
Loaders in webpack are executed from **right to left** (i.e., from last to first in the `use` array).
100100

101-
The above order of loaders should be maintained: [`'style-loader'`](/loaders/style-loader) should be listed before [`'css-loader'`](/loaders/css-loader) in the `use` array, but webpack applies them from right to left—so `'css-loader'` runs first, then `'style-loader'`. If this convention is not followed, webpack is likely to throw errors.
101+
For example:
102102

103-
T> webpack uses a regular expression to determine which files it should look for and serve to a specific loader. In this case, any file that ends with `.css` will be served to the `style-loader` and the `css-loader`.
103+
```js
104+
use: ["style-loader", "css-loader"];
105+
```
106+
107+
Here, `css-loader` runs first and processes the CSS, followed by `style-loader`, which injects the result into the DOM.
104108

105-
This enables you to `import './style.css'` into the file that depends on that styling. Now, when that module is run, a `<style>` tag with the stringified css will be inserted into the `<head>` of your html file.
109+
So even though `style-loader` appears before `css-loader` in the array, the execution happens in reverse order.
106110

107-
Let's try it out by adding a new `style.css` file to our project and import it in our `index.js`:
111+
If this order is not maintained, webpack may throw errors.
108112

109113
**project**
110114

0 commit comments

Comments
 (0)