gulp-tsb icon indicating copy to clipboard operation
gulp-tsb copied to clipboard

Do not work with "--allowJs" option

Open qtuan opened this issue 8 years ago • 6 comments

Typescript 1.8 support --allowJs, but gulp-tsb seems to ignore it.

var tsb = require('gulp-tsb');

// create and keep compiler
var compilation = tsb.create({
    target: 'es5',
    module: 'commonjs',
    declaration: false,
    allowJs: true
});

gulp.task('build', function() {
    return gulp.src(['src/**/*.ts', 'src/**/*.js'])
        .pipe(compilation()) // <- new compilation
        .pipe(gulp.dest('dist/'));
});

Correct output files are still generated for respective source *.ts, but none is generated for the *.js files. It works correctly when running tscon the command line.

qtuan avatar Apr 19 '16 07:04 qtuan

@qtuan I have added a test to check that the flag is passed on to TypeScript. The issue must be something else.

jrieken avatar Apr 28 '16 13:04 jrieken

@jrieken Thanks for checking! Could you speculate what is wrong? I couldn't figure it out. Here is my test data:

// src/x.js
export class X {
    doSomeThing() {
        console.log('X doSomeThing');
    }
}
// src/a.ts
import {X} from './x';

export class A {
    constructor(private x: X) {}

    doSomeThing() {
        this.x.doSomeThing();
        console.log('A doSomeThing');
    }
}

Build script is same as in my original post.

> gulp build: only dist/a.js is generated > tsc --target es5 --module commonjs --outDir dist --allowJs src/a.ts src/x.js: both dist/a.js and dist/x.js are generated

Version of installed packages: [email protected], [email protected]

qtuan avatar May 01 '16 06:05 qtuan

@qtuan Can you try to define the outDir when setting up the compiler? I believe TS ignores the allowJs option cos without an output dir it would overwrite the input files.

jrieken avatar May 02 '16 08:05 jrieken

@jrieken Actually I tried outDir, and just try again, but it's still not working.

Another point I forget to mention, that I need to require('object.assign').shim() at top of my build script, otherwise gulp-tsb will throw error.

\typescript-build\node_modules\gulp-tsb\lib\index.js:28
        Object.assign(config, configOrName);
               ^
TypeError: undefined is not a function

My environment: node v0.12.7, [email protected]

qtuan avatar May 04 '16 03:05 qtuan

The builder only allows for .ts and .tsx extensions:

https://github.com/jrieken/gulp-tsb/blob/b040fa10437081824696592f708053075d3fe92d/src/builder.ts#L471

This should be changed to also include .js files when allowJs: true.

getScriptFileNames(): string[] {
	const result: string[] = [];
	const libLocation = this.getDefaultLibLocation();
	const filter = this._settings.allowJs ? /\.?(ts|tsx|js)/i : /\.?(ts|tsx)/i;
	for (let fileName in this._snapshots) {
		if (filter.test(path.extname(fileName))
			&& normalize(path.dirname(fileName)) !== libLocation) {
			// only ts-files and not lib.d.ts-like files
			result.push(fileName);
		}
	}
	return result;
}

zenorbi avatar Jan 06 '17 14:01 zenorbi

@jrieken, this is a great enhancement to the build I believe I'm running into this same issue with it not generating output objects for .js files in the input stream, despite allowJs and outDir being set.

I see @zenorbi has a PR which sounds like it may fix it. What's the likelihood of getting this applied to the published version sometime soon?

Instrumentation from gulp.debug around the gulp-tsb step:

[21:47:35] Starting 'build.test.typescript'...
[21:47:35] src test\unit\applyDecorators.spec.js
[21:47:35] src test\unit\autobind.spec.js
[21:47:35] src test\unit\decorate.spec.js
[21:47:35] src test\unit\deprecate.spec.js
[21:47:35] src test\unit\enumerable.spec.js
[21:47:35] src test\unit\extendDescriptor.js
[21:47:35] src test\unit\lazy-initialize.spec.js
[21:47:35] src test\unit\nonconfigurable.spec.js
[21:47:35] src test\unit\nonenumerable.spec.js
[21:47:35] src test\unit\override.spec.js
[21:47:35] src test\unit\profile.spec.js
[21:47:35] src test\unit\readonly.spec.js
[21:47:35] src test\unit\suppress-warnings.spec.js
[21:47:35] src test\unit\time.spec.js
[21:47:35] src 14 items
[21:47:35] out 0 items
[21:47:35] Finished 'build.test.typescript' after 46 ms

BurtHarris avatar Jul 30 '17 15:07 BurtHarris