【发布时间】:2017-08-04 17:36:28
【问题描述】:
我正在使用 gulp-typescript 来编译我的 typescript 文件。它可以工作,但找不到像“@angular/core”这样的外部模块。如何配置 TSC 以在 node_modules 中查找这些模块?
PS:我想将 typescript 文件与 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:\Develop\frontend\gulpfile.js
[12:25:59] Starting 'tsc:release_dist'...
[12:26:00] gulp-debug: dist\release\main.ts
[12:26:03] gulp-debug: dist\release\app\app.component.spec.ts
[12:26:05] gulp-debug: dist\release\app\app.component.ts
[12:26:05] gulp-debug: dist\release\app\app.module.ts
[12:26:05] gulp-debug: dist\release\app\components\afafc\authafafc\authafafc.component.ts
[12:26:05] gulp-debug: dist\release\app\components\afafc\langafafc\langafafc.component.ts
[12:26:06] gulp-debug: 6 items
dist\release\app\app.component.spec.ts(3,50): error TS2307: Cannot find module '@angular/core/testing'.
dist\release\app\app.component.spec.ts(4,20): error TS2307: Cannot find module '@angular/platform-browser'.
dist\release\app\app.component.spec.ts(5,30): error TS2307: Cannot find module '@angular/core'.
dist\release\app\app.component.ts(1,27): error TS2307: Cannot find module '@angular/core'.
dist\release\app\app.module.ts(2,31): error TS2307: Cannot find module '@angular/platform-browser'.
dist\release\app\components\afafc\authafafc\authafafc.component.ts(1,34): error TS2307: Cannot find module '@angular/core'.
dist\release\app\components\afafc\langafafc\langafafc.component.ts(1,34): error TS2307: Cannot find module '@angular/core'.
dist\release\main.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 start”编译常规开发构建时,一切正常,这是 tsc -p src/ 的快捷方式。但我正在尝试使用捆绑文件构建“发布构建”gulp-task。但是,如果我将所有内容复制到我的发布构建目录并尝试使用 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": {}
}
【问题讨论】:
-
包括您的
tsconfig.json以及您的package.json文件。问题中的文件 -
完成,我将您需要的所有内容添加到我的问题中。希望你能帮助我
-
我认为你应该尝试从 tsconfig 初始化你的 gulp-typescript - 所以它的行为类似于原始 tsc 调用。像这样:
var tsProject = ts.createProject('tsconfig.json');,然后是:...tsProject.src().pipe(tsProject())...。 -
我不明白。它不起作用..
gulp.task('tsc:release_dist', function() { var tsProject = tsc.createProject(releaseBuildDest+"/tsconfig.json"); tsProject.src().pipe(tsProject());? -
var tsProject = tsc.createProject(releaseBuildDest+"/tsconfig.json"); var tsResult = gulp.src(releaseBuildDest+'/**/*.ts').pipe(tsProject()); return tsResult.js.pipe(gulp.dest('TEST'))产生:dist\release\node_modules\@angular\router\upgrade.d.ts(10,31):错误 TS2307:找不到模块“@angular/upgrade/static”。
标签: angular typescript tsc