ivogabe / gulp-typescript

A TypeScript compiler for gulp with incremental compilation support.
MIT License
831 stars 129 forks source link

Can typescript use incoming sourcemaps? #615

Open elliot-nelson opened 5 years ago

elliot-nelson commented 5 years ago

I'm trying to use gulp-typescript to generate javascript for some typescript files that don't use any module system; it concatenates all the files together and then compiles to produce a single JS file.

The output of this task works perfectly (dist/js/app.js contains the fully tersed javascript with correct source maps). But, when typescript compiler gets an error, it doesn't know about the source maps, so the compiler errors refer to the intermediate concatenated file instead of the original typescript files.

Expected behavior:

[14:05:44] Starting 'build:js'...
src/ts/Game.ts(15,4): error TS2339: Property 'input' does not exist on type 'Game'.
TypeScript: 1 semantic error
TypeScript: emit succeeded (with errors)

Actual behavior:

[14:05:44] Starting 'build:js'...
src/ts/app.ts(37,14): error TS2339: Property 'input' does not exist on type 'Game'.
TypeScript: 1 semantic error
TypeScript: emit succeeded (with errors)

Your gulpfile:

task('build:js', () => {
   const tsproject = typescript.createProject('tsconfig.json');

    return gulp.src([
        'src/ts/ambient.d.ts',
        'src/ts/Game.ts',
        'src/ts/index.ts'
    ])
    .pipe(sourcemaps.init())
    .pipe(concat('app.ts'))
    .pipe(tsproject()).js
    .pipe(terser())
    .pipe(sourcemaps.write())
    .pipe(gulp.dest('dist/js'));
});

Any ideas on how I could have the compiler know about the "pre-compilation" source maps?

ivogabe commented 5 years ago

The compiler already uses incoming source maps, but it only uses them for the generation of the output source maps. I haven't thought about your use case before, but I think it would be a logical extension to also use the incoming source maps to map the source locations of errors. If you would want to implement this, you can do that in Output#getError. The source-map package will probably give you some useful functions to map source locations to the original locations.

However, for you particular use case, you may also want to look at the out option of TypeScript. That may be an alternative which requires a lot less effort to implement.