热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

RequireJs:模块路径在与r.js的构建中不存在错误。-RequireJs:modulepathdoesnotexisterrorinbuildwithr.js

ImmakinguseofRequireJs,andImhavingtroublewiththebuild.我正在使用RequireJs,而且我在构建过程中遇到了麻烦。

I'm making use of RequireJs, and I'm having trouble with the build.

我正在使用RequireJs,而且我在构建过程中遇到了麻烦。

My structure is

我的结构

webroot
  css
    /* css here */
  files
  img
  js
    emails
      verify.js
    lib
      jquery-1.8.3.min.js
      jquery-ui.min.js
      jquery.ui.selectmenu.js
      modernizr.js
      require.js
    orders
      form.js
    common.js
  js-build
    /* expected build output here */
  js-tools
    app.build.js

This is a part of a CakePHP project, but the webroot is where the actual webroot of the web server will be.

这是CakePHP项目的一部分,但是webroot是web服务器的实际webroot。

node and r.js.cmd are both on my path, so I haven't included it in the js-tools directory.

节点和r.js。cmd都在我的路径上,所以我没有将它包含在js-tools目录中。

When accessing the default page, the is /, but it could also appear as /orders/form. For this reason, relative Urls to the JS is an issue.

当访问默认页面时,是/,但也可以显示为/订单/表单。因此,对JS的相对url是一个问题。

When I load the JS, I'm using

当我加载JS时,我在使用。



This is taken from https://github.com/requirejs/example-multipage-shim.

这是取自https://github.com/requirejs/examplemultishim。

My common.js is

我常见。js是

requirejs.config({
    "baseUrl": "/js/lib",
    "paths": {
        "orders": "../orders",
        "emails": "../emails",
        "jquery": [
            "//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min",
            "jquery-1.8.3.min"
        ],
        "jquery-ui": [
            "//ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min",
            "jquery-ui.min"
        ]
    },
    "shim": {
        "jquery.ui.selectmenu": ["jquery", "jquery-ui"]
    }
});

As it stands, this works when working with unoptimized code. The important feature is that js is referenced with an absolute URL, is it can be picked up by the code from either /, or /orders/form.

正如它所表明的那样,当使用未优化的代码时,这是有效的。重要的特性是,js使用一个绝对的URL来引用,它可以由来自任何/或/订单/表单的代码来获取。

My app.build.js is

我的app.build。js是

({
    appDir: '..', /* relative to app.build.js */
    mainConfigFile: '../js/common.js', /* relative to app.build.js */
    baseUrl: 'js/lib', /* relative to current directory */
    dir: '../js-build', /* relative to app.build.js */
    optimize: 'uglify2',
    paths: {
        "jquery": "empty:",
        "jquery-ui": "empty:",
        "jquery.ui.selectmenu": "empty:",
        "common": "../common",
        "orders": "../orders",
        "emails": "../emails"
    },
    modules: [
        {
            name: 'common',
            include: [
                'modernizr'
            ],
            exclude: ['jquery-1.8.3.min', 'jquery-ui.min', 'jquery.ui.selectmenu']
        },
        {
            name: 'orders/form',
            exclude: ['common', 'jquery.ui.selectmenu']
        },
        {
            name: 'emails/verify',
            exclude: ['common', 'jquery.ui.selectmenu']
        }
     ]
})

When optimization runs as r.js.cmd -o js-tools\app.build.js from the webroot, I get a js-build directory with a copy of the whole webroot directory, optimized. Although not ideal (I wanted to limit it to just the js directory getting optimized), I can use my Ant driven build script to copy the contents of the js-build\js directory to the correct location.

当优化运行为r.js时。cmd - o js-tools \ app.build。在webroot中,我得到了一个js构建的目录,其中包含了整个webroot目录的副本,并进行了优化。虽然不理想(我希望将其限制为只对js目录进行优化),但我可以使用Ant驱动的构建脚本将js-build\js目录的内容复制到正确的位置。

When I run the build from the command line, the generated common.js, orders/form.js and emails/verify.js all exclude jquery.ui.selectmenu. common.js has modernizr included, as well as the header from the same lib. form.js and verify.js also exclude jquery.ui.selectmenu, and are devoid of any headers.

当我从命令行运行构建时,生成的common。js、订单/形式。js和电子邮件/验证。js jquery.ui.selectmenu排除。常见的。js包含了现代化的内容,也包括了来自相同的lib表单的标题。js和验证。js也排除jquery.ui。选择菜单,并且没有任何标题。

However, when I run from my Ant script, orders/form.js and emails/verify.js include jquery.ui.selectmenu and modernizr, even though I've given specific instruction for common and jquery.ui.selectmenu to be excluded.

然而,当我从我的Ant脚本运行时,命令/表单。js和电子邮件/验证。js包含jquery.ui。selectmenu和modernizr,尽管我已经给出了通用和jquery.ui的特定指令。selectmenu被排除在外。

I've excluded jquery.ui.selectmenu, because the particular version I am working with is written the the following form, and the browser has an issue with the end jQuery variable not being available. By excluding jquery.ui.selectmenu, I can attempt to load it separately, as though it came from a CDN.

我排除了jquery.ui。selectmenu,因为我正在处理的特定版本编写了以下表单,而浏览器有一个问题,即结束jQuery变量没有可用。jquery.ui除外。selectmenu,我可以尝试单独加载它,好像它来自一个CDN。

(function($, undefined) {
    $.widget("ui.selectmenu", {...
    });
}( jQuery ));

So, my issue is, how come the same app.build.js is resulting in different output?

所以,我的问题是,如何创建相同的应用程序。js会产生不同的输出吗?

1 个解决方案

#1


4  

When dealing with relative paths you might need to include a "." at the beginning of the path string (to specify that you want to go from the current path). I ran into this issue recently while testing out r.js on grunt. I had set my baseUrl to "/" initially when it should've been "."

在处理相对路径时,您可能需要在路径字符串的开头(指定要从当前路径中走)中包含一个“。”我最近在测试r的时候遇到了这个问题。js咕哝。我把我的baseUrl设置为"/"最初应该是"。"

In your case, your baseUrl is set to "js/lib" - perhaps try ".js/lib"

在您的案例中,您的baseUrl被设置为“js/lib”—可能尝试“.js/lib”。

It's a little weird since the error message I got seemed to have the correct base path as well as the relative path, but r.js was still unable to locate the file.

这有点奇怪,因为我得到的错误消息似乎有正确的基本路径和相对路径,但是r。js仍然无法找到文件。

This question is pretty old but I see it has a bunch of views, so I figured I'd put this on here as it might help someone out there.

这个问题很老,但是我看到它有很多视图,所以我想我应该把它放在这里,因为它可能会帮助一些人。


推荐阅读
author-avatar
妖姬脸似花甘露_545
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有