问题描述
我正在使用 typescript 创建一个 Angular2 应用程序,每次运行 npm start 时,我的所有 .ts 文件都会编译成 javascript 文件并放入目录中.有没有办法把它关掉.
I'm creating an Angular2 app using typescript, and every time I run npm start, all of my .ts files are compiled into javascript files and put in the directory. Is there anyway to turn this off.
package.json
package.json
{
"name": "angular-prac",
"version": "1.0.0",
"scripts": {
"start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",
"tsc": "tsc",
"tsc:w": "tsc -w",
"lite": "lite-server",
"typings": "typings",
"postinstall": "typings install"
},
"license": "ISC",
"dependencies": {
"angular2": "2.0.0-beta.7",
"systemjs": "0.19.22",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.2",
"zone.js": "0.5.15"
},
"devDependencies": {
"concurrently": "^2.0.0",
"lite-server": "^2.1.0",
"typescript": "^1.7.5",
"typings":"^0.6.8"
}
}
推荐答案
问题很可能出在您的 tsconfig.json
- 这是文件(通常位于您的项目根目录中,与 package.json
) 包含您的 TypeScript 构建的配置.默认情况下,它只是在适当的位置编译文件,从而导致您描述的源文件夹被编译的 Javascript 文件填满的情况.您需要添加的选项取决于您想要的最终结果:
The issue is most likely with your tsconfig.json
- this is the file (usually located in your project root, alongside package.json
) that contains the configuration for your TypeScript build. By default, it just compiles the files in place, leading to the situation you're describing where your source folder fills up with compiled Javascript files. The options you'll need to add will depend on what you want the end result to be:
如果您希望将所有结果文件连接成一个输出文件:
{
"compilerOptions": {
"outFile": "./dist/app.js"
}
}
如果您希望将所有生成的文件输出到一个单独的文件夹中(但仍作为单个文件):
{
"compilerOptions": {
"outDir": "./dist"
}
}
当然,如果那里已经有 tsconfig.json
,您需要添加这些选项,而不是替换整个内容.
Of course, if there's already a tsconfig.json
there, you'll want to add these options, rather than replacing the entire thing.
有大量选项可用于配置 TypeScript 构建过程 - 如果您想对其进行更多调整,可以找到 wiki 上的信息,或者如果您需要更多示例,请告诉我,我会将它们编辑为答案.
There are a massive amount of options available for configuring the TypeScript build process - if you want to tweak it some more, you can find info on the wiki, or if you need some more examples, let me know and I'll edit them into the answer.
这篇关于运行 npm start 后打字稿自动创建 js 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!