Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does meteor 0.6.0 wrap everything into (function(){ ... })

Tags:

meteor

Since version 0.6.0, meteor wraps each javascript file into (function() { ... }). This makes perfect sense for my own javascript files. But not for third party libraries. E.g. I am using sha3.js from crypto-js. This. located at client/lib. This was perfect till 0.5.9. But now, the functions from sha3 are not available anymore.

Can this wrapping be switched off?

like image 261
David Graf Avatar asked Apr 07 '13 09:04

David Graf


2 Answers

Function closures were first introduced on the server side (and only on the server) for two main reasons :

  • Scoped variables are a great way to avoid variables collisions while keeping simple variables names
  • It was technically needed for the Npm.require feature

One of the Node/Meteor key feature is the ability to run the same file on the client and on the server. That's why variable scoping need to have the same behavior on both client and server, and why Meteor now includes functions closures on client as well.

It's not possible to switch off the wrapping (without changing the Meteor/tools code).

This behavior will be improved soon with the work on the linker branch that will automatically solve your files dependencies (based on variables names) and then 1. include javascript files in the right order 2. export in the global scope the variables that need to.

For now you will have to manually exports the objects that need to be in the global scope.

like image 130
mquandalle Avatar answered Nov 02 '22 07:11

mquandalle


You can use the undocumented bare option (formerly raw) to add_files:

api.add_files([
  'sha3.js'
], 'client', {bare: true});

And it will not wrap the added file(s).

like image 31
Mitar Avatar answered Nov 02 '22 06:11

Mitar