bmcmahen / Subtitles

Easily create subtitles (SRT files) in your web browser.
http://subtitles.fiddleware.com
147 stars 30 forks source link

meteor 0.6 breaks components #2

Closed bmcmahen closed 11 years ago

bmcmahen commented 11 years ago

consider creating a package for components?

bmcmahen commented 11 years ago

The problem is due to Meteor's new (and I think, poorly chosen) automatic closures. These are added to every javascript file on the client side even if those files are added via package.js. Not sure about solutions yet.

bmcmahen commented 11 years ago

Fixed it temporarily by altering our third party libraries instead, to export with a global.

mounte commented 11 years ago

I am quite new to javascript, closures and everything ... Could you in more detail explain the modifications you did to the third party libraries?

When I check out the project and run a component-build i get the error: Uncaught ReferenceError: require is not defined

When I run a version of the application that is "vanilla" i.e. not runnint component-build everything works.

The diff on the build.js is: -require = function(path, parent, orig) { +function require(path, parent, orig) {

best regards

bmcmahen commented 11 years ago

@mounte have you tried cloning the current repository? Everything should just work with the latest repository.

The main difference is as follows: Meteor 0.6 auto-wraps each document contained within the project in a closure, like this:

 (function(){

 // my code here 

 })(); 

Any variable that is declared within a closure is local to that closure. So if I decalare var x = 'ben' within that closure, I cannot access the variable x outside of the closure. The way to make variables accessible outside of the closure is to define them as global variables. This means removing the var from the variable, thus making it available to other documents within the project.

The problem with Components is that the require function (which is used to import libraries within the project) is a regular function. So when it is wrapped in a closure, it is not exposed to code outside of that closure. The fix is to declare the require function using a global variable instead, using the variable style function declaration that is available in Javascript. This exposes require to the rest of my app, and makes it so that I can import the notification module, loading indicator module, etc.

Does that make sense?

mounte commented 11 years ago

Thank you for the details. So, in summary, I have to manually make the require function global everytime i run componenet build. Den 19 apr 2013 17:57 skrev "Ben McMahen" notifications@github.com:

@mounte https://github.com/mounte have you tried cloning the current repository? Everything should just work with the latest repository.

The main difference is as follows: Meteor 0.6 auto-wraps each document contained within the project in a closure, like this:

(function(){

// my code here

})();

Any variable that is declared within a closure is local to that closure. So if I decalare var x = 'ben' within that closure, I cannot access the variable x outside of the closure. The way to make variables accessible outside of the closure is to define them as global variables. This means removing the var from the variable, thus making it available to other documents within the project.

The problem with Components is that the require function (which is used to import libraries within the project) is a regular function. So when it is wrapped in a closure, it is not exposed to code outside of that closure. The fix is to declare the require function using a global variable instead, using the variable style function declaration that is available in Javascript. This exposes require to the rest of my app, and makes it so that I can import the notification module, loading indicator module, etc.

Does that make sense?

— Reply to this email directly or view it on GitHubhttps://github.com/bmcmahen/Subtitles/issues/2#issuecomment-16661604 .

bmcmahen commented 11 years ago

Unfortunately, yes. I personally don't agree with Meteor's decision to automatically wrap every document in a closure. This is probably bound to break other libraries, too, that don't expose the variable using a global.