typescript automatically creating js files after running npm start

╄→尐↘猪︶ㄣ 提交于 2021-02-19 05:35:09

问题


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

 {
  "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"
  }
}

回答1:


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:

If you want all the resulting files to be concatenated into a single output file:

{
    "compilerOptions": {
        "outFile": "./dist/app.js"
    }
}

If you want all the resulting files to be output into a separate folder (but still as individual files):

{
    "compilerOptions": {
        "outDir": "./dist"
    }
}

Of course, if there's already a tsconfig.json there, you'll want to add these options, rather than replacing the entire thing.

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.




回答2:


In fact, when running the "npm run start" command, both TypeScript compiler and lite-server are started. If you only want to run the HTTP server, you could run the following command:

$ npm run lite

This way TypeScript files won't be compiled again at startup and you will use the previously compiled JS files to execute your Angular2 application.



来源:https://stackoverflow.com/questions/35662777/typescript-automatically-creating-js-files-after-running-npm-start

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!