<?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: anil kumar</title>
    <description>The latest articles on DEV Community by anil kumar (@anilkumarum).</description>
    <link>https://dev.to/anilkumarum</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%2F986082%2F3ca4fcf1-d18f-46a6-8611-674e0228a087.jpg</url>
      <title>DEV Community: anil kumar</title>
      <link>https://dev.to/anilkumarum</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anilkumarum"/>
    <language>en</language>
    <item>
      <title>Easiest way to add HMR in your chrome extension</title>
      <dc:creator>anil kumar</dc:creator>
      <pubDate>Wed, 07 Dec 2022 17:48:09 +0000</pubDate>
      <link>https://dev.to/anilkumarum/easiest-way-to-add-hmr-in-your-chrome-extension-3898</link>
      <guid>https://dev.to/anilkumarum/easiest-way-to-add-hmr-in-your-chrome-extension-3898</guid>
      <description>&lt;p&gt;When we try to build chrome extension,we notice that &lt;br&gt;
chrome don't auto-reload our extension.&lt;br&gt;
We need to reload our extension and also tab.&lt;br&gt;
In popup window, we need to close popup window then&lt;br&gt;
re-open window to view change.&lt;br&gt;
This is time consuming and frustrating job.&lt;br&gt;
We,developer don't want to do repeatable job.&lt;/p&gt;

&lt;p&gt;I found some plugins that attach with vite or webpack.&lt;br&gt;
When i debug these plugins, i found that they just &lt;br&gt;
reload whole chrome extension for just small style change.&lt;br&gt;
If i change some css property, these plugins reload injected page,&lt;br&gt;
background page and popup window. I also need to re-open popup manually.&lt;/p&gt;

&lt;p&gt;I have two options left:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Wait for somebody that create npm package for me. or,&lt;/li&gt;
&lt;li&gt;Write own npm package.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hate waiting so i decide to write own library and &lt;br&gt;
publish on npm to share with communities.&lt;/p&gt;

&lt;p&gt;NPM package for HMR in chrome extension is:&lt;br&gt;
&lt;a href="https://github.com/anilkumarum/crx-hmr"&gt;&lt;strong&gt;Crx-hmr&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is not another plugin. It is standlone full HMR library.&lt;br&gt;
It is dependencies-free package.&lt;/p&gt;

&lt;p&gt;I will explain how to start with crx-hmr to save our time and system resources.&lt;/p&gt;

&lt;h1&gt;
  
  
  what crx-hmr do ??
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Update changed file without reloading page or your extension.&lt;/li&gt;
&lt;li&gt;Support custom elements HMR.&lt;/li&gt;
&lt;li&gt;sass and pug supported (must be globally installed).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Benefits
&lt;/h1&gt;

&lt;p&gt;With &lt;strong&gt;crx-hmr&lt;/strong&gt;, we don't need to do&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manual reload chrome extension.&lt;/li&gt;
&lt;li&gt;Close and reopen popup window to view change.&lt;/li&gt;
&lt;li&gt;Need to refresh tab to view change.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Scaffolding your first chrome extension with crx-hmr
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Tools You Will Need:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Node installed on your computer. You can download Node at nodejs.org.&lt;/li&gt;
&lt;li&gt;A package manager called npm. It is automatically included in your installation of Node.&lt;/li&gt;
&lt;li&gt;A good code editor to work with our project files. I highly recommend using the editor Visual Studio Code. You can grab it at &lt;strong&gt;code.visualstudio.com&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First create project directory (folder).&lt;br&gt;
Open terminal in this directory.&lt;br&gt;
If window os, open vs-code or open git-bash.&lt;br&gt;
Run this command&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm create crx-hmr@latest&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once we run this command, Initial project structure will generated.&lt;/p&gt;

&lt;p&gt;Now, install crx-hmr package: &lt;code&gt;npm install&lt;/code&gt;.&lt;br&gt;
For start server, run &lt;code&gt;npm start&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Open chromium browser e.g chrome,brave,edge or vivaldi&lt;br&gt;
Go to extensions page: &lt;code&gt;chrome://extensions&lt;/code&gt;&lt;br&gt;
Activate developer mode and click on &lt;strong&gt;Load unpacked&lt;/strong&gt; button.&lt;br&gt;
Pick this project directory and&lt;br&gt;
Now start building extension.&lt;/p&gt;

&lt;h1&gt;
  
  
  From source
&lt;/h1&gt;

