Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rules vs Loaders in Webpack - What's the Difference?

Tags:

webpack

In some Webpack examples, you see reference to a "rules" array:

module.exports = {   module: {     rules: [       {         test: /\.scss$/,         use: ExtractTextPlugin.extract({           fallback: 'style-loader',           //resolve-url-loader may be chained before sass-loader if necessary           use: ['css-loader', 'sass-loader']         })       }     ]   },   plugins: [     new ExtractTextPlugin('style.css')     //if you want to pass in options, you can do so:     //new ExtractTextPlugin({     //  filename: 'style.css'     //})   ] } 

(https://github.com/webpack-contrib/extract-text-webpack-plugin)

And in other, a loaders array:

var ExtractTextPlugin = require("extract-text-webpack-plugin"); module.exports = {     module: {         loaders: [             {                 test: /\.css$/,                 loader: ExtractTextPlugin.extract({                     loader: "css-loader"                 })             },             { test: /\.png$/, loader: "file-loader" }         ]     },     plugins: [         new ExtractTextPlugin({             filename: "style.css",             allChunks: true         })     ] }; 

(https://github.com/webpack/webpack/tree/master/examples/css-bundle)

What is the difference? Which should be used?

like image 816
Code Whisperer Avatar asked Mar 24 '17 14:03

Code Whisperer


People also ask

What are webpack rules?

rules allows you to specify several loaders within your webpack configuration. This is a concise way to display loaders, and helps to maintain clean code. It also offers you a full overview of each respective loader. Loaders are evaluated/executed from right to left (or from bottom to top).

What's the difference between webpack loaders and plugins?

Loaders work at the individual file level during or before the bundle is generated. Plugins: Plugins work at bundle or chunk level and usually work at the end of the bundle generation process.

Why does webpack need loaders?

Webpack enables use of loaders to preprocess files. This allows you to bundle any static resource way beyond JavaScript. You can easily write your own loaders using Node. js.

How do loaders work webpack?

The idea behind Webpack loaders is to provide multiple ways to handle some resource or asset - whether that's a javascript module, a CSS stylesheet, or an image. The loader is responsible for parsing/processing the resource, which might do any of the following: Transpile it into another language (e.g. babel-loader)


1 Answers

Loaders are used in Webpack 1
Rules are used in Webpack 2+

According to the migrating docs at the official Webpack site.

module.loaders is now module.rules

The old loader configuration was superseded by a more powerful rules system, which allows configuration of loaders and more. For compatibility reasons, the old module.loaders syntax is still valid and the old names are parsed. The new naming conventions are easier to understand and are a good reason to upgrade the configuration to using module.rules.

Example of Delta

  module: { -   loaders: [ +   rules: [       {         test: /\.css$/, -       loaders: [ -         "style-loader", -         "css-loader?modules=true" +       use: [ +         { +           loader: "style-loader" +         }, +         { +           loader: "css-loader", +           options: { +             modules: true +           } +         }         ]       },       {         test: /\.jsx$/,         loader: "babel-loader", // Do not use "use" here         options: {           // ...         }       }     ]   } 
like image 179
Rasmus Rajje Josefsson Avatar answered Oct 07 '22 02:10

Rasmus Rajje Josefsson