<?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: Hridayesh Sharma</title>
    <description>The latest articles on DEV Community by Hridayesh Sharma (@vyasriday).</description>
    <link>https://dev.to/vyasriday</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%2F101665%2F1895c9ce-7138-459f-8acb-3b164746c1a6.jpg</url>
      <title>DEV Community: Hridayesh Sharma</title>
      <link>https://dev.to/vyasriday</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vyasriday"/>
    <language>en</language>
    <item>
      <title>LoginRadius Open Source For Hacktoberfest 2020</title>
      <dc:creator>Hridayesh Sharma</dc:creator>
      <pubDate>Fri, 25 Sep 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/loginradius/loginradius-open-source-for-hacktoberfest-2020-2iik</link>
      <guid>https://dev.to/loginradius/loginradius-open-source-for-hacktoberfest-2020-2iik</guid>
      <description>&lt;p&gt;Checkout LoginRadius Open Source Repositories where you can contribute for hackotberfest 2020&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.loginradius.com/engineering/blog/loginradius-opensource-repos-for-hacktoberfest-2020/"&gt;Read On&lt;/a&gt;&lt;/p&gt;

</description>
      <category>hacktoberfest</category>
      <category>opensource</category>
      <category>loginradius</category>
    </item>
    <item>
      <title>Working with Golang Maps</title>
      <dc:creator>Hridayesh Sharma</dc:creator>
      <pubDate>Fri, 25 Sep 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/loginradius/working-with-golang-maps-148h</link>
      <guid>https://dev.to/loginradius/working-with-golang-maps-148h</guid>
      <description>&lt;p&gt;Explore and learn about the usage of maps in Go.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.loginradius.com/engineering/blog/working-with-go-maps/"&gt;Read On&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>dictionaries</category>
      <category>maps</category>
    </item>
    <item>
      <title>Past is gone. Look forward to future.</title>
      <dc:creator>Hridayesh Sharma</dc:creator>
      <pubDate>Sat, 09 May 2020 10:46:24 +0000</pubDate>
      <link>https://dev.to/vyasriday/past-is-gone-look-forward-to-future-2d5f</link>
      <guid>https://dev.to/vyasriday/past-is-gone-look-forward-to-future-2d5f</guid>
      <description>&lt;p&gt;This is not a technical blog or something like that. This is to motivate all those people who have had a bad past, or those who can't get over their past. &lt;/p&gt;

&lt;p&gt;Today I wrote a simple Javascript code to render some radio buttons. Then  I realised may be I can create something good out of it. &lt;/p&gt;

&lt;h4&gt;
  
  
  So I created this &lt;a href="https://vyasriday.github.io/pages/life_in_years.html"&gt;Days in 2020&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Hope this motivates you to make the best use of time you have. &lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

&lt;p&gt;Thanks for reading. 😊&lt;/p&gt;

</description>
      <category>motivation</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>11 NPM Commands Every Node Developer Should Know.</title>
      <dc:creator>Hridayesh Sharma</dc:creator>
      <pubDate>Tue, 28 Apr 2020 15:08:23 +0000</pubDate>
      <link>https://dev.to/vyasriday/npm-commands-that-a-node-developer-should-know-2h84</link>
      <guid>https://dev.to/vyasriday/npm-commands-that-a-node-developer-should-know-2h84</guid>
      <description>&lt;h4&gt;
  
  
  1. Create a package.json file
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt; &lt;span class="c"&gt;# -y to initialize with default values. &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Install a package locally or globally
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i package &lt;span class="c"&gt;# to install locally. i is short for install&lt;/span&gt;
npm i &lt;span class="nt"&gt;-g&lt;/span&gt; package &lt;span class="c"&gt;# to install globally&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Install a specific version of a package
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;package@version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. Install a package as dev dependency
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i &lt;span class="nt"&gt;-d&lt;/span&gt; package &lt;span class="c"&gt;# -d stands for --save-dev i.e install as dev dependency&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  5.To see all the dependencies of your project
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm list &lt;span class="c"&gt;# this will list all the dependencies of the third-party modules as well&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  6. To see direct dependencies of your project
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm list &lt;span class="nt"&gt;--depth&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  7. To see the information about a package in your project
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm view package
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  8. See specific information from package.json of a third-party package like dependencies of the package
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm view package dependencies
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  9. To check different versions available for a package
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm view package versions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  10. Check outdated packages in your project
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm outdated &lt;span class="c"&gt;# run with -g flag to check globally&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  11. To update the packages
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this helps. Let me know if you use any other commands that can be helpful.&lt;/p&gt;

&lt;p&gt;Thanks for reading. 😀 &lt;/p&gt;

</description>
      <category>npm</category>
      <category>node</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Using dotenv to manage environment variables in Nodejs</title>
      <dc:creator>Hridayesh Sharma</dc:creator>
      <pubDate>Mon, 27 Apr 2020 14:23:46 +0000</pubDate>
      <link>https://dev.to/vyasriday/using-dotenv-to-manage-environment-variables-in-nodejs-3ja1</link>
      <guid>https://dev.to/vyasriday/using-dotenv-to-manage-environment-variables-in-nodejs-3ja1</guid>
      <description>&lt;h4&gt;
  
  
  Have you ever faced any of these scenarios:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;You have some dynamic values in your code which you don't want to hardcode. &lt;/li&gt;
&lt;li&gt;You have API Keys in your opensource project which you obviously don't want to push to GitHub&lt;/li&gt;
&lt;li&gt;You have some values in your code that depend on what environment you are building your code for.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;If you fall into any of the above-mentioned scenarios I have a got a solution for you.&lt;/em&gt; 😃&lt;/p&gt;

&lt;h5&gt;
  
  
  Node Environment Variables Using dotenv
&lt;/h5&gt;

&lt;p&gt;In Node, there is something called environment variables which you can see by logging &lt;code&gt;process.env&lt;/code&gt;. &lt;br&gt;
For example, you can set a &lt;code&gt;NODE_ENV&lt;/code&gt; variable which is basically used to define what environment you want to build your code for.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NODE_ENV&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then further use this variable to generate different builds.&lt;/p&gt;

&lt;p&gt;Instead of setting these environment variables ourselves, we will be using the &lt;code&gt;dotenv&lt;/code&gt; package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm i &lt;span class="nt"&gt;--save&lt;/span&gt; dotenv 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once dotenv is installed let's create a &lt;code&gt;.env&lt;/code&gt; file in the root of our project &lt;br&gt;
and add the &lt;code&gt;PORT&lt;/code&gt; variable into it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.env&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PORT=3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's see how we can use this &lt;code&gt;.env&lt;/code&gt; file in our code. We will create a basic node server and use &lt;code&gt;PORT&lt;/code&gt; defined in our &lt;code&gt;.env&lt;/code&gt;.&lt;br&gt;
NOTE: If you do not understand the server part, don't worry. It's not important to understand how to use &lt;code&gt;dotenv&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dotenv&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Let's see what we just did here.
&lt;/h3&gt;

&lt;p&gt;We basically created a server in node and the server is listening on the port defined in our node environment variables. If it's not defined there we give it a fallback value.&lt;br&gt;
Now the value of this &lt;code&gt;PORT&lt;/code&gt; can be different depending upon where you deploy your server be it Heroku or something else.&lt;/p&gt;

&lt;p&gt;dotenv is loaded first and it has a config function that basically reads our &lt;code&gt;.env&lt;/code&gt; file and populates the environment variables.&lt;/p&gt;

&lt;p&gt;You can use dotenv to store your server configuration or use it with webpack to define global variables using webpack's definePlugin.&lt;/p&gt;

&lt;p&gt;PS: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You need to load your env config once in your webpack config or in your entry file and also never commit your &lt;code&gt;.env&lt;/code&gt; files&lt;/li&gt;
&lt;li&gt;In order to maintain multiple &lt;code&gt;env&lt;/code&gt; files each for different environment you can use &lt;code&gt;dotenv-flow&lt;/code&gt; package. You must set your &lt;code&gt;process.env.NODE_ENV&lt;/code&gt; variable first as &lt;code&gt;dotenv-flow&lt;/code&gt; uses that variable to decide which env file to pick. &lt;a href="https://www.npmjs.com/package/dotenv-flow"&gt;https://www.npmjs.com/package/dotenv-flow&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you face any problem in implementing this, let me know in the comments. I will be happy to resolve.&lt;/p&gt;

&lt;p&gt;Thanks for reading 😀&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Using DefinePlugin to Define API URLs in Webpack</title>
      <dc:creator>Hridayesh Sharma</dc:creator>
      <pubDate>Sat, 18 Apr 2020 17:22:24 +0000</pubDate>
      <link>https://dev.to/vyasriday/using-defineplugin-to-define-api-urls-in-webpack-5f1e</link>
      <guid>https://dev.to/vyasriday/using-defineplugin-to-define-api-urls-in-webpack-5f1e</guid>
      <description>&lt;p&gt;In today's development cycle, there are multiple environments for any API. For example, the dev API is available at &lt;code&gt;dev.domain.com&lt;/code&gt; and production API is available at &lt;code&gt;prod.domain.com&lt;/code&gt;. &lt;br&gt;
While writing a front end library or a front end modern JavaScript application that consumes the API we use webpack to build our applications. For each environment (dev or prod) we write a different &lt;code&gt;npm script&lt;/code&gt; to build application for that environment.&lt;br&gt;
&lt;code&gt;package.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;...&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;any&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;other&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;configuration&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"webpack --env=dev --mode=development"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"prod"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"webpack --env=prod --mode=development"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;...rest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;of&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;file&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's say our code consumes an API &lt;code&gt;${env}.domain.com&lt;/code&gt; where env can be either dev or prod or any other environment that our API is available at.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;JavaScript Code&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="c1"&gt;// this code gets some data from the api&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`http://dev.domain.com`&lt;/span&gt;
&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  So how do we make the fetch to make a request to &lt;code&gt;prod.domain.com&lt;/code&gt; instead of &lt;code&gt;dev.domain.com&lt;/code&gt; when we build our application for production??
&lt;/h3&gt;

&lt;p&gt;We will use a webpack plugin called &lt;code&gt;DefinePlugin&lt;/code&gt; to achieve this.&lt;br&gt;
So let's write our webpack configuration and then we will modify the JavaScript Code above to work in different environments.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;webpack.config.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;webpack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;webpack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="cm"&gt;/* we are creating our webpack configure as a function that takes env 
as parameter which is passed from npm scripts and can be dev or prod 
as defined in npm scripts
*/&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;envConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dev&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
      &lt;span class="nx"&gt;envConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;domain&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://dev.domain.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;prod&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
      &lt;span class="nx"&gt;envConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;domain&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://prod.domain.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bundle.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;DefinePlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;API_DOMAIN&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;envConfig&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;domain&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
   &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Let's see what we just did in our webpack configuration
&lt;/h5&gt;

&lt;p&gt;We just created our webpack configuration and inside it an object &lt;code&gt;envConfig&lt;/code&gt; that contains a domain property on it. Depending upon what's the value of &lt;code&gt;env&lt;/code&gt;, the value of domain will vary. &lt;br&gt;
Then we used DefinePlugin to define a &lt;code&gt;API_DOMAIN&lt;/code&gt; constant. Now in our code this &lt;code&gt;API_DOMAIN&lt;/code&gt; can be used and we do not have to worry about the &lt;code&gt;url&lt;/code&gt; that we used in our JavaScript code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Modified JavaScript Code&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;API_DOMAIN&lt;/span&gt;
  &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PS: I have added &lt;code&gt;switch&lt;/code&gt; statement in the webpack file itself for clarity. You can add the logic in a separate file and then import it in the webpack configuration.&lt;/p&gt;

&lt;p&gt;That's how we can use webpack to handle the API domains for us based on the environment we are building our code for. Similarly we can have any other constants configured as well.&lt;/p&gt;

&lt;p&gt;The GitHub repository for the code is available at &lt;a href="https://github.com/vyasriday/webpack-manage-environment-domains"&gt;Webpack Manage Environment Domains&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webpack</category>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>What is "this" in Javascript ?</title>
      <dc:creator>Hridayesh Sharma</dc:creator>
      <pubDate>Sun, 08 Sep 2019 09:30:19 +0000</pubDate>
      <link>https://dev.to/vyasriday/what-the-heck-is-this-in-javascript-37n1</link>
      <guid>https://dev.to/vyasriday/what-the-heck-is-this-in-javascript-37n1</guid>
      <description>&lt;p&gt;While learning JavaScript there are many roadblocks like &lt;code&gt;closures&lt;/code&gt;, &lt;code&gt;asynchronous programming&lt;/code&gt;, &lt;code&gt;this&lt;/code&gt; keywords, etc. These are the parts of JavaScript that are difficult to understand for a newbie in JavaScript, but once you learn them you can leverage everything JavaScript provides. So today I will be talking about one such superpower and that is &lt;code&gt;this&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;So without further delay let's get to it. Before we go into &lt;code&gt;this&lt;/code&gt; let's talk about an analogy to understand &lt;strong&gt;scope&lt;/strong&gt; in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  An Analogy to Understand Scope in JavaScript
&lt;/h3&gt;

&lt;p&gt;There is a 10 story building and there is a policeman who is looking for a criminal in this building. Now let's see the two ways of how he finds the criminal or what if he does not find the criminal at all. 😨😨&lt;/p&gt;

&lt;p&gt;(i) The policeman enters the building and starts looking for the criminal on the ground floor. If he does not find the criminal on the ground floor he goes to the first floor and if he finds the criminal on the first floor he sends him back to police station otherwise he keeps going up one floor at a time until the criminal is caught. And if he does not find the criminal then he reports back to the police station that criminal could not be found in the building.&lt;/p&gt;

&lt;p&gt;(ii) &lt;strong&gt;This one is interesting&lt;/strong&gt; 😋 There is a way by which the policeman can be directly shot onto any one of the floors and whoever he finds on that floor is the criminal now and there is no way to change that. &lt;em&gt;Such a dynamic way of finding the criminal, isn't it?&lt;/em&gt; 😂&lt;/p&gt;

&lt;h4&gt;
  
  
  What do you think which approach can have bad consequences??
&lt;/h4&gt;

&lt;p&gt;Obviously the second approach, as in the first approach what if the real criminal is on another floor and the police just caught a civilian.😫 &lt;/p&gt;

&lt;p&gt;So with a dynamic mindset now let's try to understand the two different scopes in JavaScript: &lt;code&gt;static or lexical scope&lt;/code&gt; and &lt;code&gt;dynamic scope&lt;/code&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Lexical Scope
&lt;/h3&gt;

&lt;p&gt;In our first approach, the scope of finding the criminal is &lt;code&gt;lexical&lt;/code&gt;. The policeman first looks on the ground floor and if he does not find the criminal then he goes one floor up and so on. That's exactly how lexical scoping works in JavaScript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;outer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;lexical&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;inner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;lexical&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 21 12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above snippet &lt;code&gt;lexical&lt;/code&gt; function first looks for &lt;code&gt;inner variable&lt;/code&gt; and it finds it on its own floor. But for &lt;code&gt;outer&lt;/code&gt; it finds it own one floor up. So that's how the lexical scope works in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dynamic Scope
&lt;/h3&gt;

&lt;p&gt;Let's see in code how dynamic scope works.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/vytros_7/embed/zYORZMK?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;So here we see instead of alerting 'Gotcha Joe' we got 'Gotcha undefined' when we click on the box. Why is that ?? Why instead of catching the &lt;code&gt;Joe&lt;/code&gt;, our policeman (eventHandler function) got &lt;code&gt;undefined&lt;/code&gt;. This is because our eventHandler function uses &lt;code&gt;this&lt;/code&gt; and depending upon where our handler function is called or invoked &lt;code&gt;this&lt;/code&gt; can take different values. There are ways to fix it, I will get to them in a while, I promise.&lt;/p&gt;

&lt;p&gt;So far we have understood the scoping in JavaScript and seen how using &lt;code&gt;this&lt;/code&gt; can result in unexpected outcomes.&lt;/p&gt;

&lt;p&gt;Now let's talk about &lt;code&gt;this', and how to figure out the values of&lt;/code&gt;this` in our code.&lt;/p&gt;

&lt;h4&gt;
  
  
  What &lt;code&gt;this&lt;/code&gt; actually is?
&lt;/h4&gt;

&lt;p&gt;In JavaScript &lt;code&gt;this&lt;/code&gt; is an &lt;code&gt;object&lt;/code&gt; inside a function or in a scope which can be any value depending upon how the function is defined or how the function is called.&lt;/p&gt;

&lt;p&gt;Let's understand this in different contexts -&lt;/p&gt;

&lt;h3&gt;
  
  
  case 1 - default this binding
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;javascript&lt;br&gt;
function printThis() {&lt;br&gt;
  console.log(this);&lt;br&gt;
}&lt;br&gt;
printThis(); // in browser it can be window object or in node global object&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the above snippet, the default value of &lt;code&gt;this&lt;/code&gt; is what is available inside the function when we try to access it. Now this default value depends on how and where we are calling the function. Here we are calling our function in the global context.&lt;/p&gt;

&lt;h3&gt;
  
  
  case 2: implicit binding
&lt;/h3&gt;

&lt;pre&gt;
let obj = {
  name: 'Joe',
  handler: function() {
    console.log(this.name);
  }
}

obj.handler(); // Joe
&lt;/pre&gt;

&lt;p&gt;Here we are implicitly providing the context for &lt;code&gt;this&lt;/code&gt; to the function. The value on the left of &lt;code&gt;.&lt;/code&gt; is the value of &lt;code&gt;this&lt;/code&gt; for our handler function. In this case, it is the object &lt;code&gt;obj&lt;/code&gt; itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  case 3: binding &lt;code&gt;this&lt;/code&gt; with &lt;code&gt;apply&lt;/code&gt;
&lt;/h3&gt;

&lt;pre&gt;
function printThis() {
  console.log(this.name);
}

printThis.call({name: 'Joe'}); // Joe
&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;call&lt;/code&gt; method takes the first argument as the value of &lt;code&gt;this&lt;/code&gt; that you want to pass to the function and invokes the function. Here we have passed an object with a property name on it. This object will be used as &lt;code&gt;this&lt;/code&gt; inside our &lt;code&gt;printThis&lt;/code&gt; function.&lt;/p&gt;

&lt;h3&gt;
  
  
  case 4: the &lt;code&gt;new&lt;/code&gt; keyword
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; keyword in JavaScript is used to create a new instance of a class using class syntax or constructor functions. You can read more here in my last post &lt;br&gt;
&lt;a href="https://dev.to/_hridaysharma/understanding-classes-es5-and-prototypal-inheritance-in-javascript-n8d"&gt;Understanding Prototypal Inheritance&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;So these were different ways of how this works in the JavaScript world. In our click handler example, I promised that I will be telling you different ways of fixing that code so that instead of catching &lt;code&gt;undefined&lt;/code&gt; we will be able to catch 'Joe'. &lt;/p&gt;

&lt;p&gt;Here is the modified code for that on &lt;a href="https://codepen.io/vytros_7/pen/wvwydEK"&gt;Codepen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we have introduced two new ways of handling &lt;code&gt;this&lt;/code&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fat arrow functions&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;bind&lt;/code&gt; method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's understand them one by one&lt;/p&gt;

&lt;h3&gt;
  
  
  fat arrow functions
&lt;/h3&gt;

&lt;p&gt;ES6 introduced a new syntax for writing functions. Though it looks like just syntactic sugar, there is more to it than just new syntax. Fat arrow functions handle &lt;code&gt;this&lt;/code&gt; very differently than normal functions. In fat arrow functions &lt;code&gt;this&lt;/code&gt; is scoped lexically just like any other variable. We have created fat arrow function version of our previously defined normal functions and now inside our &lt;code&gt;eventHandlerArrowFunction&lt;/code&gt; the value of &lt;code&gt;this&lt;/code&gt; is the instance of the class itself no matter where the function is called. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;bind&lt;/code&gt; method
&lt;/h3&gt;

&lt;p&gt;Just like the &lt;code&gt;call&lt;/code&gt; method ES5 also introduced the &lt;code&gt;bind&lt;/code&gt; method. The difference between the two is that &lt;code&gt;bind&lt;/code&gt; takes the value of &lt;code&gt;this&lt;/code&gt; as the first argument and returns a new function with &lt;code&gt;this&lt;/code&gt; bound to it. Where &lt;code&gt;call&lt;/code&gt; invokes the function with the provided value of &lt;code&gt;this&lt;/code&gt; along with other arguments.&lt;br&gt;
In our click handler function, we want to pass the reference of the function to &lt;code&gt;addEventListener&lt;/code&gt; instead of invoking the function. Therefore we used the &lt;code&gt;bind&lt;/code&gt; method there.&lt;/p&gt;

&lt;p&gt;So with this, I hope you understood how the mighty &lt;code&gt;this&lt;/code&gt; works in JavaScript.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>thiskeyword</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding Classes (ES5) and Prototypal Inheritance in JavaScript</title>
      <dc:creator>Hridayesh Sharma</dc:creator>
      <pubDate>Sun, 18 Aug 2019 05:53:18 +0000</pubDate>
      <link>https://dev.to/_hridaysharma/understanding-classes-es5-and-prototypal-inheritance-in-javascript-n8d</link>
      <guid>https://dev.to/_hridaysharma/understanding-classes-es5-and-prototypal-inheritance-in-javascript-n8d</guid>
      <description>&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Joe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// {name: 'Joe', age: 20}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;In a nutshell the above snippet creates a Person class that can have multiple instances. By convention functional classes in javascript start with a capital letter.&lt;/em&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Let's dig deeper into what's going on here?? How a normal function can be used for classess?? 🤔🤔
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;Person&lt;/code&gt; function is just like any other function which when called directly just returns &lt;code&gt;undefined&lt;/code&gt; as we are not explicitly returning anything from it. But the real magic lies in the &lt;code&gt;new&lt;/code&gt; keyword on line &lt;code&gt;var me = new Person('Joe', 20)&lt;/code&gt;.&lt;br&gt;
&lt;strong&gt;Lets understand that magic - There are a couple of steps that happen when we use the &lt;code&gt;new&lt;/code&gt; keyword to initialize a function :&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An empty object &lt;code&gt;{}&lt;/code&gt; is created.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Person&lt;/code&gt; is called by passing the reference of the object to it: &lt;code&gt;Person.call({}, 'Joe', 20)&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Inside &lt;code&gt;Person&lt;/code&gt; &lt;code&gt;this&lt;/code&gt; now refers to the object passed in the above step.&lt;/li&gt;
&lt;li&gt;The object's proto is set to the function's prototype using &lt;code&gt;{}.__proto__ = Person.prototype.&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Finally return the object and that's what we get into &lt;code&gt;me&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Concept Aside&lt;/em&gt;: Every function in javascript has a prototype object available on it. That's how you use &lt;code&gt;Array.prototype.map&lt;/code&gt;. And every object has a &lt;code&gt;__proto__&lt;/code&gt; object on it. For further watch this great video &lt;a href="https://www.youtube.com/watch?v=ZUHyZHwZtUY"&gt;Discovering JavaScript&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Since the &lt;code&gt;prototype&lt;/code&gt; and &lt;code&gt;__proto__&lt;/code&gt; refer to the same object, whenever you add a new function on the &lt;code&gt;prototype&lt;/code&gt; it becomes available on all instances.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hi Joe&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;you&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hi Alice&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So far we undestood how classes are created in javascript. Let's understand how to inherit classes in javascript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's create a new class called Employee that inherits the Person class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// create Employee prototype from Person prototype&lt;/span&gt;
&lt;span class="nx"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;joe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Joe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Developer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;joe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Joe&lt;/span&gt;
&lt;span class="nx"&gt;joe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Hi Joe&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Woah, we have finally inherited our Person class to create an Employee class and we didn't have to rewrite the &lt;code&gt;greet&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's see what just happened ??&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We created our &lt;code&gt;Employee&lt;/code&gt; class just like we created &lt;code&gt;Person&lt;/code&gt; class.

&lt;ul&gt;
&lt;li&gt;Inside our employee class we are calling the Person class by passing it &lt;code&gt;this&lt;/code&gt; reference. This is just like using &lt;code&gt;super&lt;/code&gt; keyword in ES6 classes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;This is the &lt;em&gt;most important part&lt;/em&gt;. We are recreating the &lt;em&gt;Employee prototype&lt;/em&gt; from &lt;em&gt;Person's prototype&lt;/em&gt; to get access to all the methods available on the &lt;code&gt;Person&lt;/code&gt; class.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Now you may ask yourself &lt;em&gt;Why use &lt;code&gt;Object.create&lt;/code&gt; and not just assign the Person prototype to Employee&lt;/em&gt;.&lt;/strong&gt;&lt;br&gt;
This is because we don't want Person and Employee to share the same prototype as objects in javascript are referenced. That's the whole point of inheriting Person.&lt;/p&gt;

&lt;p&gt;So that's how we use prototypal inheritance in javascript. The new ES6 classes are basically a syntactic sugar on top of it. Basically this is what actually happens under the hood.&lt;/p&gt;

&lt;p&gt;PS: You can find the complete code here &lt;a href="https://github.com/vyasriday/js-snippets/blob/master/src/js-concepts/prototypalInheritance.js"&gt;GitHub Repo&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>oops</category>
      <category>prototypalinheritance</category>
      <category>classes</category>
    </item>
    <item>
      <title>Setting Up Webpack For A JavaScript Library</title>
      <dc:creator>Hridayesh Sharma</dc:creator>
      <pubDate>Sat, 17 Aug 2019 11:53:38 +0000</pubDate>
      <link>https://dev.to/_hridaysharma/setting-up-webpack-for-a-javascript-library-2h8m</link>
      <guid>https://dev.to/_hridaysharma/setting-up-webpack-for-a-javascript-library-2h8m</guid>
      <description>&lt;p&gt;A couple of days ago I decided to learn how to write a simple javascript library. I wanted to make a javascript library with a couple of function in it which could be used by anyone just like jquery.&lt;/p&gt;

&lt;p&gt;I decided to use webpack for bundling. I got webpack set up but when I embedded my bundled script in my code I could not use any of the functions that I wanted to make available in library. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"myLibrary.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="cm"&gt;/* 
 using any of my library functions here gave me reference error.  
 */&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;I knew I was trying to do what other libraries like Redux, jquery etc do. But I did not understand how they did it. So I dug deeper into webpack to understand how to do that. &lt;/p&gt;

&lt;p&gt;I have created a small project for the demonstration of how I did it. The github repo can be found at &lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/vyasriday" rel="noopener noreferrer"&gt;
        vyasriday
      &lt;/a&gt; / &lt;a href="https://github.com/vyasriday/webpack-js-library" rel="noopener noreferrer"&gt;
        webpack-js-library
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      webpack setup for writing a javascript library and making it availabe as scirt or npm package
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Webpack Set Up For Writing Your Own JavaScript Library&lt;/h1&gt;

&lt;/div&gt;

&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;How to Make the Project Work&lt;/h2&gt;

&lt;/div&gt;

&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;1.  Clone the repository&lt;/h3&gt;

&lt;/div&gt;

&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;2.  npm install&lt;/h3&gt;

&lt;/div&gt;

&lt;div class="markdown-heading"&gt;
&lt;h3 class="heading-element"&gt;3. npm run build&lt;/h3&gt;

&lt;/div&gt;

&lt;p&gt;&lt;em&gt;There is a index.js generated inside dist directory. Add it as an external script to any of your projects.&lt;/em&gt;
&lt;em&gt;Any method can be accessed on $ in in your code after embedding the bundled file.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;For example you can use $.capitalize in your javascript to use capitalize method&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The babelrc is used by jest for code transpilation.&lt;/strong&gt;&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/vyasriday/webpack-js-library" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


&lt;p&gt;I created a &lt;strong&gt;src&lt;/strong&gt; directory in which all of my source code is present. &lt;strong&gt;&lt;em&gt;index.js&lt;/em&gt;&lt;/strong&gt; is the entry file for my project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;src&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt; &lt;em&gt;index.js&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt; &lt;em&gt;capitalize.js&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt; &lt;em&gt;unique.js&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt; &lt;em&gt;distinctString.js&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;webpack.config.js&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;src/index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;  
  &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;library&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;$&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;libraryTarget&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;umd&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.(&lt;/span&gt;&lt;span class="sr"&gt;js&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/node_modules/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;use&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;babel-loader&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;modules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;src&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;devtool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sourceMap&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In &lt;em&gt;webpack config&lt;/em&gt; there are two important properties in the &lt;strong&gt;output&lt;/strong&gt; for  bundling a javascript library.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;&lt;strong&gt;library: '$'&lt;/strong&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;strong&gt;libraryTarget: 'umd'&lt;/strong&gt;&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;library&lt;/strong&gt; is the name of the &lt;em&gt;variable&lt;/em&gt;, the code can be accessed as. For example jquery is available as $, just like that. Any function can be accessed like &lt;em&gt;$.name_of_method&lt;/em&gt; and the &lt;strong&gt;libraryTarget&lt;/strong&gt; is the library will be exposed. I am using &lt;em&gt;babel-loader&lt;/em&gt; for code transpilation with webpack. The bundled file is put into &lt;strong&gt;dist&lt;/strong&gt; directory after running build script.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;package.json&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"webpack-js-library"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"jest"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"roots"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="s2"&gt;"test/"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"dist/index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"webpack"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"jest"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"homepage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://github.com/vyasriday/webpack-js-library#readme"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"devDependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@babel/core"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.5.5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"@babel/preset-env"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^7.5.5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"babel-eslint"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^10.0.2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"babel-loader"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^8.0.6"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"eslint"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^6.1.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"jest"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^24.9.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"webpack"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"^4.39.2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In &lt;em&gt;package.json&lt;/em&gt; there is one important property &lt;strong&gt;main&lt;/strong&gt;. The main property of a package.json is a direction to the entry point to the module that the package.json is describing. Here I want to it to point to the bundled file which is the compiled code for the library.&lt;/p&gt;

&lt;p&gt;I am also using jest for a basic test setup. It is good to have tests for a library. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/index.js&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;capitalize&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./capitalize&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;unique&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./unique&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;longestDistinctSubstring&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./distinctString&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;capitalize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;unique&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;longestDistinctSubstring&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It is important tha you must export whatever you want to expose in your library. Here I am exposing three functions in the library. While bundling, webpack knows that it is supposed to put these functions on library.&lt;/p&gt;

&lt;p&gt;Now I can easily access my library like - &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"dist/index.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text/javascript"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;capitalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hridayesh&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That's how you can set up webpack to write a javascript library.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webpack</category>
      <category>jest</category>
      <category>javscriptbundler</category>
    </item>
  </channel>
</rss>
