Replace Babel with TypeScript compiler #512
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rationale
After using TypeScript in production projects for quite some time, I am pretty confident that it is the correct choice going forward. I think the Bemuse project would benefit a lot from TypeScript:
Type checking makes it less likely for me to write incorrect code.
Make code more self documenting through type annotations.
Amazing editor integration with VS Code (auto-complete, auto-import, find all references, quickfixes, automated refactoring, etc.) These wouldn’t be possible if our code isn’t compatible with TypeScript’s language service.
Good ecosystem and platform for generating docs (TSDoc and friends) with inferred types.
TypeScript can also compile ES201x code down to ES6 and ES5. In addition, TypeScript’s compiler can transpile generators to ES5 without requiring a
regeneratorRuntime
(we don’t use it here since from now we will now target ES6+ browsers only). By going all-in on using TypeScript compiler, we can ensure that VS Code and webpack interprets our code the same way. I also find TypeScript compiler easier to configure than Babel. If I don’t use any Babel plugins, I see no need to directly use Babel in this project anymore. (It’s still used under the hood of some tools.)It turned out we used some Emotion-specific syntax (
css={…}
prop andstyled.div
shorthand). In this PR such custom syntax has been removed.After eliminating the usage of custom syntax that requires Babel plugins (only 3 files affected), the game still works as if nothing happened.
Looking back, I feel that I made the right bet: I used ESnext syntax very conservatively — I waited for it to turn into, say, stage 3 before using it. I don’t use stage 0 stuff in this project.
While Babel is not used here anymore, I still have a deep appreciation for the Babel project (we’ve used it since when it was still called 6to5), as it helped shape the JavaScript language to where we are today.
Scope
This PR:
Makes this project use TypeScript to transpile JavaScript instead of Babel.
Updates the configuration to remove support for non-ES6 browsers.
DOES NOT convert any file to TypeScript; it only replaces Babel with TypeScript compiler.
Remaining issues
Code coverage in Karma
Before: Use
babel-plugin-istanbul
to add instrumentation code.After: Use
istanbul-instrumenter-loader
(which under the hood usesistanbul-lib-instrument
which under the hood usesbabel
andbabel-plugin-istanbul
)