It’s a very common practice to load external libraries using CDN rather than importing all packages in a single JS file, making it bulky. To do the same with Hugo, you’ll need to shim the libraries as follows. In this example, algoliasearch and instantsearch.js will be shimmed.
Firstly, add the following to your project’s package.json:
What this does is it tells Hugo to look for the listed packages somewhere else. Here we’re telling Hugo to look for algoliasearch/lite and instantsearch.js/es/lib/main in the project’s public/js/shims folder.
Now we’ll need to create the shim JS files which export the global JS variables module.exports = window.something. You can create a separate shim JS file in your assets directory, and redirect the import paths there if you wish, but a much cleaner way is to create these files on the go, by having the following before your JS is built.