与 requirejs 连接并排除单个文件

Concatenate with requirejs and exclude a single file?

本文关键字:单个 文件 排除 requirejs 连接      更新时间:2023-09-26

我有使用 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文件。相反,我使用的是嵌套的需求调用(您可以在 define d 模块中使用 require 在运行时加载文件)。与define不同,嵌套require可能不包含在构建过程中(默认情况下它们不包含),验证阶段将很好。

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

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