我正在编写一个副业项目,以便更深入地学习TypeScript和ES6(使用babel)。
我想在TypeScript中使用ES6,因此采用了以下工作流程。
Typescript (ES6) -> Babel (ES6) -> ES5
现在我正在使用Gulp自动化所有这些内容,但我很难正确生成sourcemaps。需要提到的是,这种风格是/r/typescript上的用户建议给我的,因此我甚至不确定它是否可行。
不管怎样,这里是我当前的gulp任务:
var server = $.typescript.createProject('src/server/tsconfig.json');
gulp.task('build', ['vet'], function () {
var sourceRoot = path.join(__dirname, 'src/server/**/*.ts');
return gulp.src('src/server/**/*.ts')
.pipe($.sourcemaps.init())
.pipe($.typescript(server))
.pipe($.babel())
.pipe($.sourcemaps.write('.', { sourceRoot: sourceRoot}))
.pipe(gulp.dest('build/server'));
});
我尝试了许多不同的方法,但都没有成功。当我在VSCode中进行调试时,我的应用程序入口点:
build/server/index.js
正确加载并找到源文件 src/server/index.ts
。然而,当VSCode尝试步入另一个文件,比如
build/server/utils/logger/index.js
时,它会查找 src/server/utils/logger/index.js
,但是它找不到应该寻找 *.ts 文件。所以我做错了什么?或者这是否可能?已经盯着它看了大约 5 小时。所以任何见解都很好。
此外,VSCode 0.9.x 显示
'.../.js' 文件未找到
,而 VSCode 1.0 只是默默失败。这是我的 tsconfig.json,它由
$.typescript.createProject()
传递。{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"target": "ES6",
"sourceMap": true,
"outDir": "build/server"
}
}
.babelrc
{
"presets": ["es2015"]
}
这里是相关的npm包
"devDependencies": {
"babel-polyfill": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"gulp-babel": "^6.1.0",
"gulp-sourcemaps": "^1.6.0",
"gulp-typescript": "^2.9.2"
}
编辑:我对gulp-sourcemaps进行了一些调查,发现在不使用babel的情况下,sourcemaps可以正常工作。(删除了所有无关信息)
src/server/up/up2/four.ts - 无Babel
{
"history": [ "/home/user/code/test/src/server/up/up2/four.js" ],
"base": "/home/user/code/test/src/server/",
"sourceMap": {
"sources": ["up/up2/four.ts"],
"file": "up/up2/four.js"
}
}
注意,sourceMap.sources
中列出了正确的源文件 up/up2/four.ts
现在这里有一个示例,当我将 gulp-babel 添加到任务中时。
src/server/up/up2/four.ts - 使用 Babel
{
"history": [ "/home/user/code/test/src/server/up/up2/four.js" ],
"base": "/home/user/code/test/src/server/",
"sourceMap": {
"sources": ["four.js"],
"file": "up/up2/four.js"
},
"babel": {
"...": "..."
}
}
请注意,
sourceMap.sources
现在错误地显示了 four.js
而不是 typescript 文件。有趣的是,只要 typescript 文件位于根目录下的
src/server
,它们就可以很好地构建源映射。
src/server/two.ts - 使用 Babel
{
"history": [ "/home/user/code/test/src/server/two.js" ],
"base": "/home/user/code/test/src/server/",
"sourceMap": {
"sources": ["two.ts"],
"file": "two.js"
},
"babel": {
"...": "..."
}
}