【问题标题】:gulp-typescript: Problems using createProjectgulp-typescript:使用 createProject 的问题
【发布时间】:2016-07-23 01:50:28
【问题描述】:

我一直在尝试使用 gulp-typescript 并取得了一定程度的成功,但我遇到了一个小问题。我所有的代码都存储在“src”下,我希望将它们编译为“.tmp”,但不包含“src”。

这是我的代码,我认为问题在于不支持将值(glob)传递给 tsProject.src,因此我得到 /.tmp/src/aTypescriptFile.js 示例

这段代码是我直接从github repo得到的,我真的不明白为什么gulp.src要换成tsProject.src

有什么想法吗?我确实需要合并我的 tsconfig.json 文件。

    let tsProject = plugins.typescript.createProject('./tsconfig.json');
    return tsProject.src('/src/**/*.ts')
        .pipe(plugins.typescript(tsProject))
        .pipe(plugins.sourcemaps.init())
        .pipe(plugins.sourcemaps.write('.'))
        .pipe(gulp.dest('.tmp'));

** 编辑 **

更多信息,我已经设法通过替换

使用 glob 来限制它
             return tsProject.src('/src/**/*.ts')

             return gulp.src('/src/**/*.ts')

现在的问题是我收到关于缺少输入的错误。

        src/testme.ts(4,10): error TS2304: Cannot find name 'require'.

我的 TSCONFIG.JSON 文件在这里,里面有打字。

{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "files": [
    "typings/main.d.ts",
    "src/testme.ts"
  ]
}

【问题讨论】:

    标签: typescript gulp gulp-typescript


    【解决方案1】:

    所有路径都应该传递给 gulp.src -- 来源和类型。

    让我们有一些路径:

    var paths = { 
        lib: "./wwwroot/",
        ts: ["./sources/**/*.ts"],
        styles: ["./sources/**/*.scss"],
        templates: ["./sources/**/*.html"],
        typings: "./typings/**/*.d.ts",
        //svg: "./sources/**/*.svg",
    };
    

    我们可以将源路径数组传递给 gulp-typescript:

    gulp.task("?typescript:demo:debug", function () {
        var tsResult = gulp.src([
              paths.typings,
              // <...some other paths...>
            ].concat(paths.ts))
           .pipe(sourcemaps.init())
           .pipe(ts({
               target: "ES5",
               experimentalDecorators: true,
               noImplicitAny: false
           }));
    
        return tsResult.js
            .pipe(concat(package.name + ".js"))
            .pipe(sourcemaps.write({ sourceRoot: "" }))
            .pipe(gulp.dest(paths.lib));
    })
    

    我路过

    gulp.src([paths.typings, <...some other paths...>].concat(paths.ts))
    

    当然,也可以用更简单的方式来完成:

    gulp.src([paths.typings, paths.ts])
    

    【讨论】:

    • 谢谢我继续接受这一点,因为它对我有很大帮助,但最后我设法通过保留我的原始代码并将 "rootDir": "src" 添加到 tsconfig.json 来解决这个问题.因此,如果有人发现这个,有 2 个解决方案。
    猜你喜欢
    • 1970-01-01
    • 2020-01-28
    • 2019-09-27
    • 1970-01-01
    • 2016-08-20
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多