首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >对于包含CDN托管库(jQuery)的多页项目,RequireJS不起作用。

对于包含CDN托管库(jQuery)的多页项目,RequireJS不起作用。
EN

Stack Overflow用户
提问于 2013-08-10 05:31:42
回答 1查看 3.9K关注 0票数 16

我在一个多页面项目中使用了RequireJS,它的Javascript文件夹结构看起来像这样(如何在Markdown中再次创建那些花哨的目录树?):

代码语言:javascript
复制
common.js
lib/
-- jquery-1.9.1.min.js
-- modernizr-2.6.2.min.js
-- underscore-amd.min.js
page/
-- index.js
-- start.js
-- checkout.js

无论如何,common.js是我的主脚本文件,我在其中设置配置参数。这看起来是这样的:

common.js文件

代码语言:javascript
复制
// Configure RequireJS
requirejs.config({
    baseUrl: "assets/js/lib",
    paths: {
        page: '../page',
        jquery: [
            '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min',
            //If the CDN location fails, load from this location
            'jquery-1.9.1.min'
        ],
        modernizr: [
            '//cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min',
            //If the CDN location fails, load from this location
            'modernizr-2.6.2.min'
        ],
        underscore: [
            'underscore-amd.min'
        ]
    }
});

所有的页面调用都像预期的那样工作( CDN位置被加载),但我无法理解缩小部分。r.js优化器拒绝合作,抛出一个Error: paths fallback not supported in optimizer. Please provide a build config path override for underscore,即使下划线没有指定CDN。

build.js文件

代码语言:javascript
复制
{
    appDir: '../www',
    baseUrl: 'js/lib',
    dir: '../www-built',
    mainConfigFile: '../www/assets/js/common.js',
    modules: [
        //First set up the common build layer.
        {
            //module names are relative to baseUrl
            name: '../common',
            include: ['jquery',
                      'modernizr',
                      'underscore'
            ]
        },
        // Now the page scripts
        {
            //module names are relative to baseUrl/paths config
            name: '../page-index',
            include: ['page/index'],
            exclude: ['../common']
        },
    ],
    paths: {
        jquery: "empty",
        modernizr: "empty"
    },
    optimize: "uglify2",
    removeCombined: true
}

请帮我弄清楚如何建立这个项目,以创建common.js脚本与个人页面脚本。

(注意:我的build.js文件的结构基于this example

更新!

我已经更新了问题以包含empty:的正确语法(感谢Travis!),现在构建运行没有错误。但是,我的JS文件没有连接或丑化。CSS文件在导入点,所以发生了一些事情。下面是完整的build.js文件(请原谅,她个子很高):

代码语言:javascript
复制
{
    appDir: '../www',
    baseUrl: 'assets/js', // relative to appDir
    dir: '../www-built',
    mainConfigFile: '../www/assets/js/common.js',
    modules: [
        //First set up the common build layer.
        {
            //module names are relative to baseUrl
            name: 'common',
            //List common dependencies here. Only need to list
            //top level dependencies, "include" will find
            //nested dependencies.
            include: ['jquery',
                      'modernizr',
                      'underscore',
                      'bootstrap'
            ]
        },

        //Now set up a build layer for each page, but exclude
        //the common one. "exclude" will exclude nested
        //the nested, built dependencies from "common". Any
        //"exclude" that includes built modules should be
        //listed before the build layer that wants to exclude it.
        //"include" the appropriate "app/main*" module since by default
        //it will not get added to the build since it is loaded by a nested
        //require in the page*.js files.
        {
            //module names are relative to baseUrl/paths config
            name: 'pages/home',
            include: ['pages/home'],
            exclude: ['common']
        },
        {
            //module names are relative to baseUrl/paths config
            name: 'pages/start',
            include: ['pages/start'],
            exclude: ['common']
        },
        {
            //module names are relative to baseUrl/paths config
            name: 'pages/checkout',
            include: ['pages/checkout'],
            exclude: ['common']
        },
    ],
    paths: {
        jquery: "empty:",
        modernizr: "empty:"
//        underscore: "empty:"
    },
    optimize: "uglify2",
    optimizeCss: "standard",
    removeCombined: true,
    preserveLicenseComments: false
}

最终更新(Yaay!它起作用了)

多亏了下面的Travis,一切都运行得很好!(文件正在被缩小和连接)。由于他的解决方案托管在Dropbox上,并且将来可能会丢失(谁知道amirite呢?),我将只总结他所做的修复:

1.不要在一个文件中混用definerequire调用。

我有几个文件,我把它们混合成这样:

page/start.js

代码语言:javascript
复制
 define(['jquery','underscore'],function($,_) {
      ...
 });

 require(['jquery','page/start'], function($, Y) { // BAD!
      ...
 });

解决方法是这样做:

page/start.js

代码语言:javascript
复制
 require(['jquery','app/start_helper'], function($, Y) {
      ...
 });

app/start_helper.js

代码语言:javascript
复制
 define(['jquery','underscore'],function($,_) {
      ...
 });

2.不是"empty",而是"empty:"

这是一个棘手的问题,尽管RequireJS文档中提到了它们。

3.因为

什么样的列表只有两个要点?

Awesomelicious现在它像一个护身符一样工作:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-16 19:17:33

由于您将其路径值指定为一个数组,因此看起来要求as认为您正在尝试为下划线提供一个路径后备。试着把它当作字符串来做。

代码语言:javascript
复制
paths: {
    underscore: 'underscore-amd.min'
}

还要注意,correct symbol for empty pathsempty: (带有':'),而不是empty

最后,作为一个不相关的附注,您可以随时查看lodash,它是一种更可定制的、跨浏览器兼容的、更快的下划线替换,具有相同的API,它托管在cdnjs上,符合AMD标准。

更新

根据评论中的对话,以下是我对您的项目的更新版本:https://dl.dropboxusercontent.com/u/21823728/so_rjs.tar.gz

正如评论中提到的,您的问题似乎是在需要这些模块的同一个文件中define模块,我认为这导致r.js认为这些模块没有主入口点。约定是将模块定义从需要这些模块的文件中分离出来。

请注意,现在在assets/js下有一个app文件夹,其中存储了页面模块中以前是defined的每个模块。您的页面模块现在可以require这些模块了。当我重新运行r.js (使用uglify2作为优化器)时,一切似乎都像预期的那样工作。

此外,我从build.js文件中删除了一些冗余(如果构建配置也使用baseUrl,则只需在mainConfigFile中指定它)。最后,如果您希望将jQuery和Bootstrap全局附加到每个页面,则可能需要考虑使用shim config。否则,当您需要它们时,您应该显式地将它们作为依赖项列出。

最后,作为最后一条建议,您可以考虑减少每个文件中的require数量。对于这些页面中的大多数,您可以将所有内容包装在一个请求调用中,然后将不同的逻辑分离到函数中,以节省事件队列上的一些空间,还可以节省一些必须冗余地调用require函数的周期。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18155968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档