What Is Webpack In React

React - webpack

 
Webpack is a module bundler for modern Java script applications. It takes modules with dependencies and generates static assets representing those modules. 
 
When webpack processes the application it recursively builds a dependency graph which includes every module that application needs and then converts all those modules into a small number of bundles — generally only one of those bundles to be loaded in browser. 
 
It is highly configurable. Below are  a few of th major core concepts of webpack we cover.
  • Entry
  • Output
  • Loaders
  • Dev Server (webpack-dev-server)
  • Plugins
Entry
  • This is the starting point of graph which Webpack creates for all application dependencies. This entry point tells webpack where to start and follows the graph of dependencies to know what to bundle.
  • Entry point is define in “webpack.config.js” file using entry attribute of module.exports method .
Example
  1. module.exports =   
  2. {   
  3.    entry: "./src/index.js"   
  4. }   
Output 
  • Once all assets are bundled together, you need to tell webpack where to bundle your application.
  • The output property tells webpack how to treat bundled
Example in webpack.config.js
  1. module.exports = {  
  2.     entry: "./src/index.js"  
  3.     output: {  
  4.         path: "Idist/assets",  
  5.         filename: "bundle.js",  
  6.         publicPath: "assets"  
  7.     }  
  8. }   
Loaders
  • Intention to have all the assets in project to be webpack's concern and not the browsers.
  • Webpack treats every file (html, .css, .scss. jpg etc.) as a module, though webpack only understand java script.
  • Loaders have two purposes in webpack.config file
  • Identify what files should be transformed by a certain loader. ("test" property)
  • Transform that file so that it can be added to dependency graph (and eventually bundle). ("loader" property)
  • Loaders in webpack transform every file into modules as those are added to the dependency graph. 
Example
  1. module.exports = {  
  2.     module: {  
  3.         loaders: [{  
  4.             test: /\.(js|jsx)$/,  
  5.             exclude: /(node_modules)/,  
  6.             loader: 'babel-loader',  
  7.             query: {  
  8.                 presets: ('es2015', •react, 'stage-Ol']  
  9.             }  
  10.         }, ]  
  11.     }  
  12. }   
Dev server – webpack-dev-server
  • It can be used to quickly develop an application.
  • It provides you with a server and live reloading.
  • By default, it will save the files in the current directory, unless developer manually configure a specific content base. Inline mode (a small webpack-dev-server client entry is added to the bundle which refreshes the page on change) 
Example
  1. module.exports = {  
  2.     devServer: {  
  3.         inline: true,  
  4.         contentBase: './dist',  
  5.         port: 8080  
  6.     },  
  7. };  
Plugins
  • Loaders only execute transforms on a per-file basis, plugins are most used (but not limited to) performing actions and custom functionality on "compilations" or "chunks" of your bundled modules.
  • Webpack has a rich plugin interface. Most of the features within webpack itself use this plugin interface. This makes webpack flexible.
Example
  1. query: {  
  2.     presets: Ces2015 ', '  
  3.     react ', •stage-m,  
  4.     plugins: ['react-html-attrs'],  
  5. }