I request that Parcel 2 require less manual configuration of Babel to work with Typescript.
In Parcel 1, Typescript mostly worked for me out of the box the way that I wanted it to, in a "zero configuration" way. It was a long time before I even realized that Parcel was using Babel under the hood to transpile, rather that tsc.
In Parcel 2, I immediately had a bunch of difficulties with my Typescript code which tool a long time to resolve, and it took me a lot further in to the weeds of configuring Babel and installing Babel plugins than I would have liked.
Specifically, my code uses "decorators", which did not work out-of-the-box with Parcel 2.
The error message about this was reasonably clear about the problem but not terribly clear about the solution.
@parcel/transformer-babel: Support for the experimental syntax 'decorators-legacy' isn't currently enabled (6:5):
From this, and doing some googling, I gathered that I needed to have a .babelrc file which contained the following in the plugins section:
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose" : true }]
but unfortunately when I did this I started getting syntax errors:
Uncaught SyntaxError: Unexpected token '<'
So it took a bunch more googling to figure out that I would need to add @babel/plugin-transform-react-jsx to the plugin list. Since JSX worked correctly out-of-the-box, it took me a while to realize that I had inadventently broken JSX by creating the .babelrc. By creating the .babelrc to solve the decorators problem, I lost the settings which allowed it to process JSX.
I gather from #3911 that this behaviour has changed between Parcel 1 and 2, where in parcel 1 it merged your .babelrc and the Parcel default one where in Parcel 2, the project's .babelrc will override the default one. While I can see how the old behaviour might have created some issues with incorrect merging, the new behaviour is also troubling because I lost good defaults like the JSX processing, and I had to spend extra time researching how to re-add that functionality.
Here are a few possibilties on how the situation could be improved:
tsconfig.json to see if the experimentalDecorators setting is true, and then configure babel accordingly. tsc as the default transpiler for Typescript, to avoid issues due to Babel ignoring tsconfig.json and/or transpiling differently than tsc would. (debated extensively in #4022). I'm supportive of both sides of this debate. I see the performance benefit of using Babel by default, but also see that Babel is not looking at all like a "zero configuration" option for my Typescript code the way that it is set up in Parcel 2.tsc normally..parcelrc snippet that is in there which has "@parcel/transformer-typescript-tsc" added to the transformers section. It is not clear what that is for, but I mistakenly thought it would solve some of my issues, when in fact it did not seem to be required.(With Babel 7.10, class properties will be enabled by default)
We already have this
{
"presets": ["@parcel/babel-preset-env"]
}
which correcly applies the browserslist config from Parcel into Babel. We should somehow extend this mechanism to cover flow/ts/jsx as well.
So for now, you need:
{
"presets": ["@parcel/babel-preset-env", "@babel/preset-typescript"],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }],
["@babel/plugin-transform-react-jsx"]
]
}
(I'm not sure what the issues with that approach were, but is the new approach really the lesser of two evils?)
One example: if you specified a preset that included the jsx plugin with a custom pragma (e.g. a CSS-in-JS framework), Parcel's default JSX would override that pragma.
And: there was no way to disabled preset-env.
Typescript page (parcel2-docs.now.sh/recipes/typescript) could be more detailed about babel configuration issues. A lot of Typescript users aren't really familiar with using Babel to transpile, since they use tsc normally.
Noted
Add an explanation of the .parcelrc snippet that is in there which has
As the first line of that page tells you, "TODO" ๐
Most helpful comment
(With Babel 7.10, class properties will be enabled by default)
We already have this
which correcly applies the browserslist config from Parcel into Babel. We should somehow extend this mechanism to cover flow/ts/jsx as well.
So for now, you need:
One example: if you specified a preset that included the jsx plugin with a custom pragma (e.g. a CSS-in-JS framework), Parcel's default JSX would override that pragma.
And: there was no way to disabled preset-env.
Noted
As the first line of that page tells you, "TODO" ๐