如何轻松维护跨文件的 JavaScript 库开发环境

发布于 2024-09-03 02:44:06 字数 1942 浏览 3 评论 0原文

我一直在开发一个新的 JavaScript 应用程序,该应用程序的规模正在迅速增长。

我的整个 JavaScript 应用程序已封装在单个函数、单个文件中,方式如下:

(function(){  
   var uniqueApplication = window.uniqueApplication = function(opts){
      if (opts.featureOne)
      {
         this.featureOne = new featureOne(opts.featureOne);
      }
      if (opts.featureTwo)
      {
         this.featureTwo = new featureTwo(opts.featureTwo);
      }
      if (opts.featureThree)
      {
         this.featureThree = new featureThree(opts.featureThree);
      }
   };

   var featureOne = function(options)
   {
       this.options = options;
   };
   featureOne.prototype.myFeatureBehavior = function()
   {
       //Lots of Behaviors
   };

   var featureTwo = function(options)
   {
       this.options = options;
   };
   featureTwo.prototype.myFeatureBehavior = function()
   {
       //Lots of Behaviors
   };

   var featureThree = function(options)
   {
       this.options = options;
   };
   featureThree.prototype.myFeatureBehavior = function()
   {
       //Lots of Behaviors
   };
})();

在匿名函数和执行之后的同一个文件中,我执行如下操作:

(function(){
   var instanceOfApplication = new uniqueApplication({
       featureOne:"dataSource",
       featureTwo:"drawingCanvas",
       featureThree:3540
   });
 })();

在在线上传此软件之前,我传递了我的 JavaScript 文件,及其所有依赖项,放入 Google Closure 编译器中,仅使用默认压缩,然后我就有了一个漂亮的 JavaScript 文件,可以在线进行生产。

这项技术对我来说效果非常好 - 因为它只在 DOM 中创建了一个全局足迹,并且为我提供了一个非常灵活的框架来扩展应用程序的每个附加功能。然而,我已经达到了这样的地步:我真的不想将整个应用程序保存在一个 JavaScript 文件中。

我希望从开发期间拥有一个大型 uniqueApplication.js 文件转变为为应用程序中的每个功能拥有一个单独的文件,featureOne.js - featureTwo.js - featureThree.js

一旦我完成了离线开发测试,我就会喜欢使用某种东西,也许是 Google Closure Compiler,将所有这些文件组合在一起 - 但是我希望这些文件都在该范围内编译,就像我将它们放在一个文件中一样 - 我希望它们能够离线测试期间也保持在相同范围内。

我看到 Google Closure Compiler 支持传入模块的参数,但我还没有真正找到有关执行此类操作的大量信息。

任何人都知道如何实现这一点 - 或者对跨多个文件编写单个 JavaScript 库但仍然只在 DOM 上留下一个足迹的开发实践有任何建议吗?

I have been developing a new JavaScript application which is rapidly growing in size.

My entire JavaScript Application has been encapsulated inside a single function, in a single file, in a way like this:

(function(){  
   var uniqueApplication = window.uniqueApplication = function(opts){
      if (opts.featureOne)
      {
         this.featureOne = new featureOne(opts.featureOne);
      }
      if (opts.featureTwo)
      {
         this.featureTwo = new featureTwo(opts.featureTwo);
      }
      if (opts.featureThree)
      {
         this.featureThree = new featureThree(opts.featureThree);
      }
   };

   var featureOne = function(options)
   {
       this.options = options;
   };
   featureOne.prototype.myFeatureBehavior = function()
   {
       //Lots of Behaviors
   };

   var featureTwo = function(options)
   {
       this.options = options;
   };
   featureTwo.prototype.myFeatureBehavior = function()
   {
       //Lots of Behaviors
   };

   var featureThree = function(options)
   {
       this.options = options;
   };
   featureThree.prototype.myFeatureBehavior = function()
   {
       //Lots of Behaviors
   };
})();

In the same file after the anonymous function and execution I do something like this:

(function(){
   var instanceOfApplication = new uniqueApplication({
       featureOne:"dataSource",
       featureTwo:"drawingCanvas",
       featureThree:3540
   });
 })();

Before uploading this software online I pass my JavaScript file, and all it's dependencies, into Google Closure Compiler, using just the default Compression, and then I have one nice JavaScript file ready to go online for production.

This technique has worked marvelously for me - as it has created only one global footprint in the DOM and has given me a very flexible framework to grow each additional feature of the application. However - I am reaching the point where I'd really rather not keep this entire application inside one JavaScript file.

I'd like to move from having one large uniqueApplication.js file during development to having a separate file for each feature in the application, featureOne.js - featureTwo.js - featureThree.js

Once I have completed offline development testing, I would then like to use something, perhaps Google Closure Compiler, to combine all of these files together - however I want these files to all be compiled inside of that scope, as they are when I have them inside one file - and I would like for them to remain in the same scope during offline testing too.

I see that Google Closure Compiler supports an argument for passing in modules but I haven't really been able to find a whole lot of information on doing something like this.

Anybody have any idea how this could be accomplished - or any suggestions on a development practice for writing a single JavaScript Library across multiple files that still only leaves one footprint on the DOM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东走西顾 2024-09-10 02:44:06

jQuery github 具有与您所说的类似的设置。甚至还有一个 Makefile / ant build.xml 使用谷歌闭包编译器。

基本概念是在单独的文件中开发所有内容,然后使用cat(或类似的东西)将所有文件放在一起。

 cat intro.js core.js featureOne.js featureTwo.js featureThree.js outro.js > build/script.js

来自 jQuery 的 intro.jsoutro.js 内的代码:

 // intro.js
 (function(window, undefined) {

 // outro.js
 })(window);

The jQuery github has a similar setup to the one you speak of. There is even a Makefile / ant build.xml that use the google closure complier.

The basic concept is to develop all your stuff in separate files, then use cat (or something similar) to put all the files together.

 cat intro.js core.js featureOne.js featureTwo.js featureThree.js outro.js > build/script.js

The code inside intro.js and outro.js from jQuery:

 // intro.js
 (function(window, undefined) {

 // outro.js
 })(window);
浅唱々樱花落 2024-09-10 02:44:06

看看这个库是如何构建的
http://github.com/oyvindkinsey/easyXDM

文件是分开的,但是合并在一起,放入闭包,并通过 ant 脚本 (build.xml) 运行 jslint

ant 脚本还执行条件“编译”、字符串替换和缩小。

Take a look at how this library is built
http://github.com/oyvindkinsey/easyXDM

The files are separated, but merged together, placed into a closure, and run through jslint by the ant script (build.xml).

The ant script also does conditional 'compilation', string replacements and minification.

挥剑断情 2024-09-10 02:44:06

我建议您将代码库拆分为 AMD/RequireJS 样式的模块。

AMD 格式似乎可以满足您的大部分要求,并且正在迅速成为事实上的标准。

I recommend that you split your code base into AMD/RequireJS-style modules.

The AMD format seems to meet most of your requirements, and is rapidly becoming a de facto standard.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文