What is the best method to load JS files?

Asked

Viewed 428 times

1

I try to work with the code in the way that gives me the greatest possible use of what was written, with this, for every feature that I will need to do in javascript/jQuery I control the individual version of each thing with git.

Controlling this individual version, it happens that I end up having many files to be imported on system loading, my upload has a list like this (well summarized):

Masker-0.0.5.js

navigation-0.0.1.js

Validator-0.2.1.js

Tiles-effects-1.0.0.js

Tiles-navigation-1.0.0.js

Including external uploads, and internal third-party uploads, the list is really giant, because only the features related to Bootstrap CSS already give ~30 file-only inclusions. js, not to mention the style sheets.

Going back to my individual files, they are relatively small, averaging 3k of characters.

My current upload in development environment is done with a PHP class:

    class local__jsPackages
    {

        public static function get_packages()
        {
            $files = '';
            $packages = self::packages();
            foreach ($packages as $import)
            {
                $path = $import['path'];
                $file = $import['file'];
                $version = $import['version'];
                $files .= '<script src="' . $path . '/' . $file . '' . $version . '.js"></script>';
            }
            return $files;
        }

    private static function packages()
        {

            $packages = [
                //Plugin Bootstrap
                '0' => [
                    'path' => '../theme/admin/assets/global/plugins/bootstrap/js',
                    'file' => 'bootstrap',
                    'version' => (string) '.min'
                ]
                //Dentro da array seguem todas as inclusões locais
           ];
           return $packages;
     }
}

And when it’s passed to production, I manually group everything into a single giant file, that’s the part of the problem process.

What is the best way to LOAD many javascript files/methods/plugins ?

PS: They should be loaded all at once, as the application should not allow the user to update the page without scrolling.

  • 1

    'The application must not allow the user to update the page without displaying', ?

  • The 'F5' button has been disabled, when the user presses 'F5' the system asks to be logged out, and if it updates by the browser has to log in again.

  • I get it, let’s let someone else answer more precisely, but in my opinion, front-end loading on the server was a very used method when javascript was not yet so widespread, today there is no reason to do this, probably you have some language addiction (in the case PHP) and ends up using the same, for other purposes.

  • 1

    I really have this 'problem' with PHP, I hate to program front-end, and I end up generating a simple 'button' with a PHP class, this is one of the reasons why I don’t know how to operate very well with JS.

  • Normal kk, server front-end approach today only in frameworks like ruby on Rails and other derivatives, in Rails for example you generate a crud with the whole front-end schema in minutes, without needing to put your hand in html, only in css to give a 'color' dps, by the way today there are already PHP frameworks that match Rails, I can’t tell you which, but I heard this from the masters https://hipsters.tech/a-vez-do-ruby-on-rails-hipsters-52/

1 answer

1


Anthraxisbr, I will leave here my suggestion here as an answer to get better to read, the comments is very limited

Grunt makes use of JS and CSS files in addition to automatically minifying them.

For example:

In my wife’s blog, I use Grunt to concatenate and minify 3 files

'js/jquery.js',
'js/what-input.js',
'js/app.js'

So the uglfy of my Gruntfile.js is like this:

uglify: {
        dist: {
            options: {
                expand: true,
            },
        files: {
                'js/app.min.js': [
                    'js/jquery.js',
                    'js/what-input.js',
                     'js/app.js'
                ],
                }
            },
        },

Soon, the 3 files will be concatenated and minified in a single file: app.min.js.

but if I want to run it without knowing the file name?

Very easy, inside your files: {} would look like this:

'js/app.min.js': [
    'js/*.js //vai pegar tudo que tem extensão .JS
 ],

And if I don’t want to run a single file?

Just add an exclamation in front of the file path so:

'js/app.min.js': [
   '!js/nomearquivo.js // vai ignorar esse cara na concatenação e minificação
],

I usually create a PHP function to control the version

function get_file_version(){
 echo '20170801';
}

and finally, I call the html file as follows:

<script src="js/app.min.js?version=<?php get_file_version(); ?>"></script>

Because?

Because the browser caches the file and every time the file is modified. JS is necessary for the browser to re-read the file for the changes to take effect. Every time Voce changes the value, Voce forces the browser to read the file.

https://gruntjs.com/getting-started

I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.