从TypeScript生成的DTO对象出现webpack错误

时间:2019-06-11 17:59:00

标签: javascript typescript webpack

我有用TypeScript编写的DTO,然后将它们用在webpack打包的文件中,但是由于webpack没有将TypeScript DTO文件中的javascript文件捆绑在一起,因此生成的捆绑包会产生运行时错误。

仔细阅读webpack的文档并没有发现一种盲目包含目录中所有javascript文件的方法。

== DTO ==

module PortalSPA.Models.PurchasePath {
export class SaveModel extends LibSPA.Models.QuoteIdModel {
public FirstName: string
public LastName: string
public CompanyName: string
public DBA: string
public ContactEmail: string
public ContactPhone: string
public ContactExtension: string
public State: string
public ZipCode: string
public MultipleStates: PortalSPA.Models.PurchasePath.FEQuestionModel
}
}
var extends = (this && this.extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ proto: [] } instanceof Array && function (d, b) { d.proto = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function () { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (.prototype = b.prototype, new ());
};
})();
var PortalSPA;
(function (PortalSPA) {
var Models;
(function (Models) {
var PurchasePath;
(function (PurchasePath) {
var SaveModel = (function (_super) { extends(SaveModel, _super);
function SaveModel() {
return _super !== null && _super.apply(this, arguments) || this;
}
return SaveModel;
}(LibSPA.Models.QuoteIdModel));
PurchasePath.SaveModel = SaveModel;
})(PurchasePath = Models.PurchasePath || (Models.PurchasePath = {}));
})(Models = PortalSPA.Models || (PortalSPA.Models = {}));
})(PortalSPA || (PortalSPA = {}));

==

在另一个TypeScript文件中用作:

let saveModel: PortalSPA.Models.PurchasePath.SaveModel = {
FirstName: this.FirstName,
LastName: this.LastName,
CompanyName: this.BusinessName,
DBA: this.DBA,
ContactEmail: this.ContactEmail,
ContactPhone: this.ContactPhone,
ContactExtension: this.ContactExtension,
State: this.State,
ZipCode: this.Zip,
MultipleStates: this.MultipleStates,
QuoteId: -1,
QuoteIdEncrypted: null
};

这会导致运行时错误,因为webpack不会捆绑DTO目录中的所有DTO文件,因此在捆绑的javascript文件(如LibSPA.Models.QuoteIdModel)中的引用会导致未定义的异常。

webpack应该能够看到DTO javscript文件(例如LibSPA.Models.QuoteIdModel)中的引用意味着它需要查找包含所有相关定义的其他javascript文件。包含DTO的目录(TypeScript原始文件和转译的javascript结果)都位于webpack的源路径中,因此webpack不应没有任何理由跳过任何文件。

0 个答案:

没有答案