Best practices for managing and deploying large JavaScript apps

后端 未结 7 1772
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 14:38

What are some standard practices for managing a medium-large JavaScript application? My concerns are both speed for browser download and ease and maintainability of developm

相关标签:
7条回答
  • 2020-12-04 15:10

    Just a sidenode - Steve already pointed out, you should really "minify" your JS files. In JS, whitespaces actually matter. If you have thousand lines of JS and you strip only the unrequired newlines you have already saved about 1K. I think you get the point.

    There are tools, for this job. And you should never modify the "minified"/stripped/obfuscated JS by hand! Never!

    0 讨论(0)
  • 2020-12-04 15:17

    Read the code of other (good) javascript apps and see how they handle things. But I start out with a file per class. But once its ready for production, I would combine the files into one large file and minify.

    The only reason, I would not combine the files, is if I didn't need all the files on all the pages.

    0 讨论(0)
  • 2020-12-04 15:20

    My strategy consist of 2 major techniques: AMD modules (to avoid dozens of script tags) and the Module pattern (to avoid tightly coupling of the parts of your application)

    AMD Modules: very straight forward, see here: http://requirejs.org/docs/api.html also it's able to package all the parts of your app into one minified JS file: http://requirejs.org/docs/optimization.html

    Module Pattern: i used this Library: https://github.com/flosse/scaleApp you asking now what is this ? more infos here: http://www.youtube.com/watch?v=7BGvy-S-Iag

    0 讨论(0)
  • 2020-12-04 15:21

    Also, I suggest you to use Google's AJAX Libraries API in order to load external libraries.

    It's a Google developer tool which bundle majors JavaScript libraries and make it easier to deploy, upgrade and make them lighter by always using compressed versions.

    Also, it make your project simpler and lighter because you don't need to download, copy and maintain theses libraries files in your project.

    Use it this way :

    google.load("jquery", "1.2.3");
    google.load("jqueryui", "1.5.2");
    google.load("prototype", "1.6");
    google.load("scriptaculous", "1.8.1");
    google.load("mootools", "1.11");
    google.load("dojo", "1.1.1");
    
    0 讨论(0)
  • 2020-12-04 15:26

    The approach that I've found works for me is having seperate JS files for each class (just as you would in Java, C# and others). Alternatively you can group your JS into application functional areas if that's easier for you to navigate.

    If you put all your JS files into one directory, you can have your server-side environment (PHP for instance) loop through each file in that directory and output a <script src='/path/to/js/$file.js' type='text/javascript'> in some header file that is included by all your UI pages. You'll find this auto-loading especially handy if you're regularly creating and removing JS files.

    When deploying to production, you should have a script that combines them all into one JS file and "minifies" it to keep the size down.

    0 讨论(0)
  • 2020-12-04 15:28

    For server efficiency's sake, it is best to combine all of your javascript into one minified file.

    Determine the order in which code is required and then place the minified code in the order it is required in a single file.

    The key is to reduce the number of requests required to load your page, which is why you should have all javascript in a single file for production.

    I'd recommend keeping files split up for development and then create a build script to combine/compile everything.

    Also, as a good rule of thumb, make sure you include your JavaScript toward the end of your page. If JavaScript is included in the header (or anywhere early in the page), it will stop all other requests from being made until it is loaded, even if pipelining is turned on. If it is at the end of the page, you won't have this problem.

    0 讨论(0)
提交回复
热议问题