Skip to content

Commit 939b0d8

Browse files
authored
docs: clarify webpack loader execution order (right-to-left) (#8136)
1 parent fd3e6e5 commit 939b0d8

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/content/guides/asset-management.mdx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ contributors:
1313
- astonizer
1414
- snitin315
1515
- Brennvo
16+
- mr-baraiya
1617
---
1718

1819
If you've been following the guides from the start, you will now have a small project that shows "Hello webpack". Now let's try to incorporate some other assets, like images, to see how they can be handled.
@@ -95,15 +96,26 @@ npm install --save-dev style-loader css-loader
9596
};
9697
```
9798

98-
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+
Module loaders can be chained. Each loader in the chain applies transformations to the processed resource. A chain is executed in reverse order (right to left).
99100

100-
The above order of loaders should be maintained: [`'style-loader'`](/loaders/style-loader) comes first and followed by [`'css-loader'`](/loaders/css-loader). If this convention is not followed, webpack is likely to throw errors.
101+
For example, given the following rule:
101102

102-
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+
export default {
105+
module: {
106+
rules: [
107+
{
108+
test: /\.scss$/i,
109+
use: ["postcss-loader", "sass-loader"],
110+
},
111+
],
112+
},
113+
};
114+
```
103115

104-
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.
116+
Even though `postcss-loader` appears before `sass-loader` in the `use` array, webpack runs `sass-loader` first (compiling Sass into CSS), then runs `postcss-loader` on the result.
105117

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

108120
**project**
109121

0 commit comments

Comments
 (0)