&lt;p&gt;crx-hmr has zero dependencies,It work without npm or package.json.&lt;br&gt;
Grab client.js and server.js file from github &lt;a href="https://github.com/anilkumarum/crx-hmr"&gt;crx-hmr&lt;/a&gt; &lt;br&gt;
then add client.js on chrome extension page.&lt;br&gt;
rename server.js to server.mjs&lt;br&gt;
For start server, run &lt;code&gt;npm start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For contribute or create new issues, visit github page &lt;a href="https://github.com/anilkumarum/crx-hmr"&gt;crx-hmr&lt;/a&gt;.&lt;br&gt;
Thanks for reading&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>hmr</category>
      <category>chromeextensions</category>
    </item>
    <item>
      <title>3 ways to add npm package in chrome extension.</title>
      <dc:creator>anil kumar</dc:creator>
      <pubDate>Wed, 07 Dec 2022 14:39:58 +0000</pubDate>
      <link>https://dev.to/anilkumarum/3-ways-to-add-npm-package-in-chrome-extension-3e3b</link>
      <guid>https://dev.to/anilkumarum/3-ways-to-add-npm-package-in-chrome-extension-3e3b</guid>
      <description>&lt;p&gt;When you try to add npm packages in chrome extension.&lt;br&gt;
Chrome extension show error&lt;br&gt;
&lt;code&gt;Import path must start with ./ or /&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You might think then how node.js find path ??&lt;br&gt;
When you run npm command. NPM replaces package name with real path.&lt;br&gt;
You need to just find pkg's real path so that Chrome browser will find it.&lt;br&gt;
Real path like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/node_modules/pkg-name/dist/main.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I show you 3 different methods, how to add npm packages in your chrome extension?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 1: find Manually&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, Go to node_modules directory then&lt;br&gt;
   go to npm package directory and open package.json.&lt;br&gt;
   Find &lt;strong&gt;exports&lt;/strong&gt; key value and copy &lt;strong&gt;import&lt;/strong&gt; file value or &lt;br&gt;
    file with .js or .mjs file extension.&lt;/p&gt;

&lt;p&gt;example:&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="w"&gt;   &lt;/span&gt;&lt;span class="nl"&gt;"exports"&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;"."&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;"require"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./lib/postcss.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;"import"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./lib/postcss.mjs"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
     &lt;/span&gt;&lt;span class="nl"&gt;"types"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"./lib/postcss.d.ts"&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;This is a relative path. Chrome browser cannot find this path.&lt;br&gt;
   We need to convert this into absolute path by joining path.&lt;br&gt;
   &lt;code&gt;/node_modules/{pkg_name}/{copied_path}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
   &lt;code&gt;/node_modules/postcss/lib/postcss.mjs&lt;/code&gt;&lt;br&gt;
   Now use this file path in place of package_name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 2: nodejs API&lt;/strong&gt;&lt;br&gt;
   Create path.js file and add this line.&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;fullPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;meta&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;package_name&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;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fullPath&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;match&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;node_modules.*&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="mi"&gt;0&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;path&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then add this line inside &lt;code&gt;scripts&lt;/code&gt; in package.json and run &lt;code&gt;npm run path&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;"path": "node --experimental-import-meta-resolve path.js"&lt;/code&gt;,&lt;br&gt;
Copy console output text.&lt;br&gt;
Now use this file's real path in place of package_name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 2: Automatic find and replace&lt;/strong&gt;&lt;br&gt;
   Install other npm package to find and replace&lt;br&gt;
   npm packages' virtual path to real path so that chrome browser will find it.&lt;/p&gt;

&lt;p&gt;Install &lt;a href="https://www.npmjs.com/package/path-fixxer"&gt;Path-fixxer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i path-fixxer&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;path-fixxer&lt;/strong&gt; provide 3 api to find and fix path.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Generate path.json file with absolute path of all dependencies.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;pkgPathJson&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path-fixxer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;pkgPathJson&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Auto-fix virtual path to real path of all dependencies&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&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;setAllPkgPath&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path-fixxer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;setAllPkgPath&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Auto-fix virtual path to real path of one dependency&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;realPath&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;path-fixxer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;setPkgPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;package_name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For contribute or create new issues, visit github page &lt;a href="https://github.com/anilkumarum/path-fixxer"&gt;path-fixxer&lt;/a&gt;.&lt;br&gt;
Thanks for reading&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>chromeextensions</category>
      <category>webdev</category>
      <category>npm</category>
    </item>
  </channel>
</rss>
