<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anand Shiva on Medium]]></title>
        <description><![CDATA[Stories by Anand Shiva on Medium]]></description>
        <link>https://medium.com/@anandshiva?source=rss-563d03a439f4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*D_PfVKemLlFOT4FNkr_iPA.jpeg</url>
            <title>Stories by Anand Shiva on Medium</title>
            <link>https://medium.com/@anandshiva?source=rss-563d03a439f4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 04:29:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anandshiva/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Set up Vite + Vue3 + Tailwind App]]></title>
            <link>https://anandshiva.medium.com/set-up-vite-vue3-tailwind-app-539992b31e8f?source=rss-563d03a439f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/539992b31e8f</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[tailwind-css]]></category>
            <category><![CDATA[vitejs]]></category>
            <dc:creator><![CDATA[Anand Shiva]]></dc:creator>
            <pubDate>Sat, 15 Apr 2023 11:24:06 GMT</pubDate>
            <atom:updated>2023-04-19T20:46:10.224Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*zTSGl6omkwfUR9gqTUhvsg.png" /></figure><h3>Set up Vue3 App based on Vite.</h3><p>We will use the <em>vue/cli</em> to scaffold the Vue project.</p><pre>npm init vue@latest</pre><p>Choose your preferred setup options in the CLI. I choose</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KrJwg6rkTgx4mToBvLyRDA.png" /></figure><p>Now cd into the folder and do</p><pre>yarn install &amp;&amp; yarn format &amp;&amp; yarn dev</pre><p>Now you started a Vue App. Pretty easy and fast, like Vite right?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*0F47qUe7s9NNolGCAEMG2A.png" /></figure><h3>Setup Tailwind</h3><p>Tailwind is a feature which only helps in easier development and is not needed in production builds. So all dependencies of Taildwind need to be added as devDependencies only.</p><pre>yarn add -D tailwindcss postcss autoprefixer</pre><p>Short explanations of what these packages do and why we need them.</p><h3>tailwindcss</h3><p>This is the core tailwind dependency. It includes all the base tailwind CSS/SASS files and configurations.</p><h3>postcss</h3><p>PostCSS takes a CSS file and provides an API to analyze and modify its rules (by transforming them into an <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">Abstract Syntax Tree</a>). This API can then be used by <a href="https://github.com/postcss/postcss#plugins">plugins</a> to do many useful things, e.g., to find errors automatically, or to insert vendor prefixes. Tailwind uses Postcss to do its functionality. Technically tailwind is a plugin for Postcss. That’s why it&#39;s mandatory to work with a tailwind.</p><h3>Autoprefixer</h3><p>It is a widely used tool for making use web apps work across all browsers. Since tailwind-generated CSS should also work across browsers tailwind uses this tool for extensive browser support. Another fun fact, Autoprefixer also uses Postcss to do its work.</p><p>Now let&#39;s add the configuration files that Tailwind uses to our app. We can use the files generated in this step to customize Tailwind according to our project. This can be for things like, including your own colour palettes, changing the default margin and padding sizes etc.</p><pre>npx tailwindcss init -p</pre><p>This process creates tailwind.config.js and postcss.config.js files.</p><p>Now edit your tailwind.config.js file so that it includes all the properties present in the below file.</p><pre>/** @type {import(&#39;tailwindcss&#39;).Config} */<br>export default {<br>  content: [<br>    &quot;./index.html&quot;,<br>    &quot;./src/**/*.{js,ts,jsx,tsx}&quot;,<br>  ],<br>  theme: {<br>    extend: {},<br>  },<br>  plugins: [],<br>}</pre><p>Now to use tailwind-specific CSS in our app, we need to include those CSS files in our application. Once we include them, they will be part of our build process and all needed utility CSS and resources will be imported from the tailwind module.</p><p>To include this, create a file, let&#39;s say, tailwind.cssand import some tailwind utilities into the file.</p><pre>@tailwind base;<br>@tailwind components;<br>@tailwind utilities;</pre><p>Now include this tailwind.css into your app’s entry point file(main.ts in this case). If you have not included TS while creating the project, it might be main.js or something similar.</p><pre>import &#39;../tailwind.css&#39;</pre><p>We now have a working version of our app which can use Tailwind.</p><pre>npm run dev</pre><p>This should start your app. Go add some tailwind-specific CSSes like the ones shown below</p><pre>text-red-900 text-3xl font-bold underline</pre><p>into your HTML, (.vue file) and your local website should change the colour to red and underline the text.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0D1Qz7hNLKe8TSf1NmmvfA.png" /></figure><p>If you want to see the full source code of the above-discussed code, check out this <a href="https://github.com/AnandShiva/vite-vue3-tailwind">repository</a>.</p><p>This <a href="https://github.com/AnandShiva/vite-vue3-tailwind">repository</a> is built as a Github template. So if you want to start coding right away create a project with this public template and GO!</p><p><strong>Some Miscellaneous tips:</strong></p><p>In addition to the basic setup, there are a couple of tools i recommend to be installed when working with Tailwind.</p><p><a href="https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss">VS Code Extension for Tailwind</a></p><p>This provided very useful auto suggestions when writing Tailwind CSS and references to documentation etc.</p><p>You might see a warning in line where we included @tailwind import section. Like this with error Unknown at rule @tailwindcss(unknownAtRules)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zFy1KY-1ediSZBmCDGShJQ.png" /><figcaption>@tailwind error</figcaption></figure><p>To remove this error the proper way you need to configure your VS Code to understand tailwind directives in CSS. For that you can use your .vscode/settings.json to configure it. Create file, lets say, css-data.json inside .vscodefolder. I should contain the definition of all tailwind specific language feature.</p><pre>// file .vscode/css-data.json<br>{<br>  &quot;version&quot;: 1.1,<br>  &quot;atDirectives&quot;: [<br>    {<br>      &quot;name&quot;: &quot;@tailwind&quot;,<br>      &quot;description&quot;: &quot;Use the `@tailwind` directive to insert Tailwind&#39;s `base`, `components`, `utilities` and `screens` styles into your CSS.&quot;,<br>      &quot;references&quot;: [<br>        {<br>          &quot;name&quot;: &quot;Tailwind Documentation&quot;,<br>          &quot;url&quot;: &quot;https://tailwindcss.com/docs/functions-and-directives#tailwind&quot;<br>        }<br>      ]<br>    },<br>    {<br>      &quot;name&quot;: &quot;@responsive&quot;,<br>      &quot;description&quot;: &quot;You can generate responsive variants of your own classes by wrapping their definitions in the `@responsive` directive:\n```css\n@responsive {\n  .alert {\n    background-color: #E53E3E;\n  }\n}\n```\n&quot;,<br>      &quot;references&quot;: [<br>        {<br>          &quot;name&quot;: &quot;Tailwind Documentation&quot;,<br>          &quot;url&quot;: &quot;https://tailwindcss.com/docs/functions-and-directives#responsive&quot;<br>        }<br>      ]<br>    },<br>    {<br>      &quot;name&quot;: &quot;@screen&quot;,<br>      &quot;description&quot;: &quot;The `@screen` directive allows you to create media queries that reference your breakpoints by **name** instead of duplicating their values in your own CSS:\n```css\n@screen sm {\n  /* ... */\n}\n```\n…gets transformed into this:\n```css\n@media (min-width: 640px) {\n  /* ... */\n}\n```\n&quot;,<br>      &quot;references&quot;: [<br>        {<br>          &quot;name&quot;: &quot;Tailwind Documentation&quot;,<br>          &quot;url&quot;: &quot;https://tailwindcss.com/docs/functions-and-directives#screen&quot;<br>        }<br>      ]<br>    },<br>    {<br>      &quot;name&quot;: &quot;@variants&quot;,<br>      &quot;description&quot;: &quot;Generate `hover`, `focus`, `active` and other **variants** of your own utilities by wrapping their definitions in the `@variants` directive:\n```css\n@variants hover, focus {\n   .btn-brand {\n    background-color: #3182CE;\n  }\n}\n```\n&quot;,<br>      &quot;references&quot;: [<br>        {<br>          &quot;name&quot;: &quot;Tailwind Documentation&quot;,<br>          &quot;url&quot;: &quot;https://tailwindcss.com/docs/functions-and-directives#variants&quot;<br>        }<br>      ]<br>    }<br>  ]<br>}</pre><pre>// .vscode/settings.json<br>{<br>  &quot;css.customData&quot;: [&quot;.vscode/css_custom_data.json&quot;],<br>  &quot;files.associations&quot;: {<br>    &quot;*.css&quot;: &quot;tailwindcss&quot;,<br>    &quot;*.sass&quot;: &quot;tailwindcss&quot;,<br>    &quot;*.scss&quot;: &quot;tailwindcss&quot;<br>  },<br>  &quot;editor.quickSuggestions&quot;: {<br>    &quot;strings&quot;: true<br>  },<br>  &quot;tailwindCSS.includeLanguages&quot;: {<br>    &quot;plaintext&quot;: &quot;html&quot;<br>  }<br>}</pre><p>Ensure your @tailwind directives are in some base.css file that is imported into your app and restart your app. Your warning should go without you ignoring them.</p><p>If you are okay with just ignoring all these warnings, <a href="https://flaviocopes.com/fix-unknown-at-rule-tailwind/">this blog</a> is for you.</p><p>That’s it for now!</p><p>Hopefully this blog helped you avoid some headwinds in your tailwind journey :)</p><p>If you faced any issues in the setup, please let me know in the comments. I promise to check it out and correct it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*eR_5tOgZMe-bMY64T9dIYQ.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=539992b31e8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Vuejs library project]]></title>
            <link>https://anandshiva.medium.com/building-vuejs-library-project-2a598dd568e2?source=rss-563d03a439f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a598dd568e2</guid>
            <category><![CDATA[npm-publish]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[npm-package]]></category>
            <category><![CDATA[vue]]></category>
            <dc:creator><![CDATA[Anand Shiva]]></dc:creator>
            <pubDate>Mon, 02 Jan 2023 07:30:12 GMT</pubDate>
            <atom:updated>2023-01-02T07:30:12.842Z</atom:updated>
            <content:encoded><![CDATA[<h3>All you need to know for building and publishing a Vuejs NPM library… — Part 1.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nqgwYSXoMRIb5HDSVIEavg.jpeg" /></figure><p>So the year is 2020, and <a href="https://vuejs.org/"><em>vuejs</em></a> has grown from being a side project of developers to a framework capable of building entire enterprise systems.</p><p>As a framework evolves so are the use cases and complexity of the applications built on top of them. A single project quickly becomes a Monolith and needs to be broken down into various modules based on use cases.</p><p>Let’s set some context first then I promise we will get our hand&#39;s dirty building a complete project from scratch.</p><p>Here are the common types of UI libraries that any mid to large-scale company with two or more products might face the need to have.</p><ol><li>UI Component library — containing base UI components like buttons, forms, etc.</li><li>Utility library —containing all reusable functions that can be common across projects.</li><li>CSS library — All common styles and SCSS variables to maintain a consistent look and feel.</li></ol><p>In this post, we will be building a library that encapsulates both a UI component library and a Utility library into a single project.</p><p>So let’s get started!</p><p>We will be using the super awesome <a href="https://cli.vuejs.org/"><em>vue/cli</em> </a>to build our library. So let’s <a href="https://docs.npmjs.com/downloading-and-installing-packages-globally">install that globally</a> first.</p><p>For window’s machine, <a href="https://www.howtogeek.com/194041/how-to-open-the-command-prompt-as-administrator-in-windows-8.1/">open the command prompt as admin</a> and for other OS’s prefix <em>sudo</em> and run below command.</p><pre>yarn global add @vue/cli <br>(or)<br>npm install -g @vue/cli</pre><p>Create a new project using <em>vue create [name-of-project] </em>and select the default options.</p><pre>vue create vue-library-demo-medium</pre><p>To verify the installation, cd into the created project folder via terminal and, run the below command, This will start a development server that runs a sample vue project.</p><pre>npm run serve <br>(or)<br>yarn serve</pre><p>Now that the basic setup is done, let’s start with building the library.</p><p>Let’s code a small button component and expose this via the library.</p><p>Create <em>button.vue</em> file in a preferred folder say <em>src/components</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4c555f1a75b679fca53e394aa9dfaf3/href">https://medium.com/media/b4c555f1a75b679fca53e394aa9dfaf3/href</a></iframe><p>now that we have a component ready, we need to expose it via our library so that anyone can import the component like below in any consuming project and use it.</p><pre>// vue-library-demo-medium is the library published to npm. <br> <br>const exampleLibrary = require(&#39;vue-library-demo-medium&#39;)</pre><pre>// exampleLibrary will contain all the files/components exposed by <br>// the package<br>const ButtonComponent = exampleLibrary.Button</pre><p>To accomplish this end result, let’s use <em>src/main.js</em> which will act as an entry point for our source files and expose our button component from there.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/658f15e3f32f02afa48309be18f5306b/href">https://medium.com/media/658f15e3f32f02afa48309be18f5306b/href</a></iframe><p>As and when you want to create and expose new components or library functions from the library just import the artifacts that need to be exposed in this main.js file and export them by adding them to the default export object.</p><p>Now that we have a common entry point to our library we need to build it so that all Vue objects are <a href="https://scotch.io/tutorials/javascript-transpilers-what-they-are-why-we-need-them">transpiled</a> into plain JS objects. For this, we use the inbuilt <em>vue-cli-service</em> command in our package.json. To specifically build a library we will add a new script in our package.json called <strong><em>build:lib.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m3EeDwW5NCvboHwVLA63rQ.png" /></figure><p>The <strong><em>vue-cli-service build </em></strong>command accepts the entry file for the library as the last parameter. (./src/main.js). Modify this option in case your entry file is named differently than <em>main.js</em> or the file is present anywhere else.</p><p>The <em>--target lib</em> instructs <em>vue</em> that this build should be <a href="https://cli.vuejs.org/guide/build-targets.html">targeted for building</a> a library.</p><p>There are many more options that can be provided to configure the build setup. A few frequently used ones are listed at the end of the blog.</p><pre>// command to generate build files. <br>npm run build:lib<br>(or)<br>yarn build:lib</pre><p>This build generates four files in our <em>dist</em><strong><em> </em></strong>folder (default output folder).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c8fbgXpwy0CPTs4L3_cokw.png" /></figure><blockquote>All the css contained in our .vue files are combined and compiled into the <em>dist/vue-library-demo-medium.css</em></blockquote><blockquote>Be sure to import this also specifically in our consuming project. Else components will not have styles.</blockquote><p>There are three JS files targeting <em>umd</em>, <em>common,</em> and <em>minified umd’s javascript module </em>builds off the same source code. For details about different js-modules refer to the below link.</p><p><a href="https://www.davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/">What is AMD, CommonJS, and UMD?</a></p><p>Now that bundled build file is generated we should ensure that our library project always uses this generated build file as the entry point when anyone imports our library.</p><p>The value of the <a href="https://bytearcher.com/articles/main-property-in-package.json-defines-entry-point/">main key property in package.json</a> determines the file that will be exported when someone imports a project.</p><p>So let’s change our main property in package.json to point to the <em>dist/vue-library-demo-medium.umd.min.js.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aBNSF3f0G-klBGXuAivCGQ.png" /></figure><p>If you prefer to use a different module system, use that file instead of using umd.min.js</p><p>If you are not sure which module to use, use UMD. It is compatible with both common.js and AMD.</p><p>Similar to exposing a <em>.vue</em> component. We can also expose any Js files in src/<em>main.js</em>. This exported JS file can be imported and run like a normal file present in your local project. Those use cases are very common if you have common business logic across your organization and they need to be part of the library.</p><p>Now that we have built the library, it&#39;s time to publish the library as <em>npm</em> package so that we can install it anywhere using npm install.</p><p>It’s really simple to publish a new package in npm. Just ensure that your package name is not already used. The name property in the package.json denoted the name of the library. If it is already taken, decide on a new name and change it in package.json.</p><ol><li>Create an <em>npm</em> account if you already don’t have an account visit <a href="https://www.npmjs.com/signup">https://www.npmjs.com/signup</a></li><li>Log in to npm using the terminal with your credentials.</li></ol><pre>npm login</pre><p>After logging in we can publish the package directly.</p><p>Note: See to that in package.json the<a href="https://docs.npmjs.com/files/package.json#private"> private property</a> is not set to be <em>true</em> when publishing. If so change it to false else you will not able able to publish the project.</p><p>Now run this below magic command,</p><pre>npm publish<br>(or)<br>yarn publish</pre><p>This will publish the version of the library as specified in the version field of package.json. Yarn will ask you for a version when publishing. It will update package.json with that corresponding version and publish the package. Rest all steps remains the same w.r.t to <a href="https://classic.yarnpkg.com/en/docs/publishing-a-package/">yarn for publishing</a>.</p><p>Also, remember to increment the <em>version</em> field in <em>package.json</em> before each new library publishing. Else publish will fail since that version already exists in the NPM repository. Follow <a href="https://docs.npmjs.com/about-semantic-versioning">semantic versioning</a> when deciding the version new version number.</p><h3>How to Consume the library</h3><p>Now that we have published the library,<strong><em> it’s time to install it locally and consume it in a sample Vue project</em></strong>. Let&#39;s see the brief steps for that.</p><p>create a dummy folder with the name <em>consuming-project, </em>open the terminal there, and run the below command.</p><pre>vue create consumption-project</pre><p>Select all the default values, and once the project is created,</p><ol><li>Install the library we just created and published to the consuming project by running.</li></ol><pre>yarn add vue-library-demo-medium<br>or<br>npm install vue-library-demo-medium -S</pre><p>3. Inside the <em>App.vue</em> entry component file we can get can import and register the library component to get it working like the code sample below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56e8cabf181aa4d22ed64e6edbfa3153/href">https://medium.com/media/56e8cabf181aa4d22ed64e6edbfa3153/href</a></iframe><p>Here we imported the component we created in the library and registered it to the Vue instance. Then in the template section, we can use the registered component as if it’s a local component.</p><pre>// remember to import the css files also when rendering the UI<br>// components also if your library exposes them. Else Component will // not have any styles<br>// eg for importing css file in JS file. <br>const libCss = require(&#39;vue-library-demo-medium/dist/cssfile.css&#39;)</pre><p>Now run</p><pre>npm run serve <br>(or)<br>yarn serve</pre><p>And Tada!! Our Vue component sees the world via a consumption project.</p><p>Before we sign off, we have more good news.</p><p>Just like our component library, our journey to build a component library just started. There are various nuances and concepts that could help us make our library super efficient and flexible.</p><p>In the second post of this series, we will be looking into various advanced options for constructing a Vue library, their use cases, optimizations, and pitfalls.</p><p>If you face any <a href="https://eslint.org/"><em>eslint</em></a> issues during any steps in the blog refer to this <a href="https://www.thetopsites.net/article/50895397.shtml">post</a>.</p><p>Reference section.</p><p>Vue cli options when building a project. A list of a few parameters includes.</p><p>1. --dest</p><p>specifies where to store the generated build files. <em>default: dist</em></p><pre>// eg to use build_output/lib as output folder. <br>vue-cli-service build --dest build_output/lib</pre><p>2. --mode</p><p>specifies which <a href="https://webpack.js.org/configuration/mode/">webpack build option </a>to use for generating the build files. (<em>default: production)</em></p><p>Production builds compromise debugging messages to enhance performance. And development builds are optimized for faster build times and error messages.</p><pre>// eg for dev build<br>vue-cli-service build --mode development</pre><p>3. --watch</p><p>this runs the build process in watch mode, meaning any change in the source file will re-trigger the build process again using <a href="https://webpack.js.org/concepts/hot-module-replacement/">hot module replacement</a></p><pre>// eg for running library build in watch mode and dev mode. </pre><pre>vue-cli-service build --mode development --watch</pre><p>4.-- name</p><p>used to specify the name of the generated build files. <em>default: name of the package.</em></p><pre>// eg for name property. <br>// this generates build files with name new-lib-name.umd.js etc..</pre><pre>vue-cli-service build --name new-lib-name</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4P9s7EOCDI6OOJnXsYr-3A.jpeg" /></figure><p>Thanks for reading! Happy coding!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a598dd568e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a Chrome Extension in Vuejs.]]></title>
            <link>https://anandshiva.medium.com/building-a-chrome-extension-in-vuejs-a4868a90236b?source=rss-563d03a439f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4868a90236b</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[chrome-extension]]></category>
            <dc:creator><![CDATA[Anand Shiva]]></dc:creator>
            <pubDate>Mon, 02 Jan 2023 07:12:47 GMT</pubDate>
            <atom:updated>2023-01-02T07:12:47.787Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*xpMWNEriOLYMWVNCgKk5Ig.jpeg" /></figure><p>Armed with experiences from building an <a href="https://chrome.google.com/webstore/detail/commerceiq-insights/knmkalnmhkhjpojmnlfkmfbdagpcommg/related">Enterprise-grade chrome extension</a> and a pet project extension, both built on the best JS framework out there, <strong><em>Vuejs</em></strong>, I wanted to share with you all how the holy marriage of these holy grails took place.</p><blockquote><em>Note: These extensions and blog are built with chrome extension manifest v2 and written 2 years back. The latest recommendation is to use </em><a href="https://developer.chrome.com/docs/extensions/mv3/intro/"><em>manifest v3</em></a><em> and updated versions of the npm libraries. So please take the contents in this blog considering these limitations. Always feel free to point out if mentioned contents are causing any errors. Will update the blog.</em></blockquote><p>Before we get our hands dirty, let&#39;s reflect on a few of the design choices that were made and give a high-level summary of what we are trying to build.</p><p>Goals:</p><ul><li>We should be able to code fully in VueJs and see to it that the final bundled js is used by the chrome extension for injecting into the browser.</li><li>Use all modern JS web development standards in extension development</li><li>Have a module build system that is not limited to use for primary web apps (like vue-cli) and have future-proof and extensible build configurations that are needed for building an extension.</li></ul><h3>Pure JS extension setup</h3><p>First, let’s try to make a working prototype of an extension from scratch in pure JS, and later let’s integrate VueJs on top of that. This extension will show a dummy page whenever you open a new tab overriding the default new tab page.</p><p>This is the current directory structure, and the default values of newTab.html and manifest.json are listed below. For now, the <em>background.js</em> file can be created with empty content.</p><pre>|<br>├── src<br>│   ├── background.js<br>│   ├── manifest.json<br>│   ├── newTab.html<br>│   └── newTab.js<br>└── webpack.config.js</pre><blockquote>manifest.js contents.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1a79692c85aa6f0bb881d4e95bf589b/href">https://medium.com/media/d1a79692c85aa6f0bb881d4e95bf589b/href</a></iframe><blockquote><em>newTab.html contents.</em></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0251fccad746031e4e83c38b0f6095c2/href">https://medium.com/media/0251fccad746031e4e83c38b0f6095c2/href</a></iframe><p>Now that the needed files are in place, install the extension locally by following <a href="https://www.cnet.com/how-to/how-to-install-chrome-extensions-manually/">these steps</a>. <br>You need to load the src folder at this point, which contains the manifest.json.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FMFCIHJCab7y54D9W71%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FMFCIHJCab7y54D9W71%2Fgiphy.mp4&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FMFCIHJCab7y54D9W71%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="271" frameborder="0" scrolling="no"><a href="https://medium.com/media/d30f1f23a98d281bd3f819af9886d1c9/href">https://medium.com/media/d30f1f23a98d281bd3f819af9886d1c9/href</a></iframe><p>After loading, our extension would have <a href="https://developer.chrome.com/extensions/override">taken the control of the newTab</a> page since we modified that behavior with this code in the manifest.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KVsbmgWBfb9AkZy9qS47yQ.png" /></figure><p>It’s really that simple to make a basic extension. But with this current setup, we will not be able to scale it with the latest development features like,</p><ul><li>Webpack build setup</li><li>CI/CD integrations</li><li>Code modularization with</li><li>Hot Module Replacement</li><li>Use modern frameworks like React, Vue, etc.</li></ul><p>To achieve this let’s convert our extension into an npm project and introduce a webpack build setup into this.</p><pre>npm init -y<br>npm install webpack webpack-cli copy-webpack-plugin webpack-extension-reloader --save-dev</pre><p>Let’s create webpack.config.js at the root of the project with the below content.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2af7f6bea83b54b8b86f88e4b7a687b9/href">https://medium.com/media/2af7f6bea83b54b8b86f88e4b7a687b9/href</a></iframe><p>In the <em>package.json, </em>add the following script</p><pre>&quot;scripts&quot;: {<br>   &quot;build&quot;: &quot;webpack --mode=&#39;development&#39; --watch&quot;<br>},</pre><p>And run <em>npm run build, </em>to see <em>webpack</em> build being degenerated with HMR.</p><p>Every time a file is changed,<em> </em><a href="https://www.npmjs.com/package/webpack-extension-reloader"><em>webpack-extension-reloader</em></a><em> </em>will take care of updating the extension in the browser and reloading the webpage where the scripts are injected and background page scripts reinitialised.</p><h3>Integrate Vuejs</h3><p>Now that the pure JS version of the extension is in place, let’s create a <em>.vue</em> file and see how we can consume that in our project.</p><pre>npm install vue vue-template-compiler --save</pre><pre>npm install babel-loader css-loader @babel/core vue-loader mini-css-extract-plugin --save-dev</pre><p>These plugins help webpack understand Vue files, and CSS and create a final bundle file (newTab.js) which is being consumed in the HTML file.</p><p>Let’s configure the webpack.config.js to use these plugins and enable HMR.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/678b5d4fd8ab9c41d5318e3c54a62c0a/href">https://medium.com/media/678b5d4fd8ab9c41d5318e3c54a62c0a/href</a></iframe><p>To give a better contextual understanding let&#39;s go over each of the plugins used in the webpack setup and its use. <br>copy-webpack-plugin</p><blockquote><a href="https://webpack.js.org/plugins/copy-webpack-plugin/">CopyWebpackPlugin</a></blockquote><p>We have <em>src </em>and<em> dist </em>folders<em>. </em>Many files like the HTML and manifest.json should be copied from src to dist without any changes. This plugin will see to that.</p><blockquote><a href="https://github.com/rubenspgcavalcante/webpack-extension-reloader">webpack-extension-reloader</a></blockquote><p>This is is to achieve the <a href="https://webpack.js.org/concepts/hot-module-replacement/">Hot Module Replacement</a> similar to what we have in webpack-dev-server. This ecosystem of HMR wrt to extension is in a nascent stage of development and technically just reloads the extensions and refreshes the currently open tab where the extension is used.</p><blockquote><a href="https://vue-loader.vuejs.org/guide/">Vue Loader Plugin</a></blockquote><p>It&#39;s used for transforming .vue files content into JS, CSS, and HTML content. <br>Interestingly the vue-loader-plugin does only delegation work by internally re-routing to corresponding plugins that are mentioned for content types in <em>webpack.config.js</em>. E.g. in our case <em>js </em>content points to <a href="https://github.com/babel/babel-loader">babel-loader</a>.</p><pre>{<br> test: /\.js$/,<br> loaders: &#39;babel-loader&#39;,<br> exclude: /node_modules/<br>},</pre><blockquote><a href="https://github.com/babel/babel-loader">babel-loader</a></blockquote><p>This plugin allows transpiling JavaScript files to old JS version to support older browsers.</p><blockquote><a href="https://webpack.js.org/plugins/mini-css-extract-plugin/">MiniCssExtractPlugin</a></blockquote><p>This plugin supported officially by webpack is used to extract CSS into a separate file, combining all CSS present in vue template, imported CSS files, SCSS files, etc.</p><blockquote><a href="https://webpack.js.org/loaders/css-loader/">css-loader</a></blockquote><p>Another official webpack supported plugin, it interprets @import and url() like import/require() and will resolve them.</p><blockquote><a href="https://webpack.js.org/loaders/sass-loader/">sass-loader</a></blockquote><p>Converts SASS/SCSS code into CSS code. This plugin output will again be sent to the css-loader. This enables us to write <em>style blocks </em>in <em>vue files</em> with <em>lang</em> as scss/sass.</p><pre>&lt;style lang=&quot;scss&quot;&gt;<br> .parant-scss {<br>   input{<br>    border: none;<br>   }<br>&lt;/style&gt;</pre><p>w.r.t to CSS the transform flow looks as follows</p><pre>sass-loader -&gt; css-loader -&gt; mini-css-extract-plugin</pre><p>Ufff !! Good to know you are still with us. Let’s continue getting our hands dirty with code again.</p><p>Create a new file called <em>index.vue.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfbf677ae93787541b725d3d76b59569/href">https://medium.com/media/bfbf677ae93787541b725d3d76b59569/href</a></iframe><p>And in any entry point of the Extension file like page inject or newTab entry or popup entry we can write this content to import the Vue file and inflate the Vue instance in that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf9c0b77e8e038d23ab5ae7c94c63248/href">https://medium.com/media/bf9c0b77e8e038d23ab5ae7c94c63248/href</a></iframe><p>Note: In this blog we have considered the case of overriding the newTab page and building it with Vue. But you can follow these exact same steps make entry file as <a href="https://developer.chrome.com/extensions/content_scripts">content-script</a> / <a href="https://developer.chrome.com/extensions/options">options-page</a> etc., and write the vue instantiation code^ to work with any vue files there.</p><p>After these our folder structure will look like this.</p><pre>├── LICENSE<br>├── README.md<br>├── package-lock.json<br>├── package.json<br>├── dist<br>├── src<br>│   ├── Index.vue<br>│   ├── background.js<br>│   ├── manifest.json<br>│   ├── newTab.html<br>│   └── newTab.js<br>└── webpack.config.js</pre><p>Now that we have all the things ready, run <strong><em>npm run build</em></strong></p><p>Sometimes due to the nature of the JS ecosystem, a few libraries may evolve and become incompatible with each other when you are trying this blog. So attaching my package.json below. If you face any difficulties during the build, try to match your package.json library’s version to the one below and try again.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c39f99c618b38b942f986b6b6b2bbac/href">https://medium.com/media/4c39f99c618b38b942f986b6b6b2bbac/href</a></iframe><p>The build should now succeed and be waiting in watch mode, listening for file changes. This will generate the build files into the <em>dist </em>directory. And we can load the contents of the dist folder via <a href="https://www.cnet.com/how-to/how-to-install-chrome-extensions-manually/">Load Unpacked</a> and select the <em>dist</em> folder which was populated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*_wFWW-upDyCbAUceYDtYbg.gif" /></figure><p>And there you have it. Your own chrome extension built on <em>Vue</em> :)</p><p>Hope this blog helped you a little in whatever you were trying to achieve. If not please let me know in the comments.<br>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4868a90236b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Trigger Jenkins job from Nodejs.]]></title>
            <link>https://anandshiva.medium.com/trigger-jenkins-job-from-nodejs-f23614a8a8fd?source=rss-563d03a439f4------2</link>
            <guid isPermaLink="false">https://medium.com/p/f23614a8a8fd</guid>
            <category><![CDATA[stream]]></category>
            <category><![CDATA[ci-cd-pipeline]]></category>
            <category><![CDATA[jenkins]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Anand Shiva]]></dc:creator>
            <pubDate>Tue, 15 Dec 2020 09:57:41 GMT</pubDate>
            <atom:updated>2020-12-15T09:57:41.044Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iwXu9swqrWdPf-XAZEbK5w.png" /></figure><p>This blog exists because as of November 2020, Jenkins does not have an official Nodejs API. So till Jenkins maintainers realize the <a href="https://medium.com/@slsoftworks/javascript-world-domination-af9ca2ee5070">power of Javascript</a>, I am hoping this blog fills that void.</p><p>Luckily, Jenkins does expose a lot of REST API’s (**<em>with little documentation</em>**) which will do the trick, we will use those and try to trigger different types of Jenkins jobs like, <em>jobs with files</em>, <em>jobs with parameters, jobs with both parameters, and files.</em></p><p>Even though there are a lot of npm libraries for doing this, it&#39;s a relatively simple use case that can be solved with a few lines of code rather than bloating our application with third-party libraries.</p><p>Another reason for not going with an <a href="https://medium.com/intrinsic/common-node-js-attack-vectors-the-dangers-of-malicious-modules-863ae949e7e8">npm library is security</a>. Jenkins server credentials are very critical, so sharing them with not so popular libraries puts your entire DevOps at risk. And last time I checked taking a risk is cool only if you are an entrepreneur, not a developer.</p><p>If you are connecting to a Jenkins server which has authentication enabled, below section is relevant for you, and you need to provide this Authentication token as a header in all your request.</p><p>The Authentication token is generated by doing a base64 encoding of your username and password. If you don’t have authentication enabled, you can skip these headers in all the following examples.</p><pre>headers: {<br> Authorization: `Basic<br>      ${Buffer.from(`${userName}:${password}`).toString(&#39;base64&#39;)}`,<br>}</pre><h3>Trigger Jenkins job without parameter</h3><p>For this, You merely need to perform an HTTP POST on JENKINS_URL/job/JOBNAME/build.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9bd0240d181d3f89ef12fed536fb071/href">https://medium.com/media/e9bd0240d181d3f89ef12fed536fb071/href</a></iframe><h3>Jenkins Job with parameters.</h3><p>There are multiple ways of doing this, first, you can POST to JENKINS_URL/job/JOB_NAME/buildWithParameterswith stringified Jenkins Job parameters like the example code shown below, or submit a POST request with form data request with Jenkins parameters as form fields.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f75afe80d4eedd9290e7a51b1d4e1146/href">https://medium.com/media/f75afe80d4eedd9290e7a51b1d4e1146/href</a></iframe><p>Like life, our use-case also keeps getting difficult with time, so like life itself, let’s hope for a happy ending and proceed anyway.</p><h3>Jenkins Job with a file as a job parameter</h3><p>This approach implementation will change based on which format your file resides in your program (Local file, single request stream, multi-part file request, file string, etc). In the end, we will have to convert the file blob into a Readable stream and create a form request out of it and do a POST to the Jenkins server.</p><p>The following code transpiles the mumbo jumbo paragraph above into user understandable JS(*<em>hopefully</em>).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbaa995014292e4d7825aef59ee23134/href">https://medium.com/media/bbaa995014292e4d7825aef59ee23134/href</a></iframe><p>We are using a library called <a href="https://www.npmjs.com/package/form-data">form-data</a> for converting all these inputs into an HTTP form-data format request. So be sure to check them out in case you face any such issues with passing file inputs for full options.</p><p>In case your file to upload does not reside in your local, you can convert the file to a string, create a Readable stream using the below code; pass that as input to form-data. Keep in mind if you are using your own streams other than fs file streams, you need to manually provide all the file information to form-data so that the correct content type, request length are populated.</p><pre>const { Readable } = require(&#39;stream&#39;);<br>const FormData = require(&#39;form-data&#39;);<br>const data = new FormData();<br>const readableStream = Readable.from([fileString]);<br>data.append(&#39;file0&#39;, readableStream,{<br>   filename: &#39;filename.csv&#39;, // ... or:<br>   //   filepath: <br>   contentType: &#39;text/csv&#39;,<br>   // knownLength: 5,<br> });</pre><p>With this new power to control Jenkins from NodeJS along with <a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks">git-hooks</a> and CI/CD pipeline integration, we can achieve and automate a lot of common developer tasks.</p><p>These include but not limited to</p><ul><li>Run static code checks analysis jobs on every git commit.</li><li>Run deployment and unit test case jobs directly from your node project.</li><li>Run backup tasks triggered by various business and user events</li></ul><p>Thanks for reading.</p><p>This is my first blog in medium, so if you liked it give this post a clap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*eR_5tOgZMe-bMY64T9dIYQ.gif" /></figure><p>If you have any feedback or face some issues let me know in the comments section, I promise to look into it :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f23614a8a8fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>