rules_typescript icon indicating copy to clipboard operation
rules_typescript copied to clipboard

Force use of `--target=es6`.

Open SirVer opened this issue 8 years ago • 13 comments

I have some internal tool that only targets the latest version of chrome. For this I want to use es6 stuff like Array.findIndex. However when using these bazel rules, my code does not compile. It seems, the rules internally fall back to using es5 as the target.

How can I configure rules_typescript to compile all my typescript with --target=es6?

SirVer avatar Mar 26 '18 15:03 SirVer

/cc @pdubroy

SirVer avatar Mar 26 '18 15:03 SirVer

@mprobst Halp? Maybe you can still help my typescript woes even now after I have left Google :)

SirVer avatar Mar 27 '18 11:03 SirVer

Haha, you can never escape it ;-)

@alexeagle runs this show. But the basic idea is that you can configure a tsconfig on each ts_library that has the right settings. You'd have a single file in your project with them, and then always reference //:tsconfig.json. That should allow you to set target.

But actually, if you just need to tell TS that Array.findIndex etc exist, I think you should rather configure the libs property instead of target. target=es6 also disables down-transpilation.

See --libs on https://www.typescriptlang.org/docs/handbook/compiler-options.html for the exact options. This maps to the libs property in your compilerOptions. I think you'll want ES6 or ES2015, depending on what polyfills you have available.

mprobst avatar Mar 27 '18 12:03 mprobst

Under bazel, you have no control over --target - rather that's up to whatever rule depends transitively on the ts_library in question. A devserver might request ES5 outputs, while an optimizing bundler might request ESnext. Think of it as rules configuring each other, so the composition of rules is what determines the --target

Martin is right that you need the libs here. I have an issue to make ES2015 libs available by default, along with other "sensible" defaults.

alexeagle avatar Mar 27 '18 15:03 alexeagle

@mprobst Thanks for chiming in!

@alexeagle Can libs be set in the toplevel typescript config too?

And is there an example of an optimizing bundler rule anywhere that I could steal from? I essentially just want a ts_library bundled and rolled up into a ES6 deployable, single file. I used rollup_bundle from the npm sources, which seemed to do what I want except that it outputs ES5.

SirVer avatar Mar 27 '18 18:03 SirVer

You can get an ES6 bundle from the rollup_bundle rule, see https://bazelbuild.github.io/rules_nodejs/rollup/rollup_bundle.html#rollup_bundle

However it's not minified with uglify (which I think doesn't handle ES6 inputs?) Our toolchain currently assumes you want to ship ES5 to users. What's the use case for sending them ES6 instead? It's easy to do in Skylark code, https://github.com/angular/angular/blob/master/packages/bazel/src/ng_rollup_bundle.bzl is an example of a rule that derives from rollup_bundle and does things a little differently.

alexeagle avatar Mar 28 '18 01:03 alexeagle

Thank you! libs solved my immediate problem and I can live with targeting es5 at the moment. This unblocked me, thanks for helping me make progress.

SirVer avatar Mar 28 '18 14:03 SirVer

I ran into a few new issues that aren't solved by adding es6 to libs for me.

error TS1252: Function declarations are not allowed inside blocks in strict mode when targeting 'ES3' or 'ES5'. Modules are automatically in strict mode. and error TS2496: The 'arguments' object cannot be referenced in an arrow function in ES3 and ES5. Consider using a standard function expression.

This only happened 6 times in our 600,000+ lines of TypeScript, but it's still strange that it errors compiling something that is never used, and therefore requires code changes in order to migrate to Bazel.

JaredNeil avatar Apr 12 '18 16:04 JaredNeil

@JaredNeil can you include a repro (like what input .ts triggers these?)

alexeagle avatar Apr 13 '18 17:04 alexeagle

Here is a contrived example to show both errors.

src/main.ts:

if (require.main === module) {
    function foo() {
        const bar: any = () => {
            return arguments;
        };
        console.log(bar("bar", "args"));
    }
    (foo as any)("foo", "args");
}

Errors:

src/main.ts(2,14): error TS1250: Function declarations are not allowed inside blocks in strict mode when targeting 'ES3' or 'ES5'.
src/main.ts(4,20): error TS2496: The 'arguments' object cannot be referenced in an arrow function in ES3 and ES5. Consider using a standard function expression.

Running node out/main.js produces the following output: es6+:

{ '0': 'foo', '1': 'args' }

es5 (this is expected to be wrong because of the compilation error):

{ '0': 'bar', '1': 'args' }

JaredNeil avatar Apr 13 '18 23:04 JaredNeil

Just came accross this issue. Im building Nodejs apps that use metadata and the libs they often have issues with es5 transpiled code, Nestjs being one of them. Also the performance cost of es5 in performance critical scenarios is significant. I've seen a process go from 30% CPU on es5 to 14% on es2018. So for me I need ts_library to be able to target es2017, then es2018 when it's reasonable to upgrade agasint Nodejs. With the module being commonjs.

Toxicable avatar Jun 07 '18 22:06 Toxicable

thread moved to https://github.com/bazelbuild/rules_typescript/issues/216

alexeagle avatar Jun 08 '18 23:06 alexeagle

@Toxicable - anything new regarding your work with Nestjs?.. or is there any progress elsewhere you're following?

TheSpiciestDev avatar May 25 '19 17:05 TheSpiciestDev