RequireJS:多个main.js? [英] RequireJS: Multiple main.js?

查看:148
本文介绍了RequireJS:多个main.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用requireJS构建一个单页应用程序,到目前为止,我一直很喜欢.我已经说过要在主应用程序之外开发网站的其他部分,并且不确定如何(或是否)为此使用requireJS.

I have been building a single page app using requireJS and so far loving it. I have come to the point of developing other parts of the site outside of the main app and am not really sure how (or if) to use requireJS for this.

在我的主应用程序中,所有操作均由以下脚本标记触发:

In my main app everything is triggered by this script tag:

<script data-main='/scripts/main' src='/scripts/libs/require.js'>

我现在正在开发具有自己的前端脚本的主页.在使网站上线时使用优化器,该优化器会将所有这些脚本捆绑到一个main.js程序包中.我正在努力了解我网站其余部分的位置.

I am now developing the home page which has it's own front end scripts. Using the optimizer when it comes to getting the site live which will bundle all these scripts into one main.js package. I am struggling to understand where the rest of my site fits into this.

说我的应用程序依赖于jQuery,并且捆绑在应用程序的优化版本中,如果我想在主页上使用jQuery怎么办?我不想加载我的应用程序的main.js只是为了访问我的jQuery模块. 是的,有点困惑!

Say my app is dependent on jQuery and it gets bundled up in the optimized version of the app, what if I want to use jQuery on the homepage? I don't want to load in my app's main.js just to get access to my jQuery module. So yeah... a little confused!


我正在想象这样的网站结构:

/scripts
  - app-main.js //(includes all module dependencies after optimzation)
  - home-main.js //(includes all module dependencies after optimzation)

应用程序:

<script data-main='/scripts/app-main' src='/scripts/libs/require.js'>

主页:

<script data-main='/scripts/home-main' src='/scripts/libs/require.js'>


问题

  1. 如何使用RequireJS开发网站的不同部分?
  2. 是否建议有多个main.js文件?
  3. 我的不同main.js文件如何共享jQuery后优化之类的通用模块?
  1. How can I use RequireJS to develop different parts of a site?
  2. Is it recommended to have multiple main.js files?
  3. How can my different main.js files share common modules such as jQuery post optimization?

推荐答案

因此,应始终在任何页面上使用require.js,以实现模块化和干净的名称空间.我相信每个应用程序"都需要它自己的main.js脚本.优化网站时,r.js允许您从编译中排除模块,而您应该始终对jQuery执行此操作.

So require.js should always be used on any page to allow for modularity and a clean namespace. I believe each 'app' needs it's own main.js script. When optimizing your site r.js allows for you to exclude modules from the compilation which you should do for jQuery always.

这样,require.js总是会在大部分时间动态地从缓存中加载jquery.js.在您的应用和首页之间查找可能会缓存的其他模块,必须由您自行决定,并且取决于用户流量和其他因素.

That way require.js will always load jquery.js on the fly and most of the time from the cache. Finding other modules that might be cached between your app and homepage will have to be done at your own discretion and depends on the flow of your users and other factors.

听起来您有两个项目,一个应用程序和一个营销站点.我认为应该将它们分开,并应该有各自包含自己main.js的"js"文件夹.

It sounds like you have two projects, an app and a marketing site. I believe those should be separated to quite an extend and should have their own respective 'js' folders containing their own main.js.

这篇关于RequireJS:多个main.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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