【发布时间】:2017-04-04 11:14:37
【问题描述】:
我有一个项目,其中我的gulpfile.js 相当复杂,因此我想将一些代码分解为typescript。
如何从 gulpfile.js 调用 typescript 代码?
【问题讨论】:
标签: typescript gulp
我有一个项目,其中我的gulpfile.js 相当复杂,因此我想将一些代码分解为typescript。
如何从 gulpfile.js 调用 typescript 代码?
【问题讨论】:
标签: typescript gulp
我们可以看看 typescript 背后的开发团队是如何做到的:
这里是gulpfile.ts - 它在打字稿中 - 完美!
这里是package.json - 注意ts-node 的用法 - 这是解决问题的关键。
基本上就是这样。
【讨论】:
从一开始就使用gulpclass 可能更容易,但这就是我争论的方式:
config.ts
export function getString() : string
{
return "getString() worked!";
}
class Animal{
legs : number = 4;
Sound():string{return "Woof!"}
}
export var dog = new Animal();
tsconfig.json
{
"include": [
"./src/**/*.ts"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
],
"compilerOptions": {
"noImplicitAny": true,
"target": "es5",
"module": "commonjs",
"outDir" :"./src",
}
}
gulpfile.js
var gulp = require('gulp')
var config = require("./src/config.js");
gulp.task('default', function () {
console.log(config.getString());
console.log(config.dog.Sound());
console.log(config.dog.legs);
});
输出
[21:03:43] Starting 'default'...
getString() worked!
Woof!
4
备注
commonjs 模块设置是关键【讨论】: