【发布时间】:2016-01-31 19:26:09
【问题描述】:
我正在开发打字稿库,我想在我的网络应用程序中使用它。
我想将所有库文件编译成单个 library.js 文件。
我尝试将 ts 编译器与 tsconfig.json 文件一起使用以及使用 gulp 任务进行编译,但在这两种情况下,输出文件中的类顺序都有问题。
生成的代码也有很多 IIFE
var MyLibrary;
(function (MyLibrary) {
.....
})(MyLibrary || (MyLibrary = {}));
我猜它应该只有一次,而不是每个班级,对吧?
我的 tsconfig.json:
{
"compilerOptions": {
"module": "amd",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"outFile": "dist/js/floor-map-designer.js",
"sourceMap": true
},
"exclude": [
"node_modules",
"dist",
"src/app.ts"
]
}
和 gulp 任务配置:
var tsProject = ts.createProject({
declarationFiles: true,
noExternalResolve: false,
module: 'AMD',
removeComments: true,
outFile: "my-library.js",
exclude: ["app.ts", "config.ts"]
});
var paths = {
npm: './node_modules/',
lib: "./lib/vendor/",
tsSource: ['./src/MyLibrary/**/*.ts', "./lib/typings/tsd.d.ts"],
tsOutput: "./dist/js/",
tsDef: "./lib/typings/"
};
gulp.task('ts-compile', function () {
var tsResult = gulp.src(paths.tsSource)
.pipe(ts(tsProject));
return merge([
tsResult.dts.pipe(gulp.dest(paths.tsDef)),
tsResult.js.pipe(gulp.dest(paths.tsOutput))
]);
});
我记得上次我使用 TypeScript 创建自动生成的参考文件有很多
/// <reference path="...."
并添加
/// <ts:autoRef="referencesFile.ts"> 到所有 .ts 文件。
但我注意到 TS 编译器不应该再依赖文件顺序,我的 IDE 也不再需要它,所以我想尽可能避免它。
我还注意到使用 --out 选项被认为是不好的(至少根据这个网站:https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md)但是如何替换它呢?
我正在使用 Typescript 1.7。
【问题讨论】:
标签: javascript compilation typescript