管理和部署大型JavaScript应用程序的最佳做法 [英] Best practices for managing and deploying large JavaScript apps

查看:116
本文介绍了管理和部署大型JavaScript应用程序的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

管理中大型JavaScript应用程序的一些标准做法是什么?我的关注是浏览器下载的速度和开发的易用性和可维护性。

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 development.

我们的JavaScript代码大致为命名空间:

Our JavaScript code is roughly "namespaced" as:

var Client = {
   var1: '',
   var2: '',

   accounts: {
      /* 100's of functions and variables */
   },

   orders: {
      /* 100's of functions and variables and subsections */
   }

   /* etc, etc  for a couple hundred kb */
}

目前,我们有一个(unpacked,unpripped,高度可读)的JavaScript文件来处理Web应用程序的所有业务逻辑。另外还有jQuery和几个jQuery扩展。我们面临的问题是,永远需要在JavaScript代码中查找任何内容,浏览器仍然有十几个要下载的文件。

At the moment, we have one (unpacked, unstripped, highly readable) JavaScript file to handle all the business logic on the web application. In addition, there is jQuery and several jQuery extensions. The problem we face is that it takes forever to find anything in the JavaScript code and the browser still has a dozen files to download.

是通常有几个源JavaScript文件被编译成一个最终的压缩JavaScript文件?任何其他方便的提示或最佳做法?

Is it common to have a handful of "source" JavaScript files that gets "compiled" into one final, compressed JavaScript file? Any other handy hints or best practices?

推荐答案

我发现为我工作的方法是为每个单独的JS文件类(就像你在Java,C#和其他)。或者,您可以将JS分组到应用程序功能区域,如果您更容易导航。

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.

如果将所有JS文件放入一个目录,可以让您的服务器 - 侧面环境(例如PHP)循环遍历该目录中的每个文件,并输出一个< script src ='/ path / to / js / $ file.js'type ='text / javascript'>某些头文件中包含所有UI页面的。如果您经常创建和删除JS文件,您会发现这种自动加载特别方便。

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.

当部署到生产时,应该有一个脚本将它们全部结合转换成一个JS文件,并将其缩小以保持大小。

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.

这篇关于管理和部署大型JavaScript应用程序的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