根据您的问题,我不完全确定您要解决什么问题。你以前用过gulp吗?您只是在 VSCode 中遇到问题,还是在学习如何使用 Gulpjs?如果你以前用过 Gulpjs,你用过手表吗?我有一个适用于 TS 文件的示例。我相信这将为您指明正确的方向。
如果你有这样的项目结构:
project
.settings
tasks.json
tsconfig.json
src
app.ts
package.json
gulpfile.js
tasks.json 看起来像这样:
{
"version": "0.1.0",
"command": "gulp",
"isShellCommand": true,
"tasks": [
{
"taskName": "default",
// Make this the default build command.
"isBuildCommand": true,
// Show the output window only if unrecognized errors occur.
"showOutput": "silent"
}
]
}
tsconfig.json 看起来像这样:
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": "true",
"removeComments": "true",
"target": "ES5"
},
"files": [
"app.ts"
]
}
package.json 看起来像这样:
{
"name": "typescript-concepts",
"version": "0.0.0",
"dependencies": {
"gulp": "latest",
"gulp-typescript": "latest",
"gulp-watch": "latest"
}
}
gulpfile.js 看起来像这样:
var gulp = require('gulp');
var compileTypescript = require('gulp-typescript');
var tslint = require('gulp-tslint');
var watch = require('gulp-watch');
var tsProject = compileTypescript.createProject('./.settings/tsconfig.json');
gulp.task('compile-ts', function() {
return tsProject.src() // instead of gulp.src(...)
.pipe(compileTypescript(tsProject))
.js
.pipe(gulp.dest('dest'));
});
gulp.task('default', ['compile-ts'], function() {
return gulp.watch(['./src/app.ts'], ['compile-ts']);
});
你必须在你的盒子上安装 npm(这是 node.js 安装的一部分)。您需要导航到您的项目目录(在命令提示符中)并键入 npm install。
安装完所有 npm 模块后,您应该可以在 VSCode 窗口中键入 Ctrl+Shift+B。该项目应该构建一个 app.js 文件并将其放置在 dest 目录中(如果之前没有创建它,则应该创建它)。您可以对 app.ts 文件进行更改,保存更改后,您应该会看到 app.js 重新生成。
gulp.watch 命令将一组文件路径作为其第一个参数来监视更改,并使用一组 gulp 命令在检测到任何更改时重新运行。
您可以查看here,了解如何在 gulp 构建过程中设置 less。我不知道您是否想一次观看所有这些文件,或者您是否想根据您一次处理的任务设置两个不同的观看任务(ts vs less)。我希望这会有所帮助!
此外,这将创建一个长时间运行的任务。我不确定如何从 VSCode 终止此任务。我目前做的是运行 Ctrl+Shift+B ,它会在 VSCode 编辑器顶部给你一个小提示,说一个任务已经在运行,让你从那里终止任务。我确信有更好的方法可以做到这一点,但我还没有想出来。