gulp-typescript: Problems using createProject
Asked Answered
L

1

1

I have been trying to use gulp-typescript with some degree of success but I have a small issue. All my code is stored under 'src' and I want these to be compiled to '.tmp' but without the 'src' being included.

here is my code, I think the issue is that passing a value (glob) to tsProject.src isn't supported so I get /.tmp/src/aTypescriptFile.js for example

This code I got directly from the github repo, what I really didn't understand is why gulp.src is replaced with tsProject.src

Any ideas ? I do really need to incorporate my tsconfig.json file.

    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'));

** EDIT **

More info, I have managed to confine it using a glob by replacing the

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

with

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

problem is now that I get an error about missing typings.

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

my TSCONFIG.JSON file is here, which has the typings in there.

{
  "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"
  ]
}
Lissie answered 3/4, 2016 at 15:5 Comment(0)
V
1

All paths should be passed to gulp.src -- sources and typings.

Let us have some paths:

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

We can pass an array of source paths to 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));
})

I'm passing

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

but of course, it can also be done in a simpler way:

gulp.src([paths.typings, paths.ts])
Virgina answered 3/4, 2016 at 17:41 Comment(1)
Thanks I am going ahead to accept this as it helped me quite a bit but finally I managed to fix the issue by keeping my original code and adding "rootDir": "src" to the tsconfig.json. So in case anybody finds this, there are 2 solutions.Lissie

© 2022 - 2024 — McMap. All rights reserved.