简体   繁体   English

包含Javascript和串联的最佳实践

[英]Best Practice for Including Javascript and Concatenate

I have a medium sized project and I want to divide my fairly long javascript file into several submodules. 我有一个中等大小的项目,我想将我相当长的javascript文件分成几个子模块。 Afterwards I want to concatenate all my js files with gulp.js and also minify them. 之后,我想将我的所有js文件与gulp.js连接起来,并缩小它们。

I also would prefer not to use a watch task to concatenating my js every time I edit it, but rather use the separated js modules during development and for deployment I'd like to run my gulp concat task. 我也不想在每次编辑它时都不要使用监视任务来连接我的js,而是在开发和部署期间使用分离的js模块,以便运行gulp concat任务。 ( https://www.npmjs.com/package/gulp-concat ) https://www.npmjs.com/package/gulp-concat

My setup for my question: 我的问题设置:

I have all my js embraced by a document.ready (I'm using jQuery). 我所有的js都被document.ready包围了(我正在使用jQuery)。 For simplicity let's say I have within this 3 functions, content does not matter. 为了简单起见,假设我在这3个函数中都具有内容,这无关紧要。 I would like to distribute those 3 functions to 3 separate .js files. 我想将这3个函数分发到3个单独的.js文件。

$(function() { 
    function myFunction1(){}
    function myFunction2(){}
    function myFunction3(){}
}

What I would like to have: 我想要的是:

main.js: main.js:

$(function() { 
    // load function1.js
    // load function2.js
    // load function3.js

}

and all the separate functionX.js 以及所有单独的functionX.js

By using gulp to concatenate I would like to end up with the code at the beginning (all in one file, and properly embraced by $(function() {} 通过使用gulp进行连接,我希望以开头的代码结尾(全部在一个文件中,并由$(function() {}

Now first, I don't know how to load the functions into this, so that during development I have full functional code by just including my main.js and without concatenating. 现在首先,我不知道如何将函数加载到其中,因此在开发过程中,仅包含main.js即可实现完整的功能代码,而无需进行串联。 Is this even possible? 这有可能吗?

I tried to use 我尝试使用

$.getScript("function1.js", function(){
});

but after concatenating – not to my surprise – this just remained like it was. 但是在连接后(并不令我感到惊讶),情况仍然如此。

So is there another way of achieving this? 那么还有另一种方法可以实现这一目标吗?

Or would a better option be to write 3 different functions.js, all with their own $(function() {}) and after concatenating I have that part 3 times? 还是更好的选择是编写3个不同的functions.js,它们都带有自己的$(function() {})并在连接之后得到3次? While I would just include all of the files during development in my html? 虽然我只是将开发过程中的所有文件都包含在html中?

It would be awesome to hear your thoughts about this, and how to work with js modules while not repeating any code. 听到您对此的想法,以及如何在不重复任何代码的情况下使用js模块,真是太棒了。

Thanks a lot. 非常感谢。

Ok, I just spoke with another developer and we mainly told me the following way is the way to go: 好的,我刚刚与另一位开发人员进行了交谈,我们主要告诉我以下方法是可行的:

  • write different .js files with different modules (my own code separation for better maintenance) 用不同的模块编写不同的.js文件(我自己的代码分开以进行更好的维护)

  • all of them including their own immediately invokin function expression to prohibit scope problems and their document.ready function 它们都包括自己的立即invokin函数表达式以禁止范围问题及其document.ready函数

     (function ($) { // iief = Immediately-Invoked Function Expression, mainly useful to limit scope $(function() { // Shorthand for $( document ).ready() // whatever code }); }(jQuery)); 
  • then use eg gulp and write a watch task to concatenate on the fly after change 然后使用例如gulp并编写监视任务以在更改后立即进行连接

  • use the concat sourcemap function to still see the original files with the correct linenumbers etc. ( Sourcemaps ) 使用concat sourcemap函数仍可以查看具有正确行号等的原始文件。( Sourcemaps

  • include the all.js (concatenated one) 包括all.js(串联一个)

  • minify at the end for staging and deployment 在最后阶段进行最小化

  • and use if($('.selector').length){} to check if the code should be executed in dependency of selector existence 并使用if($('.selector').length){}检查是否应根据选择器的存在执行代码

So, that's the way I'm following right now. 所以,这就是我现在关注的方式。 I'd love to hear your thoughts upon that anyway. 无论如何,我很想听听您的想法。 Cheers 干杯

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

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