I’m having a problem when minifying javascript.
I have the following template code:
{{/* Bootstrap scripts */}}
{{ $jsData := resources.Get "js/bootstrap-5.0.0-beta1/dom/data.js" }}
{{ $jsEventHandler := resources.Get "js/bootstrap-5.0.0-beta1/dom/event-handler.js" }}
{{ $jsManipulator := resources.Get "js/bootstrap-5.0.0-beta1/dom/manipulator.js" }}
{{ $jsToast := resources.Get "js/bootstrap-5.0.0-beta1/toast.js" }}
{{ $jsSelectorEngine := resources.Get "js/bootstrap-5.0.0-beta1/dom/selector-engine.js" }}
{{ $jsCollapse := resources.Get "js/bootstrap-5.0.0-beta1/collapse.js" }}
{{/* Scripts */}}
{{ $jsCookieAlert := resources.Get "js/cookie-alert.js" }}
{{/* Bundle javascripts */}}
{{ $bundle := slice $jsData $jsEventHandler $jsManipulator $jsToast $jsSelectorEngine $jsCollapse $jsCookieAlert | resources.Concat "js/bundle.js" }}
{{ $optimizedBundle := $bundle | js.Build (dict "minify" true) | resources.Minify | fingerprint }}
<script defer src="{{ $optimizedBundle.RelPermalink}}"></script>
Inside the file js/cookie-alert.js I’m initializing my Toast for displaying a cookie alert.
// Initialize and visualize cookie alert
var toastElement = document.querySelector('#cookie-alert');
var toast = new Toast(toastElement, {"autohide": false});
toast.show();
When I run the command:
hugo server -D --disableFastRender
the console throws the following error:
Start building sites …
ERROR 2021/01/23 13:16:25 js.Build failed: Could not resolve "./dom/event-handler.js"
ERROR 2021/01/23 13:16:25 js.Build failed: Could not resolve "./dom/manipulator.js"
ERROR 2021/01/23 13:16:25 js.Build failed: Could not resolve "./dom/selector-engine.js"
Built in 1509 ms
Error: Error building site: JSBUILD: failed to transform "js/bundle.js" (application/javascript): Could not resolve "./dom/data.js"
But if I remove the js.Build and the resources.Minify from the optimized bundle variable and using it this way:
{{ $optimizedBundle := $bundle | fingerprint }}
everything works fine. The problem here is that I’m not minificating the bundle.
So, how can I minify my bundle? I’m not understanding where I’m doing wrong.
My configuration is:
baseURL = "https://example.com/"
languageCode = "en-EN"
title = "My New Hugo Site"
enableRobotsTXT = true
disableKinds = ["taxonomy", "term"]
assetDir = "resources/assets"
My javascript files are inside resources/assets/js.
Also, inside the assets folder, I have a file called jsconfig.json with the following content
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": [
"*"
]
}
}
}