application.js which serves as a manifest of included files. In the development environment the files are served individually and un-minified (which is useful for debugging) and in production they are concatenated and minified. But what do you do if you want to have page (or section)-specific files?
Your first approach might be to just add a new file to the
This will work fine in development. The file will be pre-processed and served separately like all the others and life will be good. The problem is that this will fail in production if you’re precompiling your assets (which you should be). The reason is that only
application.js is considered as a manifest during precompilation. So while
separated_code.js.coffee exists in the assets directory it won’t be compiled and minified for production.
You can add additional manifest files to the list considered during precompilation fairly simply by modifying
application.rb. You can modify the
precompile property of the assets configuration like so:
And add a new manifest file that
requires the actual code:
This works but it’s pretty tedious to add every new file like this. So instead, I decided to make it more generic. I modified my
precompile property like this:
And in my actual page:
One additional step is to remove the
require_tree directive from
application.js. Otherwise (since
require_tree is recursive) the files in each bundle will also be included in the main application file which isn’t what you want.
I try to avoid putting anything that is shared into more than one of these bundles. Placing the same file in more than one bundle will result in the contents of that file being downloaded more than once since it will be concatenated with the rest of the files in each bundle. So I try to keep shared stuff required from