Dzulqarnain Nasir

  • December 10, 2018

    IoC in JavaScript with InversifyJS

    In C#, interfaces are used to set up a definition that classes can implement. Any class that implements an interface must adhere to the definition declared by the interface.

    Think of them as a sort of menu you’d find at a typical pizzeria. The menu says they serve Margherita pizza.

    Then you have the classes that implement the interfaces - i.e. the cooks that prepare the pizza mentioned in the menu.

    The pizzeria may have multiple cooks, and the cooks may differ in the way they prepare the dish - i.e. one cook may use more cheese, and another may use less salt in the dough. Regardless, the resulting product will still be served as Margherita pizza.

    Similarly, the consumers of your modules should only be made aware of the “menu”, without having to worry about how the pizza is prepared. This way, you can swap out the “cook” whenever you want, without the consumers being any the wiser.

    This is something I’ve been taking for granted in .NET development, and while there have been some efforts in bringing this concept to JavaScript, the implementation has not been that great - until now.

    Read more »
  • November 26, 2018

    Is Yarn still faster than NPM in late 2018?

    If you’re like me, chances are you’ve heard about Yarn, an alternative to the Node Package Manager (NPM). You may have even heard how much faster it is compared to NPM.

    The dust has settled since Yarn first entered the scene, and NPM has punched back with quite a few updates. So it’s time to see if anything has changed.

    Read more »
  • November 24, 2018

    How To Auto-publish GitHub Pages Using Azure Functions

    If you’re reading this, and it’s past 24th November 2018 at 10:00 EET, it means my small experiment worked.

    As you have probably noticed, I’ve moved away from Wordpress to GitHub Pages, which runs on Jekyll, a static site generator. The one thing I miss from my Wordpress days is the ability to schedule publishing new posts.

    GitHub Pages requires you to manually add posts to the master branch of the repository, which means that if I wanted to publish the post I wrote last night at 9am the next morning, I would have to physically push or merge said post to the repository at 9am the next morning. Needless to say, this is far from ideal.

    So I decided to use Azure Functions to call GitHub’s API to instruct it to build my GitHub Pages project, which should automatically publish my posts based on their publish time.

    Read more »
  • November 06, 2018

    Smart Bundle Management Using JavaScript Modules

    I recently read an article on SmashingMagazine on managing bundles for modern and legacy browsers, in which the author had discovered that bundles built for legacy browsers are, on average, 25% larger than those created for modern browsers.

    This is mainly due to the fact that native support for ES6 has resulted in the possibility to achieve the same results with less code. Unfortunately, legacy browsers such as Internet Explorer lack support for most, if not all, of ES6, forcing developers to include workarounds and polyfills just to get things working, resulting in larger bundle files.

    In the article, the author suggested creating separate bundles for each platform, and employing server-side browser sniffing to serve the appropriate bundle to users.

    While this method works, there are numerous reasons why browser sniffing is bad.

    Read more »
  • June 07, 2018

    Webpack, Moment.js and CDNs

    In one of my projects, I needed to implement MomentJS with localisation. I thought, “Alright, this should be simple enough.”

    Here’s the funny thing about “simple” tasks. There’s always a catch. It just so happens the catch with this one is that I had to make the site load MomentJS from a CDN server, and fallback to a local copy in the event CDN is down.

    Still, it’s not really a big deal - or so I thought.

    Once I’ve set everything up, I ran Webpack. I immediately noticed that the file size had increased by over 500 kB.

    Five hundred kilobytes! What on earth..

    Turns out the moment I imported a MomentJS locale file into one of my files, for some bizzare reason webpack decided to include the entire MomentJS library, AND ALL THE LOCALE FILES.

    Read more »