ivogabe / gulp-typescript

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

TypeError: Cannot destructure property `extends` of 'undefined' or 'null'. #604

Closed bennycode closed 5 years ago

bennycode commented 5 years ago

Description:

The gulp-typescript docs have written that you can replace gulp.src(...) with tsProject.src() to load files based on the tsconfig file. I tried to do that but it does not work. My gulp task works uses gulp.src and works. My gulp task fails uses tsProject.src and fails. 😢

Tested with:

Expected behavior:

Compiled output

Actual behavior:

Compilation error:

TypeError: Cannot destructure property extends of 'undefined' or 'null'.

gulpfile.js:

const gulp = require('gulp');
const ts = require('gulp-typescript');

const tsConfig = {
  declaration: true,
  files: 'test.ts',
  outDir: 'dist'
};

const tsProject = ts.createProject(tsConfig);

gulp.task('works', function() {
  return gulp.src('test.ts')
    .pipe(tsProject())
    .pipe(gulp.dest(tsConfig.outDir));
});

gulp.task('fails', function() {
  return tsProject.src()
    .pipe(tsProject())
    .pipe(gulp.dest(tsConfig.outDir));
});

test.ts

const text: string = 'Hello World!';
ivogabe commented 5 years ago

Hi Benny, from your configuration, it seems you're not using the tsconfig file and thus cannot use the .src() function. You must use a tsconfig.json file to create the tsProject, like so:

const tsProject = ts.createProject('./tsconfig.json');

Can you verify whether that works for you?

bennycode commented 5 years ago

Thanks for your super fast reply!

When using ts.createProject('./tsconfig.json') it works! But now I am getting a little confused because your main.d.ts indicates that settings can be given as first parameter to ts.createProject.

I am generating a boilerplate setup for TypeScript and I would like to work with configuration objects rather than file paths. Would it be possible to pass the JSON object of tsconfig.json to ts.createProject instead of a file path? Technically it should not make much difference. What do you think?

It would help me a lot if I could do something like:

// Requiring original config
const tsConfig = require('./tsconfig.json');

// Do some modifications on the original config, i.e.:
tsConfig.compilerOptions.declaration = true;

// Use modified config with "gulp-typescript"
const tsProject = ts.createProject(tsConfig);
ivogabe commented 5 years ago

createProject indeed also accepts a settings object. However, the .src method can only be used when you passed it a tsconfig file. We need to have the file path of that tsconfig file to be able to resolve files.

For the use case of modifying some options, you can specify those as the second argument:

const tsProject = ts.createProject('./tsconfig.json', { whatever: true });