简体   繁体   English

带有本地Javascript文件的Cordova应用程序-许多文件还是一个?

[英]Cordova application with local Javascript files - many files or just one?

I am building a Cordova mobile application, and the custom scripts I am making are all locally inside the application package, and I am concerned about performance. 我正在构建一个Cordova移动应用程序,而我正在制作的自定义脚本都在应用程序包内部,并且我担心性能。 Is it ok for the files to be many, like each big namespace variable to be in a separate file, or should I put them all in one file? 文件是否可以很多(例如每个大命名空间变量都放在一个单独的文件中)还是应该将它们全部放在一个文件中?

Like I said, all of these files are going to be presented locally and not on a server, and I am concerned about the performance. 就像我说的那样,所有这些文件都将在本地而不是在服务器上显示,并且我担心性能。

Don't optimise for performance without first showing there is a performance problem and identifying exactly where the problem lies. 在未首先显示性能问题并准确确定问题所在的情况下,请勿对性能进行优化。 Code for maintainability first. 首先编写可维护性代码。 I'd keep your JavaScript files separate. 我会将您的JavaScript文件分开保存。

Your app should have some sort of build process. 您的应用程序应具有某种构建过程。 There are lots of tools that help automate this process ( gulp , grunt , etc.) but you can get by with simple npm scripts as well. 有很多工具可以帮助实现此过程的自动化( gulpgrunt等),但是您也可以使用简单的npm脚本来获得帮助。 How you create a build process is not the point here. 如何创建构建过程不是这里的重点。 The benefits, however, are many: 但是,好处很多:

  1. You can develop your app using multiple small files. 您可以使用多个小文件来开发应用程序。 If you have good organization and consistent naming practices, these files will be easier to maintain than one large file. 如果您具有良好的组织结构和一致的命名习惯,则这些文件将比一个大文件更易于维护。

  2. You can build appropriate debug and release versions of your app from those files. 您可以从这些文件中构建相应的应用调试和发行版本。

    a. 一种。 If you want the files to be bundled into one large file, you can use a packer like Browserify , Webpack or JSPM . 如果要将文件捆绑到一个大文件中,可以使用诸如BrowserifyWebpackJSPM之类的打包器。 Then, depending on the options you pass, your debug version can include the necessary sourcemaps to make debugging easy. 然后,根据您通过的选项,您的调试版本可以包含必要的源映射,以简化调试。

    b. You might also want to use other tools, such as a minifier for your JS code, or image optimizer for your icons. 您可能还需要使用其他工具,例如用于JS代码的压缩程序或用于图标的图像优化程序。

  3. You can use the well-established module patterns to create modular code that doesn't pollute the global namespace (if you use something like Browserify, JSPM, etc.). 您可以使用完善的模块模式来创建不会污染全局名称空间的模块化代码(如果使用的是Browserify,JSPM等)。 This means you can do something like this: 这意味着您可以执行以下操作:

     // file: say.js module.exports = { hello: function hello() { alert("hello"); }, goodbye: ..., ... }; // file: index.js var say = require("./say.js"); say.hello(); say.goodbye(); 

Do none of the above because it will be "faster", though, because the difference when loading a single file vs loading multiple files is almost certainly negligible and on the order of milliseconds. 不过,不要执行上述任何一项操作,因为它会“更快”,因为加载单个文件与加载多个文件时的差异几乎可以忽略不计,并且以毫秒为单位。 Your user simply will not notice unless you've got a huge number of files. 除非您拥有大量文件,否则用户根本不会注意到。 And should it become an issue, it's easy to make your build process generate a release version with everything bundled up into one large file without sacrificing maintainability. 而且,如果这成为一个问题,很容易使您的构建过程生成一个发行版本,并将所有内容捆绑到一个大文件中,而不会牺牲可维护性。

Do do the above to make your workflow easier. 不要上面,使您的工作更容易。 Editing a single file with a hundred thousand lines is no fun -- you don't want to do that, trust me. 编辑一个有十万行的文件很无聊-不想这样做,相信我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM