简体   繁体   English

如何为Angular 2设置gulp-typescript?

[英]How to setup gulp-typescript for Angular 2?

currently I'm using tsc compiler with watch flag from prompt. 目前我正在使用带有提示符的watch标志的tsc编译器。 It works well, load all definition files and compile correctly each angular2 file. 它运行良好,加载所有定义文件并正确编译每个angular2文件。 However is very uncomfortable to use through the shell window. 但是通过shell窗口使用会非常不舒服。

My goal is to create a gulp task that can translate any typescript file according to angular2 definitions. 我的目标是创建一个gulp任务,可以根据angular2定义翻译任何打字稿文件。 I've involved gulp-typescript , seems easy to use, so this is the code: 我参与了gulp-typescript ,看起来很容易使用,所以这就是代码:

var tsProject = $.typescript.createProject(paths.src + '/tsconfig.json');
gulp.task('typescript', function() {
  gulp
    .src([
      paths.src + '/*.ts'
    ])
    .pipe($.typescript(tsProject)).js
    .pipe(gulp.dest(paths.tmp));
});

And this is the folder structure: 这是文件夹结构:

...
src/
---- app/
-------- ts/
------------ *.ts
---- typings/
-------- angular2/
------------ angular2.d.ts
------------ http.d.ts
-------- es6-promise/
------------ ...
-------- rx/
------------ ...
-------- tsd.d.ts
---- *.ts
---- tsconfig.json
gulpfile.js
...

The tsconfig file has no files list, so the compiler should check any ts file inside src (at any level). tsconfig文件没有files列表,因此编译器应该检查src任何ts文件(在任何级别)。

When the task is invoked i receive this errors: 调用任务时,我收到以下错误:

error TS2307: Cannot find module 'angular2/angular2'.
error TS2307: Cannot find module 'angular2/http'.

How can I tell to typescript compiler which d.ts files to use? 如何告诉d.ts编译器使用哪些d.ts文件?

It is looks like you have not declared amd-reference in your files yet. 看起来您尚未在文件中声明amd-reference

Try to declare in files where you import your modules something like that: 尝试在您导入模块的文件中声明:

/// <amd-dependency path="./src/typings/angular2/angular2.d.ts" />
/// <amd-dependency path="./src/typings/angular2/http.d.ts" />

import angular2 = require("angular2");
import http = require("http")

/// <amd-dependency tells compiler that special modules exists in system and give possibility to compile without errors /// <amd-dependency告诉编译器系统中存在特殊模块,并且可以无错误地编译

I'd recommend to add definitions to gulp.src. 我建议在gulp.src中添加定义。 Something like this: 像这样的东西:

var tsProject = $.typescript.createProject(paths.src + '/tsconfig.json');
gulp.task('typescript', function() {
  gulp
    .src([
      paths.src + '/app/**/*.ts',
      paths.src + '/typings/**/*.d.ts'
    ])
    .pipe($.typescript(tsProject)).js
    .pipe(gulp.dest(paths.tmp));
});

This also had me cursing and swearing for a while. 这也让我诅咒和咒骂了一会儿。 Make sure you have this in the compilerOptions node of your tsconfig.json: 确保在tsconfig.json的compilerOptions节点中有这个:

{
  "compilerOptions": {
    "moduleResolution": "node",
  }
}

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

相关问题 使用gulp-typescript配置打字 - Configuring typings with gulp-typescript 使用gulp-typescript获得“无法找到模块&#39;angular2 / angular2&#39;” - Got “Cannot find module 'angular2/angular2'” using gulp-typescript Typescript编译排序问题gulp-typescript,TSC 1.6 - Typescript compile ordering issue gulp-typescript, tsc 1.6 gulp-typescript正在编译所有.ts文件,而不仅仅是来自特定位置的文件 - gulp-typescript is compiling all .ts files not just those from specific location 带有requirejs和许多路径别名和软件包的gulp-typescript,错误TS2307 - gulp-typescript with requirejs and many path aliases and packages, error TS2307 使用Gulp设置TypeScript / Angular - Setting up TypeScript / Angular with Gulp 如何在没有gulp的情况下将gulp设置项目转换为普通的angular js项目 - How to convert gulp setup project to normal angular js project without gulp 如何为这种脚手架设置口水? - How to setup gulp for this type of scaffolding? 如何使用gulp uglify在angular 2中加载缩小文件? (最小化TYpescript捆绑文件) - How to load minified file in angular 2 using gulp uglify ? (which minifies TYpescript Bundled File) 如何设置 gulp 文件以进行实时重新加载 - how to setup gulp file for live reload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM