Skip to content

coffeescript closure pains #34

@jescalan

Description

@jescalan

I've found that I have an issue with the way that coffeescript plays with asset pipeline libraries.

As an example, when dealing with a larger project, sometimes you'll want to split your js into multiple files then concat them at the end (exactly what snockets/sprockets is for), but when using coffeescript, there's hardly any use to concatenating your files since the extra closure on compile ensures that no code can be shared between the two files anyway. The only way around this issue is to attach shared functions to the window object, which is exactly what the extra closure is trying to prevent.

Ideally, I would want to have a single closure wrapper that I can put in myself inside an app.coffee file, for example, then inside that closure I could require all my other files which are split out for organization purposes, and have all the files compile bare (without the closure). This way, it's easy to organize code cleanly, export functions/variables from the different files, and all your code is still protected by an anonymous function closure. Ah, the dream.

So I set about seeing if I could make this happen. Initially I thought it would be easy to fork snockets and add an extra option on initialization (like bare: true) which would add the {bare: true} option into the coffeescript compile function. I did this, and it was just fantastic and only like 2 lines of code. But when I tested it, I found a rather strange result, illustrated below:

# main.coffee

->
  #= require 'other'
  console.log 'hello from the main file!'

# other.coffee

console.log 'hello from the other file!'

This is the coffeescript code I started out with, figuring that if things went magically well, I would end up with this result as the compiled js:

# main.js (imaginary)

(function(){
  console.log('hello from the other file!')
  console.log('hello from the main file!');
});

...however, what I actually ended up with was this:

# main.js

console.log('hello from the other file!')

(function(){
  console.log('hello from the main file!');
});

Now, I can totally understand why this is happening, and I feel like fixing the way the concatentation through requires works might be a pretty large a fundamental change to the library, with other possible consequences I didn't think of, as I haven't had nearly as much experience building asset pipeline libraries as you have. But I just wanted to run it by you to see your thoughts. Would this be something worth me looking into submitting a pull request for? Would it be something maybe better suited to be built as it's own tiny coffeescript requires library? Did this explanation not even exactly make sense to you and I should try re-writing this since I suck at writing? Either way, let me know!

Cheers, and thanks for reading through this epically long and ridiculous issue! : )

EDIT: Just saw this #23, read it through, and that probably is a better solution than my forked version, but either way this doesn't really change my overall issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions