I want to render a React app on the serverside and have dependencies handling problems.
More precisely, I want to load jquery from cdn and be able to do
require('jquery')
in my application's files. Everything works well on the client side, I have a webpack config file wich looks like this:
# webpack.config.js
externals : {
"jquery": "jQuery",
}
and jquery is simply loaded in an html file:
# index.html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
I am not able to make this work serverside. If I understand well, this has nothing to do with webpack, which is a only a bundler and it's job is not to actually load the files. Once again, if I understand well, this should be done by my node server which actually renders the React component.
# server.js
var reactRender = require('react-render');
reactRender({path: '/abs/path/to/component.js', ...});
The problem is that require('jquery') does not work in this context. Here (using webpack on server side of nodejs) I found an example on how to use webpack serverside but it does not adress the issue of externals components not installed in node_modules.
I also tried to use script.js (https://github.com/ded/script.js) to first load jquery from CDN and then render the app, but it requires the document component, which is undefined serverside.
# this does not work
$script(['https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js'], 'bundle');
$script.ready('bundle', function() {
reactRender(...)
});
Any thought is much appreciated! Thanks