javascript - 使用 requirejs 加载一个文件,里面有几个类(grunt-concat)

标签 javascript node.js requirejs typescript gruntjs

我对 grunt concat 和 requirejs(和 typescript )有点小问题。 问题很简单,当我为每个类生成 一个 文件并分别加载每个文件时,我没有问题。 (但浏览器速度较慢) 示例:

  • A.js
  • 消息.js
  • ValidatorMessage.js(扩展 Message 并具有依赖性)

loader.ts:

require.config({
    paths: {
        'A': '../generated/shared/A.min',
        'Message': '../generated/shared/Message.min',
        'ValidatorMessage': '../generated/shared/ValidatorMessage.min', }//more

这是可行的。我可以在浏览器中要求()每个文件,我得到它。 但如果这样做:

require.config({
        paths: {
            'shared': '../generated/shared.min',

那行不通。

里面有shared.min.js的内容

/*! MotorEngine-web - v0.0.1 - 2013-11-30 04:11:18 - development */
///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>
define([ "require", "exports" ], function(require, exports) {
    /**
    * @name ValidatorMessage
    * @description  Message object for Validation errors.
    * @author Vadorequest
    */
    var A = function() {
        function A() {}
        /**
        * Get the message.
        * @returns {string}
        */
        return A.prototype.getErrors = function() {
            return this._errors;
        }, A;
    }();
    exports.A = A;
}), ///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>
define([ "require", "exports" ], function(require, exports) {
    /**
    * @name Message
    * @description Message object for both server and client side communication.
    * @author Vadorequest
    */
    var Message = function() {
        /**
        * Constructor.
        * @param message   Message to display.
        * @param data      Data useful for callback, can be anything.
        * @param status    Status of the message.
        */
        function Message(message, data, status) {
            "undefined" == typeof data && (data = !1), "undefined" == typeof status && (status = !1), 
            /**
            * Constants.
            */
            this.FIELD_NAME_MESSAGE = "m", this.FIELD_NAME_DATA = "d", this.FIELD_NAME_STATUS = "s", 
            this.EXCEPTION_BAD_JSON_CONTENT = 'Unable to parse JSON. Bad object/string attributes. (Missing message ("' + this.FIELD_NAME_MESSAGE + '" field) or status ("' + this.FIELD_NAME_MESSAGE + '" field)?', 
            this.EXCEPTION_BAD_JSON_TYPE = "Incorrect data type. Object or string expected.", 
            this._message = message, this._data = data, this._status = status;
        }
        /**
        * Get the message.
        * @returns {string}
        */
        return Message.prototype.getMessage = function() {
            return this._message;
        }, /**
        * Get message data.
        * @returns {*}
        */
        Message.prototype.getData = function() {
            return this._data;
        }, /**
        * Get message status.
        * @returns {boolean}
        */
        Message.prototype.getStatus = function() {
            return this._status;
        }, /**
        * Returns the current object as JSON string.
        * @returns {string}
        */
        Message.prototype.toJSON = function() {
            return JSON.stringify(this._toSimpleObject());
        }, /**
        * Returns the current object without methods.
        * @returns {Object}
        */
        Message.prototype.toObject = function() {
            return this._toSimpleObject();
        }, /**
        * Returns a custom object without method using the predefined FIELD_NAME to take less space once converted in JSON string.
        * @returns {{}}
        * @private
        */
        Message.prototype._toSimpleObject = function() {
            var json = {};
            return json[this.FIELD_NAME_MESSAGE] = this._message, this._data !== !1 && (json[this.FIELD_NAME_DATA] = this._data), 
            json[this.FIELD_NAME_STATUS] = this._status, json;
        }, /**
        * Convert JSON to a Message object instance.
        * @param json  Json to convert to object.
        * @returns {Message.Message}
        */
        Message.prototype.fromJSON = function(json) {
            if ("object" == typeof json) return this._fromJSONObject(json);
            if ("string" == typeof json) return this._fromJSONString(json);
            throw "Message.fromJSON " + this.EXCEPTION_BAD_JSON_TYPE;
        }, /**
        * Convert JSON object to a Message object instance.
        * @param json  Json to convert to object.
        * @returns {Message.Message}
        * @private
        */
        Message.prototype._fromJSONObject = function(json) {
            if (json[this.FIELD_NAME_MESSAGE] && json[this.FIELD_NAME_STATUS]) return json[this.FIELD_NAME_DATA] ? new Message(json[this.FIELD_NAME_MESSAGE], json[this.FIELD_NAME_DATA], json[this.FIELD_NAME_STATUS]) : new Message(json[this.FIELD_NAME_MESSAGE], !1, json[this.FIELD_NAME_STATUS]);
            throw "Message._fromJSONObject " + this.EXCEPTION_BAD_JSON_CONTENT;
        }, /**
        * Convert JSON string to a Message object instance.
        * @param json  Json to convert to object.
        * @returns {Message.Message}
        * @private
        */
        Message.prototype._fromJSONString = function(json) {
            try {
                return this._fromJSONObject(JSON.parse(json));
            } catch (e) {
                throw "Message._fromJSONString: JSON.parse error:" + e.message;
            }
        }, Message;
    }();
    exports.Message = Message;
});

///<reference path='./def/lib.d.ts'/>
///<reference path='./def/node.d.ts'/>
var __extends = this.__extends || function(d, b) {
    function __() {
        this.constructor = d;
    }
    for (var p in b) b.hasOwnProperty(p) && (d[p] = b[p]);
    __.prototype = b.prototype, d.prototype = new __();
};

define([ "require", "exports", "Message" ], function(require, exports, __message__) {
    var message = __message__, ValidatorMessage = function(_super) {
        function ValidatorMessage(message) {
            _super.call(this, message);
        }
        return __extends(ValidatorMessage, _super), /**
        * Get the message.
        * @returns {string}
        */
        ValidatorMessage.prototype.getErrors = function() {
            return this._errors;
        }, ValidatorMessage;
    }(message.Message);
    exports.ValidatorMessage = ValidatorMessage;
});

我不明白是因为在同一个文件中有多个定义调用还是其他原因。你呢?

编辑: 我忘了说,在这个例子中,类 A 将在浏览器端可用(并且可以工作),但在下面不可用。如果我将 A 删除到 shared.min.js,那么 Message 类将起作用,但下一个不起作用,所以我认为只有加载的 first 类加载得很好!

编辑2: 我还忘了说,在 A 不存在的情况下,浏览器中的 var Message 存在并且有效(正如我在 Edit1 中所说),但 var ValidatorMessage 也存在!除了它是未定义的,我的意思是我从浏览器获得了自动完成功能,但里面什么也没有。看起来只有第一个 define() 将对象作为全局对象。

最佳答案

您应该使用 r.js optimizer in production ,而不是手动将文件连接在一起;否则你将失去模块化代码的好处(每个文件一个类)。让优化器完成计算依赖图的艰苦工作,不要试图重新发明轮子!

有一个grunt task available为此。

关于javascript - 使用 requirejs 加载一个文件,里面有几个类(grunt-concat),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20302051/

相关文章:

javascript - Backbone & Require - 离开时破坏 View

javascript - Jade 中的过滤器嵌套失败

javascript - 高速公路JS : Remote call to asyncronous function

javascript - 如何在 Yeoman web 应用程序中配置 eslint?

javascript - 无法让我的文本值在我的查询中工作

javascript - 确保加载 requireJS 条件模块

javascript - 使用 require.js 将 jQuery、Underscore 和 Backbone 包含为 AMD 模块的最佳方法是什么?

javascript - 如何在 express、couchDB 和 node.js 中使用 session

javascript - 按键时触发 AngularJS $scope 事件

javascript - 创建不同的数据集数组