Javascript 使用requirejs加载包含多个类的文件(grunt concat)
我对grunt concat和requirejs(以及typescript)有点问题。 问题很简单,当我为每个类生成一个文件并分别加载每个文件时,我没有问题。(但浏览器的速度较慢) 例如:Javascript 使用requirejs加载包含多个类的文件(grunt concat),javascript,node.js,requirejs,typescript,gruntjs,Javascript,Node.js,Requirejs,Typescript,Gruntjs,我对grunt concat和requirejs(以及typescript)有点问题。 问题很简单,当我为每个类生成一个文件并分别加载每个文件时,我没有问题。(但浏览器的速度较慢) 例如: A.js Message.js ValidatorMessage.js(扩展消息并具有依赖项) loader.ts: require.config({ paths: { 'A': '../generated/shared/A.min', 'Message': '..
- A.js
- Message.js
- ValidatorMessage.js(扩展消息并具有依赖项)
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的内容
/*!发动机网络-v0.0.1-2013-11-30 04:11:18-开发*/
///
///
定义([“需要”、“导出”]、函数(需要、导出){
/**
*@name验证器消息
*@验证错误的说明消息对象。
*@author Vadorequest
*/
var A=函数(){
函数A(){}
/**
*得到消息。
*@返回{string}
*/
返回A.prototype.getErrors=function(){
返回此。\u错误;
},A;
}();
A=A;
}), ///
///
定义([“需要”、“导出”]、函数(需要、导出){
/**
*@name消息
*@description消息对象,用于服务器端和客户端通信。
*@author Vadorequest
*/
var Message=function(){
/**
*构造器。
*@param要显示的消息。
*@param数据用于回调,可以是任何数据。
*@param状态消息的状态。
*/
功能消息(消息、数据、状态){
“未定义”==数据类型&&(数据=!1),“未定义”==状态类型&&(状态=!1),
/**
*常数。
*/
this.FIELD\u NAME\u MESSAGE=“m”、this.FIELD\u NAME\u DATA=“d”、this.FIELD\u NAME\u STATUS=“s”,
this.EXCEPTION\u BAD\u JSON\u CONTENT='无法分析JSON.BAD对象/字符串属性。(缺少消息(“+this.FIELD\u NAME\u message+””字段)或状态(“+this.FIELD\u NAME\u message+”)),
this.EXCEPTION\u BAD\u JSON\u TYPE=“不正确的数据类型。应为对象或字符串。”,
此。_消息=消息,此。_数据=数据,此。_状态=状态;
}
/**
*得到消息。
*@返回{string}
*/
return Message.prototype.getMessage=函数(){
返回此消息;
}, /**
*获取消息数据。
*@returns{*}
*/
Message.prototype.getData=函数(){
返回此。\u数据;
}, /**
*获取消息状态。
*@returns{boolean}
*/
Message.prototype.getStatus=函数(){
返回此状态。\u;
}, /**
*以JSON字符串的形式返回当前对象。
*@返回{string}
*/
Message.prototype.toJSON=function(){
返回JSON.stringify(this.\u toSimpleObject());
}, /**
*返回不带方法的当前对象。
*@返回{Object}
*/
Message.prototype.toObject=函数(){
返回此项。_toSimpleObject();
}, /**
*返回一个自定义对象,该对象不包含使用预定义字段_NAME的方法,以便在转换为JSON字符串后占用更少的空间。
*@returns{{}
*@私人
*/
Message.prototype.\u to简化对象=函数(){
var json={};
返回json[this.FIELD\u NAME\u MESSAGE]=this.\u MESSAGE,this.\u data!==!1&&(json[this.FIELD\u NAME\u data]=this.\u data),
json[this.FIELD\u NAME\u STATUS]=this.\u STATUS,json;
}, /**
*将JSON转换为消息对象实例。
*@param json要转换为对象。
*@returns{Message.Message}
*/
Message.prototype.fromJSON=函数(json){
如果(“object”==typeof json)返回这个;
如果(“string”==typeof json),则从jsonstring(json)返回此;
抛出“Message.fromJSON”+this.EXCEPTION\u BAD\u JSON\u TYPE;
}, /**
*将JSON对象转换为消息对象实例。
*@param json要转换为对象。
*@returns{Message.Message}
*@私人
*/
Message.prototype.\u fromJSONObject=function(json){
if(json[this.FIELD\u NAME\u MESSAGE]&&json[this.FIELD\u NAME\u STATUS])返回json[this.FIELD\u NAME\u DATA]?新消息(json[this.FIELD\u NAME\u MESSAGE]、json[this.FIELD\u NAME\u STATUS]:新消息(json[this.FIELD\u NAME\u MESSAGE]、!1、json[this.FIELD\u NAME\u STATUS]);
抛出“Message.\u fromJSONObject”+this.EXCEPTION\u BAD\u JSON\u内容;
}, /**
*将JSON字符串转换为消息对象实例。
*@param json要转换为对象。
*@returns{Message.Message}
*@私人
*/
Message.prototype.\u fromJSONString=function(json){
试一试{
从jsonObject(JSON.parse(JSON))返回这个;
}捕获(e){
抛出“Message.\u fromJSONString:JSON.parse错误:”+e.Message;
}
},讯息;
}();
exports.Message=消息;
});
///
///
var uu extends=这个。u extends | |函数(d,b){
函数{
这个构造函数=d;
}
对于(b中的风险值p)b.hasOwnProperty(p)&(d[p]=b[p]);
__.prototype=b.prototype,d.prototype=new__;();
};
定义([“要求”、“导出”、“消息”]、函数(要求、导出、消息){
var消息=\uuuuuuuuuuuuuuuuu消息,ValidatorMessage=函数(\uSuper){
函数验证器消息(消息){
_超级。呼叫(此,消息);
}
返回uu扩展(ValidatorMessage,super)/**
*得到消息。
*@返回{string}
*/
ValidatorMessage.prototype.getErrors=函数(){
返回此。\u错误;
},验证信息;
/*! 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;
});