我使用 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 调用(您可以在 define
d 模块中使用 require 来在运行时加载文件)。与 define
不同,嵌套的 require
可能不包含在构建过程中(默认情况下它们不包含)并且验证阶段会很好。
请注意,这种方法至少有一个缺点:
嵌套的 require 调用也是异步的。但在我的情况下这不是问题。
关于javascript - 与 requirejs 连接并排除单个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18762382/