All JS code generated from CoffeeScript sources is wrapped in an anonymous function with an immediate call (IIFE), like:

(function(){ // мой код }).call(this) 

How can I remove this wrapper so that only my code remains? The thing happens in Ruby on Rails.

1 answer 1

This is extreme measures. Go for it only if you are 100% sure that this is the best way out of your situation. And such situations are very rare.


In CoffeeScript itself, the b / bare option is responsible. It remains to understand how to turn it on.

If you dig into the sources, it turns out that the transformation of CoffeeScript to Ruby on Rails deals with Tilt . And from the source code it is easy to understand where bare included there:

 Tilt::CoffeeScriptTemplate.default_bare = true 

Where to put down is already on your choice.

If you have to do this for the whole project, you can create an initializer file, say, config/initializers/tilt.rb Where to place this line, having previously made require "tilt/coffee" (see the path to the file inside Tilt, starting with the lib folder), since this is the very beginning of the download, and the necessary definitions could not yet be fulfilled.