The browsers support better performance for many small files
You really want to support a small download on mobile
Then you are better off with a single JS download that is cached. You definitely don’t want to have duplicates JS code wrapped up with the Sprockets application.js requires.
Yes, 100% correct. In the other thing is that the multiple entry point library is amazing. Webpack inspects the modules, and figures out which dependencies are shared between all(most?) and automatically extracts them out to the common bundle.
6to5 automatically gives you all ES6 features and it transpiles JSX out of the box also. So with this and rails-assets you can save yourself a whole lot of work and have all the main features you want, except of course the hot module replacement, which in my opinion is not worth the tradeoff.
@justin Great write up! Do you think there’s a way to make your app component available in the components.js to take advantage of {prerender: true} option from react-rails?
@justin Thanks for this tutorial. I’m on a new project and am thankfully able to set something like this up pretty much from day one, so a nice ES6/React/Webpack-y future for me.
This app I’m on is a more traditional web app with some turbolinks thrown in there for good measure. A couple of the views have some embedded content at the bottom of the view that gets rendered on page load to sprinkle in some functionality and looks like this:
<%= content_for :javascripts do %>
<script type="text/javascript">
jQuery(document).on('page:change', function(){
// do some stuff
})
</script>
<% end %>
Is there an idiomatic way to move this script out of this template and into its own script that gets called when this view is hit? All I can really come up with is assigning that script to a global that gets called instead:
<%= content_for :javascripts do %>
<script type="text/javascript">
jQuery(document).on('page:change', function(){
CallMyGlobal()
})
</script>
<% end %>
But that seems inelegant.
I’ve been mainly working on a single page web app for the last couple years, right before turbolinks took off, so I’m not sure how best to go at this hybrid approach.
Do not EVER, EVER!! mess with the permissions of system files!! Don’t install node as sudo! Please install node the right way! Use nvm if you want the simplest solution. You’re going to permanently mess up your node and other library installs to circumvent a problem that doesn’t exist in the first place. Please edit your blog post to remove this bad advice!!!
I’ve thought about adding devise to the tutorial, but it would seem to take from the focus of a super simple example.
My consulting team is building a greenfield “fun” real world Silicon Valley startup app based on the tutorial, so if anybody is interested in joining this venture, get in touch! I’ll soon be making about post about all the interesting things we’re doing. However, if you want to work on a real-world project with these technologies, get in touch with me at justin@railsonmaui.com.
Just read this article today. Great stuff - thanks Justin and team! A question that occurs to me is did you considered replacing the Rails asset pipeline with Gulp? It seems like you could get more control over your integration of technologies. The main thing that you need the react-rails gem for is to plug into the asset pipeline, right?
And here’s an example talking about using the pipeline with Webpack and Gulp but without the react-rails gem (uses 6to5 loader to transpile ES6 and JSX): http://aergonaut.com/rails-4-gulp-webpack-react/
@justin hello justin, i try to deploy the heroku, but, i got this:
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
cd client && $(npm bin)/webpack --config './config/webpack/production.config.js'
module.js:338
throw err;
^
Error: Cannot find module 'jquery'
at Function.Module._resolveFilename (module.js:336:15)
at Function.require.resolve (module.js:388:19)
at Object.<anonymous> (/tmp/build_d29d23c4c49fcec213baff4ad1d9a8d0/client/config/webpack/production.config.js:32:18)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at module.exports (/tmp/build_d29d23c4c49fcec213baff4ad1d9a8d0/client/node_modules/webpack/bin/convert-argv.js:80:13)
rake aborted!
Command failed with status (1): [cd client && $(npm bin)/webpack --config '...]
/tmp/build_d29d23c4c49fcec213baff4ad1d9a8d0/lib/tasks/assets.rake:16:in `block (2 levels) in <top (required)>'
Tasks: TOP => assets:precompile => assets:compile_environment => assets:webpack
(See full trace by running task with --trace)
If you look at the example webpack config files and look for the string jQuery, jquery, or $, you’ll see the line that allows webpack to know that jQuery is globally exported by Rails.