Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Encore.enableBabelTypeScriptPreset() to "compile" TypeScript with Babel #694

Merged
merged 7 commits into from
Mar 27, 2020

Conversation

Kocal
Copy link
Contributor

@Kocal Kocal commented Feb 7, 2020

See #691, I thought it can be interesting to have a test here.

Using Babel to "compile" TypeScript is faster than using ts-loader or tsc directly, because in fact, it literally remove types annotations.

To continue to check types, you have to run tsc --emitDeclarationOnly manually (or in a CI). But this is not part of the PR.

To migrate an already existing TypeScript app, you just have to configure babel-loader to run over .tsx? file like this:

Encore
-  .enableTypeScriptLoader()
+  .configureLoaderRule('javascript', loader => {
+    loader.test = /.(j|t)sx?$/; // let Babel to run over .tsx? files too
+  })

Install some dependencies: yarn add --dev @babel/preset-typescript @babel/plugin-proposal-class-properties.

And modify your Babel configuration:

{
    "presets": [
        "@babel/env",
+        "@babel/typescript"
    ],
+    "plugins": [
+        "@babel/proposal-class-properties"
+    ]
}

Maybe I can update Encore.configureBabel() and add an option to runs over TypeScript files too... like I did in #574, something like this:

Encore
  .configureBabel(null, {
    typescript: true
  })

I've also changed the legacy import/export (import a = require('...') to import a from '...'). Because it's the legacy way (ES6 imports are very fine) and the Babel TypeScript was not compatible with them:
Capture d’écran de 2020-02-07 22-06-11

EDIT : Added Encore.enableBabelTypeScriptPreset() that do all the job for us! :)

// simple usage
Encore.enableBabelTypeScriptPreset();

// configure TypeScript preset (https://babeljs.io/docs/en/babel-preset-typescript#options)
Encore.enableBabelTypeScriptPreset({
  isTSX: true; 
})

Encore.enableBabelTypeScriptPreset() can not be used aside Encore.enableTypeScriptLoader() or Encore.enableForkedTypeScriptTypesChecking().

Copy link
Collaborator

@Lyrkan Lyrkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that the tested .ts files don't actually contain anything that only works in Typescript. It wasn't a big deal for the previous test cases because we knew everything went through the TypeScript compiler anyway but maybe that should be added for this one?

@Kocal
Copy link
Contributor Author

Kocal commented Feb 13, 2020

I've added a small TypeScript-specific code, and tests pass fine.
It should be ok now 👍

@weaverryan
Copy link
Member

Wait a second :).

If Babel is a better way to compile Typescript - I was just reading this article https://iamturns.com/typescript-babel/ - then why not make it a first-class citizen? Maybe we add a new:

Encore.enableBabelTypeScriptPreset()

And it takes care of everything for you:

  1. The warning on what you need to install
  2. Adding the preset to your Babel config (similar to what we do with enableReactPreset() and (I guess?) also the @babel/proposal-class-properties plugin
  3. Tweak the babel loader test to include .tsx? files

WDYT?

@Kocal
Copy link
Contributor Author

Kocal commented Mar 20, 2020

I don't know if it's a better way to compile TypeScript with Babel, but that's an interesting alternative.

But note that you lose TypeScript power at compilation since every types are removed during transpilation. You need to run tsc manually to check your .ts files (it can be done in CI for example), we should document it.

However, Encore.enableBabelTypeScriptPreset() sounds nice if it can make our life easier 😄

I will try to work on it tonight :)

@Kocal Kocal changed the title Add test for TypeScript "compile" TypeScript with Babel Add Encore.enableBabelTypeScriptPreset() to "compile" TypeScript with Babel Mar 21, 2020
@Kocal
Copy link
Contributor Author

Kocal commented Mar 21, 2020

@weaverryan I've added support to Encore.enableBabelTypeScriptPreset()

@Kocal Kocal requested a review from Lyrkan March 23, 2020 07:24
Copy link
Collaborator

@Lyrkan Lyrkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Kocal,

👍 for an enableBabelTypeScriptPreset() method :)

Really minor comment + maybe a test should also be added in the test/config-generator.js file? (similar to the one called "with configureBabelPresetEnv()")

lib/WebpackConfig.js Outdated Show resolved Hide resolved
@Kocal
Copy link
Contributor Author

Kocal commented Mar 24, 2020

Hi @Lyrkan, thanks for your review.
I fixed the error message and added two tests in config-generator.js.

@weaverryan
Copy link
Member

This is really, really nice work. Thank you for an awesome new feature @Kocal!

@weaverryan weaverryan merged commit 03f217a into symfony:master Mar 27, 2020
@Kocal Kocal deleted the babel-typescript branch March 27, 2020 15:11
@jennevdmeer
Copy link
Contributor

I ran into this trying to help someone on slack. #691 mentions to create a PR/issue for symfony-docs. Has said documentation ever been done? I could not find anything about it.

@weaverryan
Copy link
Member

I am guessing that's correct!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants