Monkeying Around with npm Packages

As a researcher in the field of artificial intelligence, I often find myself needing to modify existing npm packages to fit my specific needs. However, monkeypatching these packages can be a delicate task, as it can easily lead to unintended consequences and make the package unusable. In this blog post, I want to share a clever hack I used to disable gamepad support in the Babylon.js library, which is a popular JavaScript framework for building 3D experiences.

Background

———-

Babylon.js is a powerful tool for building 3D experiences on the web. However, its gamepad support is horribly broken, and I needed to disable it for my demo. Unfortunately, monkeypatching the library is not straightforward, as it can easily lead to unintended consequences and make the package unusable.

The Solution

————-

To disable gamepad support in Babylon.js, I used a tool called patch-package. Patch-package is a lovely little tool that enables you to generate patch files simply by editing a given npm package in-situ. It also automatically and transparently applies the generated patch files on npm install, requiring no additional setup steps.

Here’s how I used patch-package to disable gamepad support in Babylon.js:

1. Install patch-package:

“`bash

npm install patch-package

“`

2. Edit the Babylon.js package:

First, I had to switch from the main Babylon.js package to @babylon/core, which contains the source code. Unfortunately, the official documentation for Babylon.js is rather inconsistent, which can lead to confusion using the latter. However, once I figured out how the imports worked, it all came out in the wash.

Next, I directly edited the files associated with the target package in node_modules/. To do this, you need to open the package’s directory in your code editor and edit the files directly.

3. Generate the patch file:

Once you’ve made the desired changes to the package, generate the patch file like so:

“`bash

patch-package –package-name @babylon/core –write-patches

“`

This should create a patch file in the directory patches/ alongside your package.json file.

4. Apply the patch:

To apply the patch, open up your package.json file for editing and add the following to the scripts object:

“`json

“scripts”: {

“patch”: “patch-package –package-name @babylon/core”

}

“`

This will automatically apply the generated patch file on npm install.

5. Commit the changes:

Finally, commit your changes to your Git/Mercurial/whatever repository. It’s important to keep a record of your modifications, especially if you plan to reuse this hack in future projects.

Conclusion

———-

In this blog post, I shared a clever hack for disabling gamepad support in Babylon.js using patch-package. This technique can be useful when you need to modify existing npm packages without monkeypatching them. Just keep in mind that monkeypatching can still have unintended consequences, so always make sure you understand the risks before proceeding.

Leave a Reply