Giter Club home page Giter Club logo

Comments (9)

mbektimirov avatar mbektimirov commented on May 20, 2024

@gaearon why don't replace React.creactClass with hmr proxy on top of the app? RegExp check is a mess. Even if replace it with a parser like esprima or graspjs the variable name is unpredictable.

from react-hot-loader.

mbektimirov avatar mbektimirov commented on May 20, 2024

I think it is possible to inject hmr code once in app entry point instead of patching every module. If you don't have time for this I could try to manage the issue and send a PR.

from react-hot-loader.

gaearon avatar gaearon commented on May 20, 2024

You can't inject it on top because each module needs its own module-specific createClass function.

You can probably do following:

  1. Instead of replacing regex, monkey-patch React.createClass at the beginning of each module
  2. Restore React.createClass at the end of each module (otherwise first require in the file will mess it up)
  3. Currently we opt out of hot reload for modules that don't have regex matches. If we say goodbye to regex, we must only enable hot reload (if module.hot code) if monkey-patched createClass was called during module definition.

from react-hot-loader.

mbektimirov avatar mbektimirov commented on May 20, 2024

Confused about point 3. Why need this check if patched createClass will be called explicitly every time we want create a component?

from react-hot-loader.

gaearon avatar gaearon commented on May 20, 2024

Modules opt in to HMR by calling module.hot.accept. We don't want to opt-in to HMR for files that contain no components. For plain JS files, it's best to let HMR bubble up dependency tree because for most React apps, there is a component somewhere on the too, and it is HMR-capable by itself. As a consequence, side-effect free plain JS modules will get hot reloading as a bonus by the virtue of being require from HMR-capable components.

However, if we just put module.hot.accept in every file (as opposed to those containing createClass calls), Webpack will think that these files are truly HMR-capable (which they are not), and won't bubble reload up when they change.

Right now we add module.hot.accept when regex matches, but if we do away with regex, we must have other way to know if module is “componentish” or not. One way to do that is to check if monkeypatched createClass was called by the end of module definition. If not, probably this module contains no components, and therefore we shouldn't emit module.hot.accept, or we lose the benefits of HMR bubbling.

Finally, this is only a problem if regular (non-component) files go through this loader. This is often the case. Personally, I use JSX transforms even on plain JS files so I can use Harmony syntax and some JSX occasionally in helpers.

from react-hot-loader.

gaearon avatar gaearon commented on May 20, 2024

@mbektimirov

1.0.0-alpha no longer uses the regex. Instead, only module.exports is hot-enabled implicitly.

However, there is an API to hot-enable any class (e.g. a private one, or one inside a function):

var SomeComponent = React.createClass({ ... }); // or ES6 class in the future

if (module.makeHot) {
  SomeComponent = module.makeHot(SomeComponent);
}

from react-hot-loader.

mbektimirov avatar mbektimirov commented on May 20, 2024

Great, looks much better now. Will you drop components store by displayName?

from react-hot-loader.

gaearon avatar gaearon commented on May 20, 2024

Yeah, there's no need for that now.

Now it works like this:

  • module.exports is always stored implicitly, regardless of displayName
  • call to module.makeHot(SomeComponent) by default uses displayName or name (ES6), but you can specify any other key as a second parameter

from react-hot-loader.

gaearon avatar gaearon commented on May 20, 2024

Closing, as this will be available in 1.0.

from react-hot-loader.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.