After I’d converted the code base, every single one of our .js files had a function closure wrapping everything. It seemed awfully repetitive so I wondered if there was a way to automatically generate them. As it turns out, you can do i fairly easily using Rails’ asset pipeline! I had a hard time figuring this out so I figured I’d quickly blog about it in case anyone else wants to know how it’s done.
First, create a class, for example
Then all you have to do is create an initializer to activate it, for example
Note you can use this to wrap any type of file that passes through the preprocessor with content.
One caveat – if you change the IIFE code, you’ll have to clear your
tmp directory in order to get your assets to recompile.