<?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>Forem: Ajit Singh Kamal</title>
    <description>The latest articles on Forem by Ajit Singh Kamal (@ajitsinghkamal).</description>
    <link>https://forem.com/ajitsinghkamal</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%2F151428%2F1de0ff3f-6641-4d63-9266-bd54e866837d.jpg</url>
      <title>Forem: Ajit Singh Kamal</title>
      <link>https://forem.com/ajitsinghkamal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ajitsinghkamal"/>
    <language>en</language>
    <item>
      <title>Using emotionJs with Vite</title>
      <dc:creator>Ajit Singh Kamal</dc:creator>
      <pubDate>Mon, 15 Mar 2021 00:02:58 +0000</pubDate>
      <link>https://forem.com/ajitsinghkamal/using-emotionjs-with-vite-2ndj</link>
      <guid>https://forem.com/ajitsinghkamal/using-emotionjs-with-vite-2ndj</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Disclaimer: the post is now quite old. Vite has a dedicated react plugin which you can easily configure now to get the intended outcome. The comment section has better snippets to help you :)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://vitejs.dev/" rel="noopener noreferrer"&gt;&lt;strong&gt;Vite&lt;/strong&gt;&lt;/a&gt; is the next cool thing everyone's talking about. For the uninitiated, it's a tool that provides a dev server with a blazingly fast refresh(HMR) speed and comes loaded with the Rollup module bundler for generating highly optimized builds.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/yyx990803" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F184319%2Fd23f649a-12f9-4b67-bc84-58ba7ad27218.jpg" alt="yyx990803"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/yyx990803/announcing-vite-2-0-2f0a" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Announcing Vite 2.0&lt;/h2&gt;
      &lt;h3&gt;Evan You ・ Feb 16 '21&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#webdev&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#vite&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;Most of you would already be familiar with &lt;a href="https://github.com/emotion-js/emotion" rel="noopener noreferrer"&gt;&lt;strong&gt;EmotionJs&lt;/strong&gt;&lt;/a&gt; - a very popular CSS-in-JS library. It comes with a react flavor which provides a &lt;code&gt;css&lt;/code&gt; prop that greatly enhances the overall developer experience of writing styles in your react component. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyftj2lb4pyf39btuqqs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyftj2lb4pyf39btuqqs.png" alt="emotion's css prop official doc example" width="800" height="361"&gt;&lt;/a&gt;&lt;br&gt;
 &lt;em&gt;An example from the official &lt;a href="https://emotion.sh/docs/css-prop" rel="noopener noreferrer"&gt;docs&lt;/a&gt;&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;However, every time we need to use this very convenient CSS prop, we would need to add emotion's custom JSX pragma on the very top of our &lt;code&gt;jsx&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @jsx jsx */
import { jsx } from '@emotion/react'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are planning to give Vite a shot - The good news is that you don't need to do any additional tinkering. Emotion will work without any break when using the above approach. But, there is a far better way, with which we can simply avoid importing this chunk of import in all our JSX files. &lt;/p&gt;

&lt;p&gt;To do that - you'd need to update the &lt;code&gt;esbuild&lt;/code&gt; options in your project's &lt;code&gt;vite.config&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;import { defineConfig } from 'vite';


// https://vitejs.dev/config/

export default defineConfig({
   plugins: [...],
   esbuild: {
     jsxFactory: `jsx`,
     jsxInject: `import { jsx } from '@emotion/react'`,
   }
   ...
});

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

&lt;/div&gt;



&lt;p&gt;Vite uses &lt;a href="https://esbuild.github.io/" rel="noopener noreferrer"&gt;&lt;em&gt;esbuild&lt;/em&gt;&lt;/a&gt; under the hood for compilation. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;jsxInject&lt;/code&gt; simply set esbuild's &lt;code&gt;--inject&lt;/code&gt; transformation option and auto imports the provided module in all &lt;code&gt;.jsx&lt;/code&gt; files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;jsxFactory&lt;/code&gt; overrides the default &lt;code&gt;React.creatElement&lt;/code&gt; with emotion&lt;code&gt;s&lt;/code&gt;jsx` factory function.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And, that's it. You can now use emotion in all your jsx components by default. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you want you can also import the &lt;code&gt;css&lt;/code&gt; function along with &lt;code&gt;jsx&lt;/code&gt; to avoid it importing later in your components to construct serialized style objects.&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jsxInject: `import {jsx, css} from '@emotion/react'` 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>vite</category>
      <category>emotionjs</category>
      <category>react</category>
    </item>
    <item>
      <title>Explaining the difference between an Array and an Object in Javascript with a Rick and Morty analogy.</title>
      <dc:creator>Ajit Singh Kamal</dc:creator>
      <pubDate>Sat, 30 May 2020 19:59:20 +0000</pubDate>
      <link>https://forem.com/ajitsinghkamal/explaining-the-difference-between-an-array-and-an-object-in-javascript-with-a-rick-and-morty-analogy-12l3</link>
      <guid>https://forem.com/ajitsinghkamal/explaining-the-difference-between-an-array-and-an-object-in-javascript-with-a-rick-and-morty-analogy-12l3</guid>
      <description>&lt;p&gt;An Object is a Morty. Every universe has its own version of Morty. There's lizard Morty, lawyer Morty, fat Morty, evil Morty, and so on.&lt;br&gt;
So, if you are an omnipresent creator and you want to create a new Morty in one of the infinite timelines, you would simply do&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Morty = {};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Cool, we just created a personality-less piece of crap. Every Object can have their own personality which sets them apart. These traits can be expressed as &lt;code&gt;property&lt;/code&gt; and &lt;code&gt;methods&lt;/code&gt;. &lt;br&gt;
So, for a Morty these properties can be eyes, nose, limbs. The evil Morty has one eye, the lizard Morty has a tail, and the mutant Morty, well... you get the gist.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Morty = {
  eyes: 2,
  hammerForHead: false,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Similar to properties which defines the traits, an object's method defines the behavior. So, for a Morty,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Morty = {
  eyes: 2,
  hammerForHead: false,
  ...,
  panic: function() {
      // console.log('find Rick for help')
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You see, when creating a Morty with an objects, we can play around with the possibilities. An object is pretty flexible.&lt;/p&gt;

&lt;p&gt;Now, suppose Rick wants an army of robot Mortys. So he decides to build a robot Morty maker machine with these traits.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Morty = {
    eyes: 2,
    hand: 2,
    tail: 0 
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now as you see, the possibilities you can have for the kind of Morty reduces drastically from infinite to a fixed set because Rick's machine can only possibly make robot Mortys of a certain type. Each morty must have their eye at the proper position, their heads at their designated place, their tails between their legs. Basically, everything must go to their properly indexed position for the machine to build up a Morty. &lt;/p&gt;

&lt;p&gt;So, if the eyes are assembled first, then the limbs, and so on and Rick being the lazy smartass as always just do this,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const BotMorty = {
     0: 2,
     1: 2,
     2: 0,
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Also, none of these robot Mortys have free will. Rick predefined their behavior and these Morty can only do so. To make this process easy, Rick can create this in this form and the machine would assign 2 to zeroeth position, 1 to first, and 2 to second position.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const BotMorty = [ 2, 2, 0 ];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That's it, an array is also an object secretly but a special one. Its properties are defined as indexes. You can put anything in these indexes and can work with it using the methods it already provides.&lt;/p&gt;

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