Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack 2 loaders declared in reverse?

Tags:

webpack

When using webpack 2, why is it that loaders need to be added for the "use:" key in reverse order? Why not list each loader from first to last, left to right? Is there a reason?

like image 973
Ruegen Avatar asked Apr 18 '17 12:04

Ruegen


People also ask

How do Webpack loaders work?

A loader in webpack is a function that transforms a source code of imported modules. Loaders are the heart and soul of how webpack compiles TypeScript to JavaScript, turns SASS into CSS, and JSX into React. createElement calls. In fact, webpack doesn't really do all that, loaders do!

How do Webpacks handle pictures?

Essentially, there is not much in Webpack to include your desired images for your web application. First, put your image files into one folder of your projects application. For instance, your src/ folder may have a folder assets/ which has a folder images/.

What are Webpack config rules?

A Webpack config is a JavaScript object that configures one of Webpack's options. Most projects define their Webpack config in a top-level webpack. config. js file, although you can also pass the config as a parameter to Webpack's Node.


1 Answers

It seems like a convention that could just as easily be the other way to match the convention of execution order matching source order, but the reason it uses this "reverse" order is because it's not using a sequential model, but a nested one. Like an onion, the top/left items wrap the lower/right items.

The reason is that they are essentially function calls, so this:

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

doesn't mean "Do the CSS Loader, then the Style Loader" and is just declared backwards. Instead, it's essentially equivalent to style-loader(css-loader()), so as you go down the list, each subsequent loader is a nested function.

Hope that helps.

like image 142
redOctober13 Avatar answered Nov 15 '22 14:11

redOctober13