简体   繁体   English

用于 Angular2 的 TypeScript 到 JavaScript 转译器

[英]TypeScript to JavaScript transpiler for Angular2

Since Ancular2 is written in TypeScript, I also write my app in Typescript.由于 Ancular2 是用 TypeScript 编写的,我也用 TypeScript 编写我的应用程序。 I want to deploy the app on a tomcat server without node.js running.我想在不运行 node.js 的情况下在 tomcat 服务器上部署应用程序。 After a long search, I found two possibilities that work: http://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5?p=preview In this version, the Typescript-files are transpiled on-the-fly and this is works for what I need.经过长时间的搜索,我发现了两种可行的方法: http ://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5?p=preview 在这个版本中,Typescript 文件是即时转换的,这适用于什么我需要。 But there would be a nicer solution with transpiling and minifying it before the deployment on the server.但是在服务器上部署之前,将有一个更好的解决方案,即转换和缩小它。 I tryed it with gulp but the resulting .ts-files (with and without minifying) didn't work.我用 gulp 尝试过,但生成的 .ts 文件(有和没有缩小)不起作用。 then I foung another plunker with only js-files working: https://plnkr.co/edit/8lOtCy4GPsq9hZH6D38E?p=preview Comparing those files, I saw that my compiled files looks much more complicated:然后我找到了另一个只有 js 文件工作的 plunker: https ://plnkr.co/edit/8lOtCy4GPsq9hZH6D38E?p=preview 比较这些文件,我发现我编译的文件看起来要复杂得多:

 "use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var core_1 = require("@angular/core"); var AppComponent = (function () { function AppComponent() { this.name = 'Angular'; } return AppComponent; }()); AppComponent = __decorate([ core_1.Component({ selector: 'my-app', template: "<h1>Hello {{name}}</h1>" }) ], AppComponent); exports.AppComponent = AppComponent;

So I tried diferent tsconfig.json properties to get a working transpiler, but it seem to be something with the transpiler itself (I used for both variants the typescript@2.1.6 version)所以我尝试了不同的 tsconfig.json 属性来获得一个有效的转译器,但它似乎与转译器本身有关(我用于两种变体的 typescript@2.1.6 版本)

I already tried with model in tsconfig.json beeing: amd, umd, system, commonjs, es2015 but this didn't solve the problem.我已经在 tsconfig.json beeing: amd, umd, system, commonjs, es2015 中尝试使用模型,但这并没有解决问题。 Also I tried with noEmeritHelpers, but the problem was still there (I think that I once read a problem with var core_1 = require("@angular/core"); not beeing defined, but this didn't help me).我也尝试使用 noEmeritHelpers,但问题仍然存在(我想我曾经读过一个问题var core_1 = require("@angular/core"); not beeing defined,但这对我没有帮助)。

I want to transpile all .ts-files from my app folder, combine them in one and then minify it.我想从我的应用程序文件夹中转译所有 .ts 文件,将它们合二为一,然后将其缩小。 The best way to do it would by with gulp, since this would be easy to combine with my maven project.最好的方法是使用 gulp,因为这很容易与我的 Maven 项目结合。

use Angular CLI使用 Angular CLI

https://github.com/angular/angular-cli https://github.com/angular/angular-cli

edit the angular-cli.json config file编辑angular-cli.json配置文件

"outDir": "path/to/maven-app/main/resources/webapp",

then you could run ng build --prod --watch or ng build --watch然后你可以运行ng build --prod --watchng build --watch

then build your maven app, it'll contain all build file: js, css, assets, etc然后构建您的 maven 应用程序,它将包含所有构建文件:js、css、资产等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM