Javascript 无法启动TypeScript项目
最近我改用大口喝。经过一番努力,我终于成功地编译了我的项目。现在,我无法启动它,VS代码告诉我: 无法启动程序“../src/bootstrap.ts”;设置“outFiles”属性可能会有所帮助。 当我使用tsc编译和运行应用程序时,一切都正常 以下是我的配置文件: launch.json:Javascript 无法启动TypeScript项目,javascript,typescript,gulp,visual-studio-code,Javascript,Typescript,Gulp,Visual Studio Code,最近我改用大口喝。经过一番努力,我终于成功地编译了我的项目。现在,我无法启动它,VS代码告诉我: 无法启动程序“../src/bootstrap.ts”;设置“outFiles”属性可能会有所帮助。 当我使用tsc编译和运行应用程序时,一切都正常 以下是我的配置文件: launch.json: { "version": "0.2.0", "configurations": [ { "type": "node", "r
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"preLaunchTask": "build",
"sourceMaps": true,
"program": "${workspaceRoot}/src/bootstrap.ts",
"outFiles": ["${workspaceRoot}/lib/**/*.js"],
"cwd": "${workspaceRoot}"
}
]
}
tasks.json:
{
"version": "0.1.0",
"command": "gulp",
"isShellCommand": true,
"args": [
"--no-color"
],
"tasks": [
{
"taskName": "build",
"isBuildCommand": true,
"showOutput": "always",
"problemMatcher": "$tsc"
}
]
}
gulpfile.js:
var gulp = require('gulp')
var ts = require('gulp-typescript');
var sourcemaps = require('gulp-sourcemaps');
gulp.task('copy', function () {
var copyResult = gulp.src('src/templates/*.hbs')
.pipe(gulp.dest('lib/templates/'));
return copyResult;
});
gulp.task('compile', function () {
var tsResult = gulp.src('src/**/*.ts')
.pipe(sourcemaps.init())
.pipe(ts({
module: "commonjs",
target: "es6",
noImplicitAny: false,
typeRoots: ["node_modules/@types"],
declaration: true,
experimentalDecorators: true,
emitDecoratorMetadata: true
}));
return tsResult.js
.pipe(sourcemaps.write('.', { sourceRoot: function (file) { return file.cwd + '/lib'; } }))
.pipe(gulp.dest('lib'));
});
gulp.task('build', ['copy', 'compile']);
以下是项目文件夹结构:
- lib (compiled output)
- api
- engine
- src
- api
- engine
- bootstrap.ts
- gulpfile.js
- tsconfig.json
因此,我无法找到外部文件设置的错误
编辑
好吧,我想我已经知道发生了什么
这是tsc生成的js.map文件(映射属性省略):
这是gulp生成的js.map文件(mapping和sourceContent省略):
区别在于sources属性中明显缺少相对文件夹路径。所以当我用gulp编译时,VSCode找不到.ts文件。所以我想我必须找到一种方法来编译所有的文件,就像tsc对gulp所做的那样。好的,明白了
在sourcemaps.write()之前添加下面的代码可以解决此问题
.pipe(sourcemaps.mapSources(function (sourcePath, file) {
// source paths are prefixed with '../src/'
return '../src/' + sourcePath;
}))
{"version":3,"sources":["bootstrap.ts"],"names":[]}
.pipe(sourcemaps.mapSources(function (sourcePath, file) {
// source paths are prefixed with '../src/'
return '../src/' + sourcePath;
}))