首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用requirejs加载包含多个类的文件(grunt-concat)

使用requirejs加载包含多个类的文件(grunt-concat)
EN

Stack Overflow用户
提问于 2013-11-30 15:37:43
回答 1查看 448关注 0票数 2

我有一个小问题的咕噜,让步和要求(和打字稿)。问题很简单,当我生成每个类一个文件,然后单独加载每个文件时,我就没有问题了。(但对于浏览器来说,速度要慢一些):

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

loader.ts:

代码语言:javascript
复制
require.config({
    paths: {
        'A': '../generated/shared/A.min',
        'Message': '../generated/shared/Message.min',
        'ValidatorMessage': '../generated/shared/ValidatorMessage.min', }//more

这就是工作。我可以要求浏览器中的每个文件并得到它。但如果这样做:

代码语言:javascript
复制
require.config({
        paths: {
            'shared': '../generated/shared.min',

那不管用。

有shared.min.js的内容

代码语言:javascript
复制
/*! 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,那么类消息将工作,而不是下一个,所以我认为只有加载的第一个类才能很好地加载!

Edit2: --我也忘记说,在A不存在的情况下,浏览器中的var消息存在并工作(正如我在Edit1上说的那样),但是var ValidatorMessage也存在!除了它是未定义的,我的意思是我从浏览器获得自动完成,但里面什么也没有。看起来,只有第一个define()将对象放置为全局对象。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-01 10:04:03

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

这里有一个可使用的grunt任务

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20302051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档