Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Migrated to React 0.14.3#63

Merged
mhegazy merged 2 commits into
microsoft:masterfrom
johnnyreilly:master
Nov 25, 2015
Merged

Migrated to React 0.14.3#63
mhegazy merged 2 commits into
microsoft:masterfrom
johnnyreilly:master

Conversation

@johnnyreilly
Copy link
Copy Markdown
Contributor

This PR ports react 0.13.3 to 0.14.3 which should handle the issues raised in #61

All works fine / tests pass. One quirk; I'm having to capture the React variable in each of the JSX tests and in the main.tsx like so:

import * as React from 'react';

// ....

const __react = React; // only in place to prevent React being purged from dependencies as not used directly

These files do not directly use the imported React and so, without the workaround, React is not loaded. I think this is the TypeScript compiler performing an optimisation here; saying "well, they didn't use React directly so they must not need it". I initially thought this was a problem with ts-loader but came to the conclusion it was probably tsc related. Take a look at this demo in the playground. You can see that React is not considered a dependency by TypeScript unless the __react = React is uncommented.

@jbrantly would you be able to advise if there is a better way around this than what I've done here? (I know you pretty much handled the port of React typings to 0.14 so I thought it might be worth asking.)

Just to be clear, the PR is safe to merge but it would be good to find out if the workaround could be replaced with something a little nicer. I suspect it can't

@DanielRosenwasser
Copy link
Copy Markdown
Member

@mhegazy and @RyanCavanaugh

Thanks @johnnyreilly!

This isn't an optimization as much as it is us trying to give the user more control over this. If we didn't perform this step, there'd be no way to say "I only need the typings from this import." On the other hand, with the current behavior, you can always force an import by

  • Adding an extra blind import:

    import * as React from "react";
    import "react";
  • Using an imported entity as an expression (like in the following statement expression):

    import * as React from "react";
    React;

Actually, would you mind shortening your fix to one of the above forms just because it's a bit cleaner?

@johnnyreilly
Copy link
Copy Markdown
Contributor Author

Done. I had to use approach 2 as approach 1 didn't work. Any idea why that might be?

@DanielRosenwasser
Copy link
Copy Markdown
Member

That's weird - approach 1 didn't work?

I pulled down the changes and tried using an import "react"; and just running tsc from `src.. The compiled output maintained the import. If I'm not mistaken, this sounds like a potential problem with the other tools. A sanity check from @mhegazy and @RyanCavanaugh would help.

@johnnyreilly
Copy link
Copy Markdown
Contributor Author

Curious - ts-loader is doing the TSC invoking so @jbrantly might have a view.

@johnnyreilly
Copy link
Copy Markdown
Contributor Author

Try approach 1 on the repo and run npm run serve to see the issue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

import "react";

instead ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

nope Can't find variable: React results

@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Nov 23, 2015

Just one comment, using import "react" instead of the use as value.

@jbrantly
Copy link
Copy Markdown

@johnnyreilly I've never run into the issue I don't think because my JSX is always within a React component (so extending React.Component).

I wonder if the lack of jsx compiler option in the test tsconfig.json has anything to do with it. Per @RyanCavanaugh here when using jsx: "react" any JSX is supposed to act as if React was referenced. I can understand why that might not be the case for jsx: "preserve".

@johnnyreilly
Copy link
Copy Markdown
Contributor Author

Hi @mhegazy

Just one comment, using import "react" instead of the use as value.

I did try this but it didn't work I'm afraid. Please feel free to prove me wrong!

@DanielRosenwasser
Copy link
Copy Markdown
Member

I've repro'd the issue on my side as well, it's just that somewhere in the pipeline, the import "react" isn't getting respected.

@johnnyreilly
Copy link
Copy Markdown
Contributor Author

Import can't get no respect? That makes Aretha sad 😄

@jbrantly
Copy link
Copy Markdown

The difference between React; and import "react" can also be seen in the playground example given earlier by @johnnyreilly. If you use React; then React is both added to the list of dependencies AND injected into the module factory function. If you use import "react"; then it's added to the list of dependencies but not injected.

This actually makes sense to me. When I think of import "react" I'm thinking of importing a module for a side-effect, not somehow saying that a completely different imported variable (albeit from the same module) is somehow "used".

I don't think there is an actual bug here, although it's a bit of a weird situation. This is handled correctly with --jsx react because TS knows that the JSX is being compiled to React.createElement expressions so it knows to keep the imported variable. With --jsx preserve there's no guarantee that React is being used at all.

I think the correct course of action is to either use React; or just use --jsx react.

@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Nov 25, 2015

👍

mhegazy added a commit that referenced this pull request Nov 25, 2015
@mhegazy mhegazy merged commit 488d8ea into microsoft:master Nov 25, 2015
@mhegazy
Copy link
Copy Markdown
Contributor

mhegazy commented Nov 25, 2015

thanks!

@johnnyreilly
Copy link
Copy Markdown
Contributor Author

Pleasure. When Babel fix the sourcemaps in v6 I intend to upgrade the sample to use Babel 6 instead of v5. Just waiting on a PR to be merged here:

babel/babel#3108

@johnnyreilly johnnyreilly mentioned this pull request Dec 11, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants