<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: adetroja</title>
    <description>The latest articles on DEV Community by adetroja (@adetroja).</description>
    <link>https://dev.to/adetroja</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F815600%2F8ea456ce-62e7-4568-8cd0-2f8f862f5732.png</url>
      <title>DEV Community: adetroja</title>
      <link>https://dev.to/adetroja</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adetroja"/>
    <language>en</language>
    <item>
      <title>Different types of security token</title>
      <dc:creator>adetroja</dc:creator>
      <pubDate>Tue, 24 May 2022 11:50:18 +0000</pubDate>
      <link>https://dev.to/oneadvanced/different-types-of-security-token-4on</link>
      <guid>https://dev.to/oneadvanced/different-types-of-security-token-4on</guid>
      <description>&lt;h1&gt;
  
  
  Types of Security Token
&lt;/h1&gt;

&lt;p&gt;This article demonstrates different types of tokens in OpenID Connect. At the end of this article, you will have a clear understanding of the below points,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;About JSON Web Tokens (JWT).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;What is an Access Token?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example of an Access Token.&lt;/li&gt;
&lt;li&gt;Why do we need an Access Token?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;What is an ID Token?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example of an ID Token.&lt;/li&gt;
&lt;li&gt;Why do we need an ID Token?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;What is a Refresh Token?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example of a Refresh Token.&lt;/li&gt;
&lt;li&gt;Why do we need a Refresh Token?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  About JSON Web Token (JWT)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;JWT i.e. JSON Web Tokens is an important piece in ensuring trust and security in your application.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JWT allows claims such as user data to be represented securely.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A JWT is represented as a sequence of base64url encoded values that are separated by a dot character. Its ideal format is like “Header.Payload.Signature”, where the header keeps metadata for the token. The payload is the claims of the entity (typically a user) and a signature for the signed token.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Signed token is generated by combining the encoded JWT header and Payload and it is signed by using an encryption algorithm like HMAC SHA–256. The signature private key is always held by the server so it will be able to verify existing tokens as well as sign new tokens.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JWT could be used as an opaque identifier and could be inspected for additional information – such as identity attributes that it represents as claims.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Sample JWT token format could look like
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Ftypes-of-security-token%2Fjwt_parts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Ftypes-of-security-token%2Fjwt_parts.png" alt="Describe the JWT parts when you would like to decode."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Ftypes-of-security-token%2Fjwt_parts_01.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Ftypes-of-security-token%2Fjwt_parts_01.png" alt="Describe the JWT headers,payload and signature."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an Access Token?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Access tokens are credentials used to access protected resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Access tokens are used as bearer tokens. A bearer token means that the bearer (who holds the access token) can access authorized resources without further identification.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of this, bearer tokens must be protected. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;These tokens usually have a short lifespan for security purposes. When it expires, the user must authenticate again to get a new access token limiting the exposure of the fact that it is a bearer token.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Access token must never be used for authentication. Access tokens cannot tell if the user has authenticated. The only user information the access token processes is the user id, located in sub-claims.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The application receives an access token after a user successfully authenticates and authorizes access. It is usually in JWT format but does not have to be.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The application should treat access tokens as opaque strings since they are meant for APIs. Your application should not attempt to decode them or expect to receive a token in a particular format.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This token does not contain any information about the user itself besides their ID (“sub”). It only contains authorization information about which actions the application is allowed to perform at the API (“scope”). This is what makes it useful for securing an API, but not for authenticating a user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An access token is put in the Authorization header of your request, usually looks like Bearer “access_token” that the API you are calling can verify and grant you access.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example of an Access Token
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Here is the sample response from the token endpoint! The response includes the ID token and access token. Your application can use the access token to make API requests on behalf of the user.&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;{&lt;br&gt;&lt;br&gt;
  "token_type": "Bearer",&lt;br&gt;&lt;br&gt;
  "expires_in": 86400,&lt;br&gt;&lt;br&gt;
"access_token": "vCwWSQiaYhMHN2IbnEijtDWJ-BpiHbPohI6tOVrkrUrL2MqlF05K84MhBzvoC6iShEdUXl7t",&lt;br&gt;&lt;br&gt;
  "scope": "openid profile email photo",&lt;br&gt;&lt;br&gt;
  "id_token": "eyJraWQiOiJzMTZ0cVNtODhwREo4VGZCXzdrSEtQUkFQRjg1d1VEVGxteW85SUxUZTdzIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJmcmlnaHRlbmVkLWhlcnJpbmdAZXhhbXBsZS5jb20iLCJuYW1lIjoiRnJpZ2h0ZW5lZCBIZXJyaW5nIiwiZW1haWwiOiJmcmlnaHRlbmVkLWhlcnJpbmdAZXhhbXBsZS5jb20iLCJpc3MiOiJodHRwczovL3BrLWRlbW8ub2t0YS5jb20vb2F1dGgyL2RlZmF1bHQiLCJhdWQiOiJpVnUwQUMyOFFHLXNwc1psWk1janFRY2EiLCJpYXQiOjE2MDQyMTY1MzgsImV4cCI6MTYwNjgwODUzOCwiYW1yIjpbInB3ZCJdfQ.ZoPvZPaomdOnnz2GFRGbgaW7PPWIMFDqSBp0gbN4An4a9F-Bc-4_T9EBGV8aGetyjZYAON0gjNV0p0NGFiwettePWKuxBzusuGCEd9iXWWUO9-WTF5e2AGr3_jkg34dbxfiFXy3KgH7m0czm809cMaiZ_ofLYgJHVD8lqMQoWifhoNhpjPqa19Svc3nCHzSYHUgTXQWvA56NmQvyVPh_OM7GMpc6zHopmihJqt3eREof8N-bOd7FL39jeam2-k1TFSDogyJE513aC0OssRADr_TWvtL8xoaPkXM_7bXYs9_7erXmzF9la0hvmOuasieetpLhOvFeoiOJWCU9xhxj4Q"&lt;br&gt;&lt;br&gt;
}  &lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&amp;lt;figure align="center"&amp;gt;
  &amp;lt;img
  src="https://cdn.svc.oneadvanced.com/engineering-blogs/types-of-security-token/access_token.png"
  alt="Describe the access token" 
  width=800&amp;gt;
&amp;lt;/figure&amp;gt;

### Why do we need an Access Token?

&amp;gt; Access tokens are used to inform an API that the bearer of the token has been authorized to access the API and perform a predetermined set of actions specified by the scope. It is used to authorize API access.
## What is an ID Token?

- OpenID Connect always issues ID tokens along with access tokens to provide compatibility with OAuthand match the general tendency for authorizing identity.

- ID token carries personal information about end-users that authenticate on an OpenID Connect flow. In addition, this security token contains claims data about the user as saved with the authentication server.

The ID token represents JWT. For example, 

- If there is an app that uses google to login into users and syncs their calendars, google sends an Id token to the app that includes information about the user.

- This token authenticates the user to the application. The audience of this token is set to the application’s identifiers, which means a specific application should consume the token.

- Identity token payload contains “auth_time” (when the end-user authenticated), “iss” (who has issued the token), “aud” (Intended audience), “sub” (unique identifier of the user), “idp” etc.

### Example of an ID Token (JWT)

&amp;gt; Here is the sample response from the token endpoint! The response includes the ID token and access token.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;{&lt;br&gt;&lt;br&gt;
  "token_type": "Bearer",&lt;br&gt;&lt;br&gt;
  "expires_in": 86400,&lt;br&gt;&lt;br&gt;
  "access_token": "vCwWSQiaYhMHN2IbnEijtDWJ-BpiHbPohI6tOVrkrUrL2MqlF05K84MhBzvoC6iShEdUXl7t",&lt;br&gt;&lt;br&gt;
  "scope": "openid profile email photo",&lt;br&gt;&lt;br&gt;
"id_token": "eyJraWQiOiJzMTZ0cVNtODhwREo4VGZCXzdrSEtQUkFQRjg1d1VEVGxteW85SUxUZTdzIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJmcmlnaHRlbmVkLWhlcnJpbmdAZXhhbXBsZS5jb20iLCJuYW1lIjoiRnJpZ2h0ZW5lZCBIZXJyaW5nIiwiZW1haWwiOiJmcmlnaHRlbmVkLWhlcnJpbmdAZXhhbXBsZS5jb20iLCJpc3MiOiJodHRwczovL3BrLWRlbW8ub2t0YS5jb20vb2F1dGgyL2RlZmF1bHQiLCJhdWQiOiJpVnUwQUMyOFFHLXNwc1psWk1janFRY2EiLCJpYXQiOjE2MDQyMTY1MzgsImV4cCI6MTYwNjgwODUzOCwiYW1yIjpbInB3ZCJdfQ.ZoPvZPaomdOnnz2GFRGbgaW7PPWIMFDqSBp0gbN4An4a9F-Bc-4_T9EBGV8aGetyjZYAON0gjNV0p0NGFiwettePWKuxBzusuGCEd9iXWWUO9-WTF5e2AGr3_jkg34dbxfiFXy3KgH7m0czm809cMaiZ_ofLYgJHVD8lqMQoWifhoNhpjPqa19Svc3nCHzSYHUgTXQWvA56NmQvyVPh_OM7GMpc6zHopmihJqt3eREof8N-bOd7FL39jeam2-k1TFSDogyJE513aC0OssRADr_TWvtL8xoaPkXM_7bXYs9_7erXmzF9la0hvmOuasieetpLhOvFeoiOJWCU9xhxj4Q"&lt;br&gt;&lt;br&gt;
} &lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
### Why do we need an ID Token?

&amp;gt; The ID token is used to retrieve the user’s basic profile information like name, DOB, email, and phone, which is present in the authentication server. ID token should not be used to gain access to an API.

&amp;lt;figure align="center"&amp;gt;
  &amp;lt;img
  src="https://cdn.svc.oneadvanced.com/engineering-blogs/types-of-security-token/why_id_token.png"
  alt="Describe why Id token is required." 
  width=800&amp;gt;
&amp;lt;/figure&amp;gt;


## What is a Refresh token?

- This token is long-lived compared to the access token and is used to request a new access token in cases where it is expired.

- It can be considered as credentials used to obtain access tokens.

- It's allowed for long-lived access and is highly confidential.

- Refresh tokens can be used for grant types – authorization code and password credentials grant.

- Refresh tokens are intended for use only with authorization servers and are never sent to resource servers.

You will receive this in an encoded format only that cannot be decoded. An example could be 494c427ace9e04dea03c7234cea96c5ca53e0ce4ea95147e961fd9ebcf8feb84

### Example of a Refresh Token

&amp;gt; Here is the sample response from the token endpoint! The response includes the access token along with the refresh token.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;{&lt;br&gt;&lt;br&gt;
  "access_token":"ya29.a0AfH6SMARVjPq6G2y_P3hn3mbDdnRVrTGwO1ZkTXvUHye9wcpAPyiRKilq6Wh20TRbVx0nA1Nn8z1cpk_Jjs6qRwDvbOFNZhpA8e2GxDRcJ_PlrhlMnauvxktSDkjUyG-NWwuckHpiaOfr_uITriM0aS2t3HbGIKQJiU",&lt;br&gt;&lt;br&gt;
  "scope":"&lt;a href="https://mail.google.com/" rel="noopener noreferrer"&gt;https://mail.google.com/&lt;/a&gt;",&lt;br&gt;&lt;br&gt;
  "token_type":"Bearer",&lt;br&gt;&lt;br&gt;
  "expires_in":3599,&lt;br&gt;&lt;br&gt;
  "refresh_token":"1//0419Pth1mYFyBCgYIARAAGAQSNwF-L9IrcV8zK4wHDznJqUbeXrcEoE2O-Tmz7ryNpztTrLOiYOvs-0z4hxddGBpcKc0pEzLcWFI"&lt;br&gt;&lt;br&gt;
} &lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
### Why do we need Refresh token?

&amp;gt; As access token has defined lifetimes, there could be a possibility that the current access token becomes invalid or expires. This is the token used to request new access tokens without user interaction.
&amp;lt;figure align="center"&amp;gt;
  &amp;lt;img
  src="https://cdn.svc.oneadvanced.com/engineering-blogs/types-of-security-token/refresh_token.png"
  alt="Describe reshesh token" 
  width=800&amp;gt;
&amp;lt;/figure&amp;gt;

#### Requesting an access token using a refresh token
&amp;gt; While requesting a refresh token, the scope should be set as offline_access to the scope parameter.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Method: POST&lt;br&gt;&lt;br&gt;
Content-Type: application/x-&lt;a href="http://www.form-urlencoded" rel="noopener noreferrer"&gt;www.form-urlencoded&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Authorization: Bearer “access token”&lt;br&gt;&lt;br&gt;
&lt;a href="https://authorization-server.com/token" rel="noopener noreferrer"&gt;https://authorization-server.com/token&lt;/a&gt;?&lt;br&gt;&lt;br&gt;
grant_type=refresh_token&lt;br&gt;&lt;br&gt;
&amp;amp;refresh_token =&amp;lt;&amp;gt;&lt;br&gt;&lt;br&gt;
&amp;amp;scope=offline_access  &lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; Application receives an access token with redirect URI.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;{&lt;br&gt;&lt;br&gt;
  "token_type": "Bearer",&lt;br&gt;&lt;br&gt;
  "expires_in": 86400,&lt;br&gt;&lt;br&gt;
  "access_token": "QKY08tqDO8aeNebZbfgUFs1PH-cjerK2WBvE9FZpYGgZHnS_nLfhKYTECMBmPF_chz5GipOA",&lt;br&gt;&lt;br&gt;
  "scope": "photo offline_access",&lt;br&gt;&lt;br&gt;
  "refresh_token": "8V2dMpzRqB5cQDvoTb6X_Msl"&lt;br&gt;&lt;br&gt;
} &lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## Last Minute Summary

- Below is a quick reference of all the tokens at a glance,

- Access tokens are used in token-based authentication to gain access to resources by using them as bearer tokens.

- Refresh token is a long-lived special kind of token used to obtain a renewed access token.

- ID token carries identity information encoded in the token itself, which must be a JWT. It must not contain any authorization information, or any audience information — it is merely an identifier for the user.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>jwt</category>
      <category>accesstoken</category>
      <category>idtoken</category>
      <category>refreshtoken</category>
    </item>
    <item>
      <title>Webpack module bundler</title>
      <dc:creator>adetroja</dc:creator>
      <pubDate>Tue, 15 Mar 2022 11:42:32 +0000</pubDate>
      <link>https://dev.to/oneadvanced/webpack-module-bundler-52jh</link>
      <guid>https://dev.to/oneadvanced/webpack-module-bundler-52jh</guid>
      <description>&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;p&gt;In this blog post, we will show you, what developers are facing problems earlier with libraries and frameworks, how the webpack bundle is solving the problem, and use case of the webpack bundle for typescript base applications.&lt;/p&gt;

&lt;p&gt;Also, we will show you, core concepts with help of examples and functions of webpack bundle.&lt;/p&gt;

&lt;h1&gt;
  
  
  What was the Problem before webpack.
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_bundler_01.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_bundler_01.jpg" alt="A image to visualize, what happened when so many frameworks and libraries are involved in project."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are as JS developers, have a huge dev community, involved in the constant quest of improving the overall user and developer experience around using and building JavaScript/web applications, therefore we are creating new libraries and frameworks.&lt;/p&gt;

&lt;p&gt;A few design patterns also evolved to give developers a better, more powerful yet very simple way of writing complex JavaScript applications. &lt;/p&gt;

&lt;p&gt;Gradually, they started getting bulky, with the introduction of JavaScript modules, eventually, all of this led to a situation where we had 4x or 5x files in the overall application package.&lt;/p&gt;

&lt;p&gt;So, the overall size of the application is a challenge. Now, question is, how to manage the dependencies and size of the application.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Webpack Module Bundler?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_bundler_02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_bundler_02.png" alt="A image to visualize, what is webpack module bundler."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Webpack is a JavaScript library, built and maintained by &lt;strong&gt;Tobias Koppers and the team&lt;/strong&gt;. It is an aggressive and powerful module bundler for JavaScript applications.&lt;/p&gt;

&lt;p&gt;It packages all the modules (files like CSS, HTML, Typescript, .env, etc..) in your application into one or more bundles (often, just one file, main.js or index.js or index.html(browser)).&lt;/p&gt;

&lt;p&gt;Webpack is taking the help of &lt;strong&gt;loaders&lt;/strong&gt; and &lt;strong&gt;plugins&lt;/strong&gt;, then, it can &lt;strong&gt;transform&lt;/strong&gt;, &lt;strong&gt;minify&lt;/strong&gt; and &lt;strong&gt;optimize&lt;/strong&gt; all types of files before serving them as one bundle to the browser or server.&lt;/p&gt;

&lt;h1&gt;
  
  
  How webpack is worked internally ?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_bundler_03.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_bundler_03.png" alt="A image to visualize, how webpack is worked internally"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In brief, Webpack goes through your package and creates what it calls a &lt;strong&gt;dependency graph&lt;/strong&gt; which consists of various modules which your application/project would require to function as expected.&lt;/p&gt;

&lt;p&gt;Depending on this graph, it creates a new package which consists of the very bare minimum number of files required, often just a single bundle.js or index.js file which can be plugged into the HTML file (if react Application) or main.js (Server-side application) easily and used for the application.&lt;/p&gt;

&lt;h1&gt;
  
  
  Core concepts
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Entry&lt;/li&gt;
&lt;li&gt;Output&lt;/li&gt;
&lt;li&gt;Loaders&lt;/li&gt;
&lt;li&gt;Plugins&lt;/li&gt;
&lt;li&gt;Mode&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Entry
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;The entry object is where webpack looks to &lt;strong&gt;start building the bundle&lt;/strong&gt;. The context is an absolute string to the directory that contains the entry files.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;single entry file&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  entry:'./src/index.js'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;multiple entry file&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  entry: ['./src/file_1.js', './src/file_2.js'],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Output
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Configuring the output configuration options tells webpack &lt;strong&gt;how to write the compiled files to disk&lt;/strong&gt;. Note that, while there can be multiple entry points, only &lt;strong&gt;one output&lt;/strong&gt; configuration is specified.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, 'dist'),
    filename: 'main.js'
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some &lt;code&gt;libraryTarget&lt;/code&gt; options, which version of ECMAScript you want to output.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;libraryTarget: "umd", // enum&lt;/li&gt;
&lt;li&gt;libraryTarget: "umd-module", // ES2015 module wrapped in UMD&lt;/li&gt;
&lt;li&gt;libraryTarget: "commonjs-module", // ES2015 module wrapped in CommonJS&lt;/li&gt;
&lt;li&gt;libraryTarget: "commonjs2", // exported with module.exports&lt;/li&gt;
&lt;li&gt;libraryTarget: "commonjs", // exported as properties to exports&lt;/li&gt;
&lt;li&gt;libraryTarget: "amd", // defined with AMD defined method&lt;/li&gt;
&lt;li&gt;libraryTarget: "this", // property set on this&lt;/li&gt;
&lt;li&gt;libraryTarget: "var", // variable defined in root scope&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Loaders
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Loaders are &lt;strong&gt;transformations&lt;/strong&gt; that are applied to the source code of a module. They allow you to pre-process files as you import or “load” them.&lt;/p&gt;

&lt;p&gt;Loaders can transform files from a different language (like TypeScript) to JavaScript or load inline images as data URLs. Loaders even allow you to do things like import CSS files directly from your JavaScript modules!&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  /*...*/
  module: {
    rules: [
      {
        test: /\.css$/,
        use:
          [
            'style-loader',
            'css-loader',
          ]
      }
    ]
  }
  /*...*/
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Few examples of Loaders,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;sass-loader&lt;/strong&gt;: Loads a SASS/SCSS file and compiles it to CSS. It requires node- sass to work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;node-sass&lt;/strong&gt;: This library allows you to natively compile .scss files to CSS at incredible speed and automatically via a connect middleware.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;css-loader&lt;/strong&gt;: The css-loader interprets &lt;a class="mentioned-user" href="https://dev.to/import"&gt;@import&lt;/a&gt; and url() like import/require() and resolves them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;style-loader&lt;/strong&gt;: Add CSS to the DOM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;file-loader&lt;/strong&gt;: Instructs webpack to emit the required object as a file and to return its public URL.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;image-webpack-loader&lt;/strong&gt;: Minify PNG, JPEG, GIF, and SVG images with imagemin.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Plugins&lt;/strong&gt;
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;Webpack contains default behaviors to bundle most types of resources. When loaders are not enough, we can use plugins to &lt;strong&gt;modify&lt;/strong&gt; or &lt;strong&gt;add capabilities&lt;/strong&gt; to Webpack.&lt;/p&gt;

&lt;p&gt;Plugins are like loaders but on steroids. They can do things that loaders can’t do, and they are the main building block of webpack.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  /*...*/
  plugins: [
    new HTMLWebpackPlugin(),
    new CleanWebpackPlugin(['dist']),
  ]
  /*...*/
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;HTMLWebpackPlugin&lt;/strong&gt; plugin has the job of automatically creating an HTML file, adding the output JS bundle path, so the JavaScript is ready to be served.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CleanWebpackPlugin&lt;/strong&gt; can be used to clear the dist/ folder before creating any output, so you don’t leave files around when you change the name of the output file.&lt;/p&gt;

&lt;h1&gt;
  
  
  Mode
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;This mode (introduced in webpack 4) sets the &lt;strong&gt;environment&lt;/strong&gt; on which webpack works. It can be set to &lt;strong&gt;development&lt;/strong&gt; or &lt;strong&gt;production&lt;/strong&gt; (defaults to production, so you only set it when moving to development)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Production&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_production.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_production.png" alt="describe the production build file webpack bundle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Development&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_development.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_development.png" alt="describe the development build file webpack bundle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Installing webpack
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Global install
&lt;/h3&gt;

&lt;p&gt;with Yarn:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn global add webpack webpack-cli&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;with npm:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i -g webpack webpack-cli&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once this is done, you should be able to run &lt;code&gt;webpack-cli&lt;/code&gt; in cmd and add this to your &lt;code&gt;package.json&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  //...
  "scripts": {
    "build": "webpack --config webpack.prod.js"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then run &lt;code&gt;npm run build&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Console output&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_example.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_example.png" alt="describe the development build file webpack bundle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;JFrog deployed npm package: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_npm_package.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.svc.oneadvanced.com%2Fengineering-blogs%2Fwebpack-module-bundler%2Fwebpack_module_npm_package.png" alt="describe the development build file webpack bundle"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webpack</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
