我正在使用 VS2015 开发 HTML+TS+CSS 项目。是否可以将 JS 文件添加到组合的 TS 输出文件中?如果是这样怎么办?
最佳答案
您可以使用设置allowJs
.
Allow JavaScript files to be compiled.
与outFile
设置结合使用时,会将JS文件输出到组合文件中,并输出编译后的TS。
为了测试,我制作了一个快速的网络应用程序,其中包括 bootstrap 和 jquery 等等。
然后我还添加了一个自定义 JS 文件、一个自定义 TS 文件和一个 tsconfig.json。
这是 tsconfig.json 文件的内容:
{
"compilerOptions": {
"allowJs": true,
"outFile": "../output.js",
"alwaysStrict": true,
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules",
"wwwroot"
]
}
查看 output.js,我可以看到 bootstrap、jquery 以及我的自定义脚本。
请注意,我将输出文件放在项目目录之外。这是因为默认情况下 TS 编译器会拉入项目中的所有 TS 和 JS 文件。因此,如果输出文件位于项目中,并且项目中的所有文件都包含在内...您会收到一条错误消息,告诉您这不是一个好主意。
您可以使用excluded and included properties in the tsconfig.json来控制包含和排除哪些文件和路径。 .
例如
{
"compilerOptions": {
"allowJs": true,
"outFile": "output.js",
"alwaysStrict": true,
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules",
"Scripts/bootstrap.js",
"Scripts/jquery-1.10.2.js",
"output.js"
]
}
关于javascript - 将 Typescript + Javascript 与 Visual Studio 2015 相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45008077/