-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Exclude react from bundle, and include it directly from the layout #1275
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
@YoussefTaghlabi Do you add <script> tag in your page?
|
@xusiyuan841028 Yep, I do. But webpack is looking for |
Solution:
In addition, you can also use the ProvidePlugin in conjunction with the dummyReact.js. |
Thanks @IngwiePhoenix , the following did work indeed:
I had to remove My next question, how can I make use of |
Straight from the docs:
http://webpack.github.io/docs/shimming-modules.html#order-of-loaders I use it exactly like that for jQuery and other modules. Use something like this for React, maybe.
ProvidePlugin correctly resolves aliases too. So if you just add the plugin like os, itll "just work" :) |
Perfect! |
If you wonder how exactly this affects your code: var $ = require("jquery");
reuqire("jquery-plugin");
require("other-plugin");
code();
// VS.
code(); Combining Alias and Provide, you are able to pretty much reduce the amount of "startup" code you write - effectively saving yourself lines of code (which in turn saves bytes with Uglify). |
@YoussefTaghlabi Very welcome. =) |
That works really well!! |
I have no idea how to achieve this, sorry. :) But this is actually a graceful soltution imo. But if you ever find something, feel free to post it. |
I sure will. Thanks for your help! |
{
...
externals: {
// Use external version of React
"react": "React"
},
...
} should be enough |
either external or ProvidePlugin works in conjunction with alias. |
#1275 (comment) should be in the docs, surely? #1275 (comment) alone doesn't exclude React from my bundle. |
Alright, so after playing around, my issue was that because ReactDOM was not being excluded, React was being bundled along with ReactDOM. I needed to add ReactDOM as an external, in addition to React: {
...
externals: {
// Use external version of React
"react": "React",
"react-dom": "ReactDOM"
},
...
} This is applicable since ReactDOM was made a separate dependency when mounting a component (React 0.14.x?). I don't require any |
If you want to import your component as a library in another project already importing react instead of having a script tag, you must do this : externals: {
react: 'umd react',
'react-dom' : 'umd react-dom'
} |
@larrybotha Does this mean that you are loading in ReactDOM externally, then? When I bundle per your comment I get "ReactDOM is not defined". I'm having the same issue of ReactDOM requiring React, but would rather not have to fetch ReactDOM via CDN as it requires an extra roundtrip. |
I'm not sure what is going on, but I'm getting react included in my bundle despite explicit exclusion. I've tried every-which-way to stop this, but nothing seems to be working.
Yet when loading the app I still get two logs of |
@aft-luke Could you open a question about that at Stack Overflow with more info? You could also check the resulting bundle through the analyze tool to see what's bringing React in. You could have something like |
@bebraw thanks for the hint about the analyze tool. I was able to deduce that some of the react addons directly require react. CSSTransitionGroup and TransitionGroup, for example, were the culprits in this case. Changing my externals to this seemed to solve the problem:
|
@aft-luke Great to hear! |
So which is it.. externals: {
react: 'react',
'react-dom' : 'react-dom',
} or externals: {
react: 'React',
'react-dom' : 'ReactDOM',
} Seeing the following in the console.
Curious because different solutions seem to be working for different folks (presumed via the thumbs up) and I am not understanding why.. |
Okay so here is the solution that worked for me.. all credits to @Download In webpack.config.js you can apparently pass an object.. externals: {
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
},
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom'
}
} |
Thanks for the credits @buildbreakdo and glad it's working for you now. Basically what is happening here is that there are different conventions on different systems: On the web, libraries normally export to the Many people that are writing React applications are only using it on the client side. They set Edit: |
Just to add my two cents, this did the trick for me: output: {
...
libraryTarget: 'umd'
}, |
Another two cents: If you feel like you've done all the right things and it still doesn't work, maybe it's actually not your fault but one of your dependencies includes a react addon (e.g.
|
If you are using lerna and "monorep", and building packages with webpack - this may help: alias: {
react: path.resolve(__dirname, "node_modules/react")
} |
@jaredreich , Work for me |
I'm getting the below error when using
|
I'm including React directly from my layout, and I would like webpack to exclude it from the bundle, and make
react
refers to the window global objectReact
I'm using the following:
react
gets excluded from the bundle, but client side I'm gettingUncaught Error: Cannot find module "react"
.Any hints?
The text was updated successfully, but these errors were encountered: