使用requirejs bundles选项时,requirejs是否应包含在主构建文件中

时间:2015-02-17 22:33:06

标签: requirejs r.js

使用require bundles选项时遇到问题。如果主要构建文件内部有requirejs,那么一切正常,直到我尝试从另一个bundle加载文件。检索捆绑的文件,但随后抛出“define is undefined”错误。我能够加载bundle的唯一方法是确保requirejs不在主构建文件或pm.js中,然后使用脚本标记加载requirejs并使用data-main属性,但是这样似乎不对。

所以像这样的东西最初在main-built.js中包含requirejs时工作(网站加载正常),但是当pm.js包加载时我得到“define is undefined”错误

<script type="text/javascript" src="~/dist/main-built.js"></script>

requirejs.config({
    bundles: {
        'pm': ['pm/dashboard', 'text!pm/dashboard.html']
    }
});

这就是我最终让它发挥作用的方式,但似乎并不正确。

<script type="text/javascript" src="~/scripts/require.js" data-main="dist/main-debug")"></script>

这个durandal任务创建了主建文件

        durandal: {
            main: {
                src: ["app/**/*.*", "scripts/durandal/**/*.*", "!app/mockup/**/*.*", "!app/performancemanagement/**/*.*"],
                options: {
                    //name: "scripts/require",
                    name: "",
                    baseUrl: requireConfig.baseUrl,
                    paths: mixIn({}, requireConfig.paths, { "require": "scripts/require.js" }),
                    exclude: ["jquery", "knockout", "toastr", "moment", "underscore", "amplify"],
                    optimize: "none",
                    out: "dist/main-debug.js"
                }
            },
        },

此任务构建pm.js包

requirejs: {
            compile: {
                options: {
                    include: generateFileList("app/pm", "**/*.*", false, false),
                    //exclude: ["jquery", "knockout", "toastr", "moment", "underscore", "amplify", "preferenceconstants", "constants", "config", "utility/koutilities", "scripts/logger", "base/viewmodel"]
					//	.concat(generateFileList("scripts/durandal", "**/*.js", false))
					//	.concat(generateFileList("app/dataservice", "**/*.js", false))
					//	.concat(generateFileList("app/model", "**/*.js", false))
					//	.concat(generateFileList("app/reports", "**/*.js", false)),
                    baseUrl: "app/",
                    name: "",
                    paths: mixIn({}, requireConfig.paths, { "almond": "scripts/almond-custom.js" }),
                    optimize: 'none',
                    inlineText: true,
                    pragmas: {
                        build: true
                    },
                    stubModules: ['text'],
                    out: "dist/pm.js"
                }
            }
        }

当main-built中的任何东西需要它时,下载并执行pm.js包,现在它由Durandal中的路由器完成,但我很确定Durandal与此问题无关。

1 个答案:

答案 0 :(得分:0)

这在您的主文件构建中显得很可疑:

paths: mixIn({}, requireConfig.paths, { "require": "scripts/require.js" }),

我不确定mixIn位的作用,因为这不是库存RequireJS代码,但您似乎希望在名称为require的构建中包含RequireJS,这肯定是错误。 documentation says

  

如果要将require.js包含在main.js源中,可以使用以下命令:

node ../../r.js -o baseUrl=. paths.requireLib=../../require name=main include=requireLib out=main-built.js
  

因为&#34;要求&#34;是一个保留的依赖项名称,您创建一个&#34; requireLib&#34;依赖并将其映射到require.js文件。