-
Notifications
You must be signed in to change notification settings - Fork 492
Add full support for generator functions #194
Conversation
This should be supported down to ES3 using the Have you tried that flag? And if so, which problems occurred? |
@DorianGrey I tried Maybe the problem is related with the following sentence on the TS 2.3 page:
|
Ah, I see - The error message indicates an object containing everything that an iterator contains, The babel polyfill includes a shim for this, aside from (potentially) others. Would you mind testing another shim like the one from Just want to make sure that there is no "simpler" way than integrating |
@DorianGrey I'll look into it. (I'm super busy this week though, so this might take a while.) |
@DorianGrey Okay, I've enabled We could now switch the TypeScript target back to ES5, and remove Babel from the TypeScript pipeline. Interestingly, that slightly increases the bundle size from 120KB to 125KB. I'm not sure what the pros and cons are for having Babel in the TypeScript pipeline. |
Glad it worked - seems that
Seems there are differences in how the code is transpiled, and how the required helper functions a picked up. By default,
The major downside - at least for larger projects - is the increased (re-)build time, since the whole code has to be processed twice. On the other hand, |
@DorianGrey I've just removed Babel from the TypeScript pipeline. The build time on my local machine went down from 13s to 12.2s. I'm actually involved in a 50kloc Angularjs project whose where folks just added Babel to the typescript pipeline. I'm not sure about the build times, but they didn't get much worse I think. The people responsible for the change didn't give a clear list of benefits. But one benefit was that certain unit test are now running in Phantom. Of course, Phantom is irrelevant for React. I'm now in charge of an upcoming major React project, and my hope is to avoid ejecting as long as possible - to avoid needless maintenance and focus on actual features. That's the deeper reason why I made this fork. I can't tell yet if there will be a reason to re-add Babel to the TypeScript pipeline. May I ask: do you have the rights to merge PRs here, or have you just been a good sport? |
CRA upstream is babel based. I'm not sure there's a good reason to remove babel. There are plenty of reasons to use babel. Phantom JS is EOL. |
No, I've just contributed some stuff and take a look at some issues and PRs when possible - I'm using this fork personally, thus I'm interested in its progress, and attempt to help out where possible.
It always depends on the particular setup, LoC, code complexity, caching configuration, etc. In most projects I've tried a chain of
Since the plugin system is not yet available, you might quickly face issues or requirements that will force you to do so - e.g. using a particular loader or webpack plugin. However, maintaining an ejected project isn't that heavy in maintenance - I'm using these myself, and the average update took less than 10 minutes for the last versions: Changes to the setup mostly affect the webpack configuration, which is (in general) easy to merge with its updated counterpart. |
@bootstraponline To avoid misunderstanding: the repo to which this PR belongs has Babel, but it doesn't use Babel to postprocess what comes out of the Typescript compiler. So the discussion here isn't about removing Babel. It's about whether to add Babel as a backend to Typescript. |
@DorianGrey Thanks for those helpful infos! My apprehension about maintaining an ejected project isn't about updating the ejected packages via yarn; what I'd like to avoid is loosing touch with updates to CRA and failing to benefit from improved solutions there. |
Even without ejecting the project, you will still have to deal with updates to files generated for your project, e.g. the service worker registration script. As long a you don't reorganize the ejected structure completely, updates are simple to handle:
The last merge on one of my playground projects was this one: Only took a couple of minutes. Main purpose for ejection in my case were several plugins to use and the modification of some loader-chains. I also deduped the webpack configs a bit, since there are a lot of duplicates in dev vs. prod config in upstream. Still thinking about using webpack-blocks for further optimizations on. |
@DorianGrey Very helpful suggestion, thanks! |
@cfuehrmann did you original issue get fixed by updating your |
@wmonk Note quite. To make my generator-function example work in IE11, I also had to add the shim "core-js/es6/symbol" to the two webpack configs. But that, together with "downlevelIteration" in the tsconfig, sufficed to solve my issue. |
Even though generators are not that uncommon, I'm not sure if using the polyfill and the compiler option should be the default. Both increase the resulting bundle size, and the compiler option has a negative impact on the runtime performance. However, the various
since |
I agree about the increase in bundle size - though that's "only" around 8% (from 120KB to 130KB). As for the runtime performance, I see no downside: before the change, generator functions didn't work at all (because of the ES5 targeting without downlevelIteration), and now they work - just not at ES6 speed. So, no previously working feature got slowed down. Or am I missing something? |
The runtime performance gets a bit worse because of additional helpers used and introduced, but I have to admit that I'm not sure about concrete numbers on how bad the would be. However, the generated code would not be slower than its counterpart generated by Thus, I agree that feature parity should be favorable in this case. |
Synced with upstream. And I just found out: if a user wants full generator support for IE11 without the changes in my PR, they could also add |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with this semantically, just stated my last comment in an explicit in-line review.
Only some structural stuff.
@@ -41,6 +41,7 @@ module.exports = { | |||
// This means they will be the "root" imports that are included in JS bundle. | |||
// The first two entry points enable "hot" CSS and auto-refreshes for JS. | |||
entry: [ | |||
'core-js/es6/symbol', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd favor to add this polyfill to the general polyfills
files instead of this array, esp. regarding maintenance. Would be good to have some kind of comment or a short description of why this is included.
@@ -64,7 +64,11 @@ module.exports = { | |||
// You can exclude the *.map files from the build during deployment. | |||
devtool: shouldUseSourceMap ? 'source-map' : false, | |||
// In production, we only want to load the polyfills and the app code. | |||
entry: [require.resolve('./polyfills'), paths.appIndexJs], | |||
entry: [ | |||
'core-js/es6/symbol', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
@DorianGrey I don't know from which repo these polyfills come. Who could add "core-js/es6/symbols" to them? |
The polyfills file referenced in these configs is this one (it's picked up via a relative path by them):
|
Sorry for the long silence. My immediate problem has been solved, by simply importing "core-js" from the JavaScript entry point, "index.js". I'm now no longer sure if baking in (a superset of) the required polyfills is the right way to go. As far as I'm concerned, this PR can be closed. Shall I close it? |
This PR was triggered by a need to use generator functions from TypeScript. That's possible for target ES6, but not ES5. To make things work in ES5, I added "babel-loader" to the TypeScript pipeline. Plus, "babel-polyfill" had to be added to make generator functions work in Internet Explorer.
To see that this works, create a new app with the create-react-app command, and change the App.tsx like below. Then one can see that the spread operator works even in Internet Explorer. (Certainly in IE 11.)