简体   繁体   English

需要还是不要求?

[英]To require or not to require?

So I've been building a single page web app for a while now and I've been writing all my code in an extremely modular approach. 因此,我已经建立了一个页面的Web应用程序已有一段时间了,并且我已经以一种非常模块化的方式编写了所有代码。 I've been using the javascript module pattern for all modules and my main API uses the revealing module pattern to expose a small API for plugins and other modules. 我一直在对所有模块使用javascript模块模式,而我的主要API使用显示模块模式来公开用于插件和其他模块的小型API。

So even if I've been writing my code like this someone mentioned I should be using require.js as it gives a better modular approach. 因此,即使我一直在这样写代码,也有人提到我应该使用require.js,因为它提供了更好的模块化方法。

I decided that require.js doesn't really make it more modular so my next thought was how require.js separates out dependencies. 我认为require.js并没有真正使它更具模块化,因此我的下一个想法是require.js如何分离出依赖关系。 Require.js forces you to name dependencies in each module file. Require.js强制您在每个模块文件中命名依赖关系。 But this to me seems to be a task I have to do for every module and I have a lot of modules. 但这对我来说似乎是每个模块都要执行的任务,而且我有很多模块。 At the moment all my files are concatenated into a single javascript file in my grunt build process so really all my files are loaded at the start. 目前,在我的咕files构建过程中,我的所有文件都串联到一个javascript文件中,因此实际上我的所有文件都是在开始时加载的。 I need most of my modules loaded at the start so it made sense to do this. 我需要在开始时加载大多数模块,因此这样做很有意义。

So my question is... Should I use require.js even though my code is modular and all my files are concatenated together and loaded at the start? 所以我的问题是...即使我的代码是模块化的,并且我所有的文件都连接在一起并在一开始就加载,我仍应使用require.js吗? Do i need to worry about dependencies or loading order? 我是否需要担心依赖关系或加载顺序? Any help/advise or previous experience when dealing with this situation would really help. 处理这种情况时的任何帮助/建议或以前的经验都将真正有帮助。 Thanks 谢谢

  1. Have you had annoyances putting <script> tags in the correct order to handle dependencies right? 您是否有烦恼以正确的顺序放置<script>标记以正确处理依赖项?

    Having a requirejs config file where you declare the third-party code and your own code dependency tree is a much more organised approach than declaring <script> tags by yourself. 与您自己声明<script>标记相比,拥有一个requirejs配置文件来声明第三方代码和自己的代码依赖关系树是一种更有条理的方法。

  2. When you are testing your app in dev environment, wouldn't be helpful to have all those modules in separated files that are easier to debug instead of all of them concatenated? 在开发环境中测试应用程序时,将所有这些模块放在单独的文件中进行调试(而不是将它们全部串联在一起)是否会有所帮助?

    With requirejs you can switch between optimised/concatenated/minified code used in production environment and a list of independent files representing each module in the development environment. 使用requirejs,您可以在生产环境中使用的优化/链接/缩小代码与代表开发环境中每个模块的独立文件列表之间进行切换。

  3. Are you creating a global variable for each module in your app? 您是否正在为应用程序中的每个模块创建全局变量?

    Requirejs avoids creating a global variable for each module so your global scope doesn't get cluttered. Requirejs避免为每个模块创建一个全局变量,这样您的全局范围就不会混乱。

  4. It's usually a good practice to follow conventions in the way you declare modules. 在声明模块时遵循惯例通常是一个好习惯。 Requirejs implements AMD specification that is a well thought way of loading modules in client javascript. Requirejs实现了AMD规范,这是在客户端javascript中加载模块的一种深思熟虑的方式。

    http://requirejs.org/docs/whyamd.html http://requirejs.org/docs/whyamd.html

    It's way easier to follow conventions if they're already implemented in a mature framework. 如果约定已经在成熟的框架中实现,则遵循约定更容易。 And sometimes we don't notice but between apps we do the same thing differently and that affects in the maintenance phase. 有时我们不会注意到,但在应用程序之间,我们做相同的事情的方式有所不同,这会影响维护阶段。

  5. Take a look at the requirejs optimizer. 看一下requirejs优化器。 Look all the options it give you. 查看它给您的所有选项。 It allows you to exclude files, to change versions of a given module, to change the minification tool, to integrate the optimization process with other tools like grunt or bower , etc. 它允许您排除文件,更改给定模块的版本,更改缩小工具,将优化过程与gruntbower等其他工具集成。

http://requirejs.org/docs/optimization.html http://requirejs.org/docs/optimization.html

If you don't need any of this, then just continue doing it in your way. 如果您不需要任何这些,请继续以您的方式进行。

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

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