In my current project we need a “stripped down” version of a particular JS which we can execute on a page that isn’t part of the rest of our website. We want things like navbar functionality on that page, but we don’t need or want the rest of our do-everything.js
file running since the page would be missing many elements that that file is expecting to exist.
I thought this would be a perfect use-case for esbuild as I could simply export the functions I wanted and then create a new JS file that simply imports the functions I actually want and leaves the rest. (Let’s call that exported.js
.) However, while it seemed like this was working initially, I’ve recently started getting errors from exported.js
and upon further investigation, it turns out the file contains basically the entirety of the do-everything.js
file, including the functions that that file imports from elsewhere.
In order to check that I wasn’t causing this with my own screwup I ran a test by changing the export in do-everything.js
to the following:
function test() {
console.log("Hello world");
}
export {test};
And then my exports.js
file had the following:
import {test} from "./global";
test();
and of course I have a pretty standard usage on the Hugo side:
{{- $exportsJS := resources.Get "js/exports.js" -}}
{{ $outputFile := $exportsJS | js.Build | fingerprint }}
<script src="{{ $outputFile.Permalink }}" defer></script>
After running hugo --ignoreCache
I’d expect to find an exports.js
file in my build folder that consisted of basically just one function. Instead, I find that exports.js
contains over 600 lines of code, which I assume is basically the entirety of my 900+ line do-everything.js
file with the comments and various linebreaks stripped out.
So either I’ve misunderstood how esbuild works with Hugo or this is a pretty gigantic bug that has completely broken treeshaking. (I should note that according to the esbuild docs, treeshaking can’t even be turned off, so this couldn’t simply be an issue of bad settings.)
I wanted to check in here before I went and posted this to issues as a bug, just in case there is something I’m missing here. Thanks for any help y’all may be able to provide.
Notes on my setup:
hugo v0.82.0-9D960784+extended windows/amd64 BuildDate=2021-03-21T17:28:04Z VendorInfo=gohugoio