javascript - 与 requirejs 连接并排除单个文件?

标签 javascript text requirejs concatenation

我使用 requirejs 构建和应用程序,并使用它通过文本插件加载 html 模板和单个配置文件。

串联后,html 模板内联在 JS 中,这很好,但我需要 config.json 文件仍保留为文件,像串联前一样从外部加载。

主视图示例代码

define([
'jquery',
'underscore',
'backbone',
'lib/text!templates/main.html',
'lib/text!config.json',
], function($, _, Backbone, projectListTemplate, Config) {
    var MainView = Backbone.View.extend({});
})

这是我的构建文件

({
appDir: './../public',
baseUrl: 'static/js/app',
dir: './../dist',

modules: [
    {
        name: 'main'
    }
],
fileExclusionRegExp: /^(r|build)\.js$/,
exclude: ['config.json'],
optimizeCss: 'standard',
removeCombined: true,
paths: {
    jquery: 'lib/jquery-2.0.3.min',
    waypoints: 'lib/waypoints.min',
    underscore: 'lib/underscore',
    backbone: 'lib/backbone',
    text: 'lib/text',
},
shim: {
    underscore: {
        exports: '_'
    },
    backbone: {
        deps: [
            'underscore',
            'jquery',
            'waypoints'
        ],
        exports: 'Backbone'
    },
}
})

最佳答案

您应该像包含文件一样排除文件。试试这个方法:

exclude: ['lib/text!config.json']

但我只是停止使用这种方法,因为它需要在构建阶段存在 config.json 文件。相反,我使用嵌套的 require 调用(您可以在 defined 模块中使用 require 来在运行时加载文件)。与 define 不同,嵌套的 require 可能不包含在构建过​​程中(默认情况下它们不包含)并且验证阶段会很好。

请注意,这种方法至少有一个缺点:

嵌套的 require 调用也是异步的。但在我的情况下这不是问题。

关于javascript - 与 requirejs 连接并排除单个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18762382/

相关文章:

javascript - 自执行函数

javascript - 如何使用 ExtJs 创建动画文本

javascript - 在单页中加载两个 requireJs 应用程序

javascript - Backbone.js - 更新模型的特定属性不想触发 "change"事件

javascript - 显示微调器,直到 iframe 加载了 http post 响应

html - 使用 JSoup 删除 HTML 标签之间的文本

javascript - SVG 文本 - 使文本显示在 2 行中

javascript - Backbone targetModel = undefined

javascript - AMD:模块加载中的并发资源

javascript - 使用appendTo()时,点击事件不会绑定(bind)到动态元素