Typescript Compiler 找不到几个 npm 包(gulp-typescript)



我正在使用Gulp-typecript来编译打字稿文件。它有效,但它找不到"@angular/core" 之类的外部模块。如何配置TSC以在Node_modules中查找这些模块?

ps:我想将打字稿文件与Systemjs捆绑在一起。也许我可以以某种方式包括Systemjs-config,以便TSC知道在哪里?

代码:

gulp.task('tsc:release_dist', function() {
    return gulp.src(releaseBuildDest+'/**/*.ts')
        .pipe(gulpIgnore('node_modules/**'))
        .pipe(debug())
        .pipe(tsc({
            noImplicitAny: true,
            out: 'bundle.js',
            module: 'system'
        }))
        .pipe(gulp.dest(releaseBuildDest));
});

我的项目结构:

.dist/
..../release/
......./app/ (angular2 components, ...)
......./assets/ (css, img, ...)
......./node_modules/ (angular2 and other dependencies)
.......index.html
.......main.ts
.......systemjs.config.js

控制台输出

[12:25:59] Using gulpfile C:Developfrontendgulpfile.js
[12:25:59] Starting 'tsc:release_dist'...
[12:26:00] gulp-debug: distreleasemain.ts
[12:26:03] gulp-debug: distreleaseappapp.component.spec.ts
[12:26:05] gulp-debug: distreleaseappapp.component.ts
[12:26:05] gulp-debug: distreleaseappapp.module.ts
[12:26:05] gulp-debug: distreleaseappcomponentsafafcauthafafcauthafafc.component.ts
[12:26:05] gulp-debug: distreleaseappcomponentsafafclangafafclangafafc.component.ts
[12:26:06] gulp-debug: 6 items
distreleaseappapp.component.spec.ts(3,50): error TS2307: Cannot find module '@angular/core/testing'.
distreleaseappapp.component.spec.ts(4,20): error TS2307: Cannot find module '@angular/platform-browser'.
distreleaseappapp.component.spec.ts(5,30): error TS2307: Cannot find module '@angular/core'.
distreleaseappapp.component.ts(1,27): error TS2307: Cannot find module '@angular/core'.
distreleaseappapp.module.ts(2,31): error TS2307: Cannot find module '@angular/platform-browser'.
distreleaseappcomponentsafafcauthafafcauthafafc.component.ts(1,34): error TS2307: Cannot find module '@angular/core'.
distreleaseappcomponentsafafclangafafclangafafc.component.ts(1,34): error TS2307: Cannot find module '@angular/core'.
distreleasemain.ts(1,40): error TS2307: Cannot find module '@angular/platform-browser-dynamic'.

我的组件如何要求这些软件包:

import { NgModule } from '@angular/core';

tsconfig.js

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

其他信息:当我使用" NPM启动"编译常规开发构建时,一切正常,这是TSC -P SRC/的快捷方式。但是我试图用捆绑文件构建一个"释放构建"湾任务。但是,如果我将所有内容复制到发布构建目录并尝试使用TSC编译,则会在上面显示这些错误。

软件包JSON

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "my-project",
  "scripts": {
    "build": "tsc -p src/",
    "build:watch": "tsc -p src/ -w",
    "build:e2e": "tsc -p e2e/",
    "build:release": "gulp build:release",
    "serve": "lite-server -c=bs-config.json",
    "serve:e2e": "lite-server -c=bs-config.e2e.json",
    "prestart": "npm run build",
    "start": "concurrently "npm run build:watch" "gulp default:watch" "npm run serve"",
    "pree2e": "npm run build:e2e",
    "e2e": "concurrently "npm run serve:e2e" "npm run protractor" --kill-others --success first",
    "preprotractor": "webdriver-manager update",
    "protractor": "protractor protractor.config.js",
    "pretest": "npm run build",
    "test": "concurrently "npm run build:watch" "karma start karma.conf.js"",
    "pretest:once": "npm run build",
    "test:once": "karma start karma.conf.js --single-run",
    "lint": "tslint ./src/**/*.ts -t verbose",
    "gulp": "gulp"
  },
  "keywords": [],
  "author": "",
  "license": "UNLICENSED",
  "dependencies": {
    "@angular/common": "~2.4.0",
    "@angular/compiler": "~2.4.0",
    "@angular/core": "~2.4.0",
    "@angular/forms": "~2.4.0",
    "@angular/http": "~2.4.0",
    "@angular/platform-browser": "~2.4.0",
    "@angular/platform-browser-dynamic": "~2.4.0",
    "@angular/router": "~3.4.0",
    "core-js": "^2.4.1",
    "jquery": "^3.1.1",
    "material-design-icons": "^3.0.0",
    "rxjs": "5.0.1",
    "systemjs": "0.19.40",
    "zone.js": "^0.7.4"
  },
  "devDependencies": {
    "@types/jasmine": "2.5.36",
    "@types/node": "^6.0.46",
    "canonical-path": "0.0.2",
    "concurrently": "^3.2.0",
    "gulp": "^3.9.1",
    "gulp-browserify": "^0.5.0",
    "gulp-clean": "^0.3.2",
    "gulp-debug": "^3.1.0",
    "gulp-ignore": "^2.0.2",
    "gulp-jshint": "^2.0.0",
    "gulp-livereload": "^3.8.1",
    "gulp-npm-files": "^0.1.3",
    "gulp-refresh": "^1.1.0",
    "gulp-sass": "^2.2.0",
    "gulp-sass-lint": "^1.3.2",
    "gulp-sequence": "^0.4.6",
    "gulp-typescript": "^3.1.5",
    "gulp-util": "^3.0.7",
    "jasmine-core": "~2.4.1",
    "jshint": "^2.9.2",
    "jshint-stylish": "^2.1.0",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "lite-server": "^2.2.2",
    "lodash": "^4.16.4",
    "protractor": "~4.0.14",
    "rimraf": "^2.5.4",
    "tslint": "^3.15.1",
    "typescript": "~2.0.10"
  },
  "repository": {}
}

尝试设置您的Gulp打字稿以使用有关您项目的TSCONFIG信息。它可能会有所帮助。以下是一个对我有用的示例。如果您不需要,请删除相关零件。

var ts = require('gulp-typescript');
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
gulp.task('build.js.dev', () => 
{
    var tsProject = ts.createProject('tsconfig.json');
    var tsResult = tsProject.src()
        .pipe(sourcemaps.init())   
        .pipe(tsProject()); 
        //Write definitions 
        //tsResult.dts.pipe(gulp.dest(TEMP_TARGET_FOLDER)),
        //Write compiled js
    return tsResult.js.pipe(sourcemaps.write(
            ".", 
            { 
                includeContent: true, 
                sourceRoot: __dirname + "/dist"
            })).pipe(gulp.dest(TEMP_TARGET_FOLDER));
});

最新更新