Range over array for bundling assets


#1

Hello

I’m trying to bundle a bunch of smaller javascript files to one combined file, using the method specified here.

But i was wondering, that instead of having each file as a single variable, as it can be cumbersome to maintain in a growing platform. So what i tried to do, was make an array of the paths to the files, and then i would like to range over that array to create that part resources.Get "path" dynamically.

In my config.toml i have this array:

[params.js]
    node = [
        'jquery/dist/jquery.min.js',
        'jquery-validation/dist/jquery.validate.min.js'
    ]
    project = [
        'generic/global.js',
        'generic/parallax.js',
        'generic/format.js',
        'generic/intro.js',
        'navigation/smooth-scroll.js',
        'navigation/sticky-nav.js',
        'gallery/modal.js',
        'contact/google-maps.js',
        'contact/validation.js',
        'contact/send-mail.js'
    ]

And this is what i tried, but without luck in my layout file _scripts.html

{{ $scripts := slice range .Site.Params.js.project resources.Get . end | resources.Concat "dist/scripts.min.js" | resources.Minify }}
<script type="text/javascript" src="{{ $scripts.Permalink }}"></script>

I get the following error when doing a range like that: unexpected <range> in operand

Kind regards,
Dennis


#2

Hi,

I don’t think you can inline range like that. Instead:

{{ $js := slice }}
{{ range .Site.Params.js }}
  {{ $js = $js | append (resources.Get .) }}
{{ end }}
{{ $scripts := $js | resources.Concat "dist/scripts.min.js" }}
...

#3

Thank you so much for helping. It works perfectly!


#4

I’m just adding some more information to this thread that is very much related, if someone else stumbles upon it.

I was hoping that i could include files directly from my node_modules folder, unfortunately not.

In the meantime i’ve just included the files i needed in node_modules directly into my assets folder, and crossing my fingers that the above feature will be added at some point.