【问题标题】:Packaging up RequireJS "text!" modules打包RequireJS“文本!”模块
【发布时间】:2013-10-26 05:14:57
【问题描述】:

我试图弄清楚是否(以及如何,如果可能的话)使用RequireJS optimization tool 不仅包括我的 JavaScript 模块,还包括我的“文本!”模块。我正在开发一个使用“文本”的 Durandal 应用程序!视图模块。

有时我们的用户在尝试加载视图时会超时。这是一个错误示例:

Error: Load timeout for modules: text!views/primaryapplicants.html
http://requirejs.org/docs/errors.html#timeout

I've got another question I just posted about handling that timeout。我不知道如何拦截它并重试。我知道模块定义是有效的,只是客户可能有网络连接问题——尤其是如果他们使用手机的话。

然而,当我继续思考这一点时,我意识到如果我可以简单地将整个应用程序打包到一个文件中,那么我们可以避免额外的 HTTP 调用——这可能会减少这样的超时。这意味着应用程序要么加载,要么不加载——而不是“部分”加载的可能性。

这个应用没有大量的浏览量。我估计使用 gzip 压缩添加每个视图会增加大约 20kb。

那么,是否可以打包这些“文本!”模块以某种方式启动?

【问题讨论】:

  • 你试过使用 Weyland durandaljs.com/documentation/Building-with-Weyland Durandal 的 builder 吗?默认情况下,它将包含所有视图作为文本!**/*.html。
  • 我什至不知道它的存在......所以不,我没有尝试过。将检查出来。感谢您的提示!
  • 我没有 weyland-config.js 文件,也没有关于如何创建的文档。它只是说'......细节即将推出......'。所以这是一个未开始的,除非我可以在其他地方找到关于它的文档。哦,好吧,这似乎是正确的解决方案。
  • 添加了一个示例作为答案。

标签: knockout.js requirejs durandal bundling-and-minification r.js


【解决方案1】:

inlineText build config option(默认为true)指示优化器完全按照您的意愿行事。需要注意的一点是,与任何其他模块一样,它只会检测在某些模块的 define() 块中指定的依赖项。换句话说,它将无法检测到按需请求的 text! 依赖项,除非它们被明确地设置为可访问 - 这就是您的问题所在。

一种解决方法(如果您有许多视图文件,则远非理想)是指定您在构建配置中的 include option 中使用的每个 text! 依赖项,例如:

// ...
include: ["text!views/primaryapplicants.html",
  "text!views/secondaryapplicants.html",
  // etc.
]
// ...

【讨论】:

  • include 配置指令是我所需要的。谢谢!
【解决方案2】:

您可能想尝试一下 Durandal 的优化器 weyland。可以在 HTML StarterKit 中找到一个示例 weyland-config.js 配置。

https://github.com/BlueSpire/Durandal/blob/master/platforms/HTML/StarterKit/weyland-config.js

exports.config = function(weyland) {
    weyland.build('main')
        .task.jshint({
            include:'app/**/*.js'
        })
        .task.uglifyjs({
            include:['app/**/*.js', 'lib/durandal/**/*.js']
        })
        .task.rjs({
            include:['app/**/*.{js,html}', 'lib/durandal/**/*.js'],
            loaderPluginExtensionMaps:{
                '.html':'text'
            },
            rjs:{
                name:'../lib/require/almond-custom', //to deploy with require.js, use the build's name here instead
                insertRequire:['main'], //not needed for require
                baseUrl : 'app',
                wrap:true, //not needed for require
                paths : {
                    'text': '../lib/require/text',
                    'durandal':'../lib/durandal/js',
                    'plugins' : '../lib/durandal/js/plugins',
                    'transitions' : '../lib/durandal/js/transitions',
                    'knockout': '../lib/knockout/knockout-2.3.0',
                    'bootstrap': '../lib/bootstrap/js/bootstrap',
                    'jquery': '../lib/jquery/jquery-1.9.1'
                },
                inlineText: true,
                optimize : 'none',
                pragmas: {
                    build: true
                },
                stubModules : ['text'],
                keepBuildDir: true,
                out:'app/main-built.js'
            }
        });
}

【讨论】:

  • 这些路径都与我的应用程序中的路径不同,但这是一个很好的开始。我将一次通过这些路径,并根据我的应用程序的布局方式调整它们。我确信我是从“入门工具包”开始的,但显然不是因为我缺少 weyland-config.js 文件而且我的路径有点不同。我会报告进展情况。谢谢!
  • 嗯,这看起来很有希望,但是在更新所有路径并尝试执行 weyland 之后,它失败了,没有错误。实际上根本没有输出——即使使用“-v”选项(即“使用详细日志记录”)。这让我不知道下一步该去哪里。我想我会尝试让 RequireJS 的打包工作——并希望 Durandal 的人们整理更多关于 Weyland 的文档! :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 2012-03-07
  • 2012-01-24
  • 2013-07-29
  • 2015-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多