Skip to content

Conversation

@dphaener
Copy link

With the update to Babel 6, modules are being exported differently. For
instance, the default module is now under a default key. This is
breaking things when using an expose loader with webpack. This adds a
check for that before trying to import the module.

@rmosolgo
Copy link
Member

Interesting, could you share a couple files that have the issue? Like, one file that defines a component and another that puts the component in the global namespace?

I wonder if, to support this case, it could be:

var constructor
// First, try accessing the name globally: 
constructor = window[className]

// If that doesn't work, try eval (to handle "My.Namespaced.Component")
if (!constructor) { 
 constructor = eval.call(window, className)
}

// Then, if it has a `.default` attribute, we assume it's exported from Weback in that way: 
if (constructor && constructor.default) {
  constructor = constructor.default 
}

// Assume className is simple and can be found at top-level (window).
// Fallback to eval to handle cases like 'My.React.ComponentName'.
var constructor = window[className] || eval.call(window, className);
var constructor = window[className].default || window[className] || eval.call(window, className);
Copy link
Member

Choose a reason for hiding this comment

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

if window[className] returns undefined, will this throw TypeError: Cannot read property 'default' of undefined ?

Copy link
Author

Choose a reason for hiding this comment

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

Yup, it sure would. Sorry about the delay on this. I think the solution you outlined above would work just fine. I'll update and resubmit.

dphaener added 2 commits April 29, 2016 10:20
With the update to Babel 6, modules are being exported differently. For
instance, the default module is now under a default key. This is
breaking things when using an expose loader with webpack. This adds a
check for that before trying to import the module.
This uses @rmosolgo's suggestion for safely grabbing the class
constructor from the window. It moves all of the logic into a private
function that is called to set the constructor for each className.
@dphaener
Copy link
Author

@rmosolgo Cleaned this up a bit and rebased current master. Hopefully this will pass CI. 👍


// If that didn't work, try eval
if (!constructor) {
constructor = eval.call(window, className)'
Copy link
Member

Choose a reason for hiding this comment

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

oopsie '

@dphaener
Copy link
Author

And to answer your other question @rmosolgo I have bundle files that look something like this:

require('expose?React!react');
require('expose?Navigation!../javascripts/application/views/shared/navigation');
require('expose?Flash!../javascripts/application/react-components/flash');
require("font-awesome-webpack!../stylesheets/font-awesome.config.js");
require('../stylesheets/bundles/breweries');

Then it's being bundled up by webpack with that file as the entry point. I'm exposing, in this instance, the Navigation and Flash components globally, but they get exported with the default object. This is the new Babel behavior. I tried many things to get it to work without using this workaround, but short of going back to Babel 5 I haven't been able to figure out anything.


// Get the constructor for a className
// Not a part of the public API
function getConstructor(className) {
Copy link
Member

Choose a reason for hiding this comment

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

this function could be really useful for people using NodeJS-type builds, could you make it part of ReactRailsUJS?

(That way, people could override getConstructor to hook into their build systems!!)

Copy link
Author

Choose a reason for hiding this comment

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

Oh I didn't think about that. Great idea! Coming...

Copy link
Member

Choose a reason for hiding this comment

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

Am I reading the diff right? It looks like this function is in the source twice, but we only need the one that's attached to ReactRailsUJS, right?

dphaener added 3 commits April 29, 2016 10:26
This moves the getConstructor function into the public API, allowing
anyone to override it if they have custom build behaviors that they need
to account for.
@dphaener
Copy link
Author

@rmosolgo Ok then. Guess I missed that. Whoops.

@rmosolgo
Copy link
Member

Wow, sorry I left this sitting around for so long! I was just thinking "I should write some docs for that new component lookup function" and then I didn't find it on master 😬

Thanks for this!

@rmosolgo rmosolgo merged commit 53d9ad7 into reactjs:master Jun 22, 2016
@rmosolgo
Copy link
Member

Shipped in 1.8.0!

@dphaener
Copy link
Author

:shipit: 👍

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.

2 participants