You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exclude react from bundle, and include it directly from the layout #1275
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 object React
I'm using the following:
{
...
externals: {
// Use external version of React
"react": "React"
},
plugins: [
new webpack.IgnorePlugin(/react/)
]
...
}
react gets excluded from the bundle, but client side I'm getting Uncaught Error: Cannot find module "react".
Any hints?
Reacted by ibrahim, Kevin Wu, wildeyes, seanadenise, LennyLip, Peng Li, Titus Woo, dominictobias, Rohit Gupta, Sabri Meviş and 6 more
changed the title [-]Exclude react from bundle, and including it directly from the layout[/-][+]Exclude react from bundle, and include it directly from the layout[/+]on Jul 22, 2015
@xusiyuan841028 Yep, I do. But webpack is looking for react while on my page I have React.
Even having window.react = React right after loading my React script from CDN doesnt solve it.
Internally webpack is returning Uncaught Error: Cannot find module "react"
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).
That works really well!!
One more thing, is there anyway we can get rid off dummyReact.js and somehow directly export window.React from webpack?
Now that it's working, I'm trying to make it graceful :)
There is another module with an equal name when case is ignored.
This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
Rename module if multiple modules are expected or use equal casing if one module is expected.
Curious because different solutions seem to be working for different folks (presumed via the thumbs up) and I am not understanding why..
Reacted by Jason Pickens, Igor, Stepan Koltsov, Kevin Ghadyani - Minted, Chris Keen and Nantris
Reacted by Ali Hmer , Shashikanth Gadgay, Kerry, Kiryl Valkovich, Jruif, Yury Shevchenko, Karol Klepacki, Jay Cheroske, Didier Chartrain, Ghislain Seguin and 20 more
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 window object. E.g. react to window.React, react-dom to ReactDOM etc. On Node on the other hand, we require the dependencies by package name.
Many people that are writing React applications are only using it on the client side. They set output.target to 'web' and only need to list the web method (grab from root) in their externals config. However if you target more than one environment, you need separate externals configs for each environment. React MDL targets the 4 environments listed above.
Edit:
For completeness, have a look at the docs on externals. Specifically, the table below it shows for each possible externals type what the resulting import statement looks like. So if you know where it should be coming from, you can work backwards to determine which externals type you need.
Reacted by buildbreakdo, Ali Hmer , Zach Smith, Ivan Zheliev, Titus Woo and Nantris
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. material-ui).
As they [Facebook] state in a bug report, they'll ignore the exclusion. But by adding even more webpack configuration you can work around that too:
I'm getting the below error when using noParse: ['react'] as noted above:
Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration[0].module.noParse[0]: The provided value "react" is not an absolute path!
-> An absolute path, when the module starts with this path it is not parsed
Activity
[-]Exclude react from bundle, and including it directly from the layout[/-][+]Exclude react from bundle, and include it directly from the layout[/+]xusiyuan841028 commentedon Jul 22, 2015
@YoussefTaghlabi Do you add <script> tag in your page?
YoussefTaghlabi commentedon Jul 22, 2015
@xusiyuan841028 Yep, I do. But webpack is looking for
reactwhile on my page I haveReact.Even having
window.react = Reactright after loading myReactscript from CDN doesnt solve it.Internally webpack is returning
Uncaught Error: Cannot find module "react"IngwiePhoenix commentedon Jul 22, 2015
Solution:
Create a JS file called dummyReact.js
Fill that with these contents:
module.exports = window.React;
Now add an alias in your config.webpack.js
"react": "dummyReact.js"
Make sure your react is loaded before the WebPack bundle, and this should work.
In addition, you can also use the ProvidePlugin in conjunction with the dummyReact.js.
YoussefTaghlabi commentedon Jul 22, 2015
Thanks @IngwiePhoenix , the following did work indeed:
I had to remove
new webpack.IgnorePlugin(/react/)for it to work as it seems webpack completely ignores anything that has to do withreact.My next question, how can I make use of
ProvidePluginin this use case?IngwiePhoenix commentedon Jul 22, 2015
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" :)
YoussefTaghlabi commentedon Jul 22, 2015
Perfect!
Thanks @IngwiePhoenix !
IngwiePhoenix commentedon Jul 22, 2015
If you wonder how exactly this affects your 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).
IngwiePhoenix commentedon Jul 22, 2015
@YoussefTaghlabi Very welcome. =)
YoussefTaghlabi commentedon Jul 22, 2015
That works really well!!
One more thing, is there anyway we can get rid off dummyReact.js and somehow directly export window.React from webpack?
Now that it's working, I'm trying to make it graceful :)
IngwiePhoenix commentedon Jul 22, 2015
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.
YoussefTaghlabi commentedon Jul 22, 2015
I sure will. Thanks for your help!
sokra commentedon Jul 22, 2015
should be enough
YoussefTaghlabi commentedon Jul 22, 2015
either external or ProvidePlugin works in conjunction with alias.
7 remaining items
bebraw commentedon Aug 11, 2016
@aft-luke Great to hear!
buildbreakdo commentedon Sep 8, 2016
So which is it..
or
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..
buildbreakdo commentedon Sep 8, 2016
Okay so here is the solution that worked for me.. all credits to @Download
In webpack.config.js you can apparently pass an object..
Download/react-mdl@755c568
Download commentedon Sep 9, 2016
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
windowobject. E.g.reacttowindow.React,react-domtoReactDOMetc. On Node on the other hand, werequirethe dependencies by package name.Many people that are writing React applications are only using it on the client side. They set
output.targetto'web'and only need to list the web method (grab from root) in theirexternalsconfig. However if you target more than one environment, you need separate externals configs for each environment. React MDL targets the 4 environments listed above.Edit:
For completeness, have a look at the docs on externals. Specifically, the table below it shows for each possible externals type what the resulting import statement looks like. So if you know where it should be coming from, you can work backwards to determine which externals type you need.
jaredreich commentedon Sep 12, 2016
Just to add my two cents, this did the trick for me:
neopostmodern commentedon Sep 16, 2016
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.
material-ui).As they [Facebook] state in a bug report, they'll ignore the exclusion. But by adding even more webpack configuration you can work around that too:
try to fixed react refs must have owner: webpack/webpack#1275 (comment)
try to fixed react refs must have owner: webpack/webpack#1275 (comment)
try to fixed react refs must have owner: webpack/webpack#1275 (comment)
try to fixed react refs must have owner: webpack/webpack#1275 (comment)
AZaviruha commentedon Dec 17, 2017
If you are using lerna and "monorep", and building packages with webpack - this may help:
lerna/lerna#1049
ycscholes commentedon Jan 24, 2018
@jaredreich , Work for me
Fix dev environment per webpack/webpack#1275
abhchand commentedon Oct 28, 2019
I'm getting the below error when using
noParse: ['react']as noted above: