我正在使用TS 1.7,并且正在尝试将我的项目编译为一个大文件,并将其包含在html文件中。
我的项目结构如下:
-build // Build directory
-src // source root
--main.ts // my "Main" file that uses the imports my outer files
--subDirectories with more ts files.
-package.json
-tsconfig.json
我的tsconfig文件是:
{
"compilerOptions": {
"module":"amd",
"target": "ES5",
"removeComments": true,
"preserveConstEnums": true,
"outDir": "./build",
"outFile":"./build/build.js",
"sourceRoot": "./src/",
"rootDir": "./src/",
"sourceMap": true
}
}
构建项目时,我希望build.js文件是从我的源代码编译来的一个大文件。但是build.js文件为空,我将所有文件编译为js文件。
我的每个TS文件看起来都像这样
import {blabla} from "../../bla/blas";
export default class bar implements someThing {
private variable : string;
}
我究竟做错了什么 ?
这将在TypeSript 1.8中实现。在那个版本中,当模块是amd或system时,outFile选项可以工作。
目前,该功能在Typescript的开发版本中可用。要安装该运行:
$ npm install -g typescript@next
对于以前的版本,即使不是很明显,模块和outFile选项也不能一起使用。
您可以检查此问题以获取更多详细信息。
如果要输出版本低于1.8的单个文件,则不能使用tsconfig.json中的module选项。相反,您必须使用module关键字创建名称空间。
您的tsconfig.json文件应如下所示:
{
"compilerOptions": {
"target": "ES5",
"removeComments": true,
"preserveConstEnums": true,
"outFile": "./build/build.js",
"sourceRoot": "./src/",
"rootDir": "./src/",
"sourceMap": true
}
}
您的TS文件也应如下所示:
module SomeModule {
export class RaceTrack {
constructor(private host: Element) {
host.appendChild(document.createElement("canvas"));
}
}
}
而且,除了使用import语句,您还必须按名称空间引用导入。
window.addEventListener("load", (ev: Event) => {
var racetrack = new SomeModule.RaceTrack(document.getElementById("content"));
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句