3
votes

In my webpack build, I would like to load React from a CDN, but not ReactDOM, as it requires an extra roundtrip for a very small file.

My webpack configuration has the following block of code declaring "externals" so that it will not build these files (I instead include CDNs).

webpack.config.js

...

externals: {
    react: 'React'
},

...

The problem is that only including React in externals still builds React because ReactDOM depends on it.

node_modules/react-dom/index.js

module.exports = require('react/lib/ReactDOM');

Adding 'react-dom': 'ReactDOM' to externals effectively removes them both from the bundle, but I don't want to have to include the ReactDOM CDN...

How can I configure webpack to load React from a CDN, but include ReactDOM in my main bundle?


Note: I'm using webpack 2.1.0-beta17 and React 15.1.0.


Update I tried adding react/lib/ReactDOM to externals.

...

externals: {
    react: 'React',
    'react/lib/ReactDOM': 'commonjs react-dom'
},

...

But I get the following error.

require is not defined

1
But it says it's intended to be included with React, so when you put React on CDN you also have ReactDOM included. - AlexandruB
Right, I just want to include ReactDOM in my bundle, rather than include CDN for it. - Himmel
I don't understand the use case but I guess define react-dom as it's own Webpack entry and make it a separate chunk? - AlexandruB
Including react-dom as a separate chunk would not satisfy my intention to include it as part of my main bundle. - Himmel
My best guess: try adding a webpack external for react/lib/ReactDOM. Otherwise, check stackoverflow.com/questions/34602831/… for ideas. Yet another idea: try the dedupe plugin. - Ed Staub

1 Answers

2
votes

[Not an answer, too big for comment]

See issues https://github.com/facebook/react/issues/5413, https://github.com/facebook/react/issues/6128, which argue for having a CDN bundle of React+ReactDom, which would float your boat, if I understand correctly. Dan Abramov feels it, so I'm hopeful. Note, however, his comment that ReactDOM will get a lot bigger soon.