【问题标题】:How to call a typescript method from gulpfile.js如何从 gulpfile.js 调用 typescript 方法
【发布时间】:2017-04-04 11:14:37
【问题描述】:

我有一个项目,其中我的gulpfile.js 相当复杂,因此我想将一些代码分解为typescript

如何从 gulpfile.js 调用 typescript 代码?

【问题讨论】:

    标签: typescript gulp


    【解决方案1】:

    我们可以看看 typescript 背后的开发团队是如何做到的:

    1. 这里是gulpfile.ts - 它在打字稿中 - 完美!

    2. 这里是package.json - 注意ts-node 的用法 - 这是解决问题的关键。

    基本上就是这样。

    【讨论】:

      【解决方案2】:

      从一开始就使用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 模块设置是关键
      • gulp 文件正在编译 *.ts(此处未显示),这意味着当前 gulp 总是加载一个版本太旧的 tsc。 gulpclass 使用 transpile 语法可能会更好

      【讨论】:

        猜你喜欢
        • 2015-05-25
        • 1970-01-01
        • 1970-01-01
        • 2019-10-08
        • 1970-01-01
        • 2017-10-07
        • 1970-01-01
        • 2017-05-14
        • 1970-01-01
        相关资源
        最近更新 更多