1

我使用 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'
    },
}
})
4

1 回答 1

2

您应该以与包含它相同的方式排除您的文件。试试这个方法:

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

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

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

嵌套的 require 调用也是异步的。但就我而言,这不是问题。

于 2014-01-15T00:54:12.343 回答