communication between modules inside closures

58 views
Skip to first unread message

pablo platt

unread,
Nov 27, 2012, 3:05:36 PM11/27/12
to closure-comp...@googlegroups.com
Hi,

I have an app with plugins that I want to load on demand.
I'm creating one module for the main app and a separate module for each plugin.

To avoid polluting the global namespace I'm wrapping each module with a closure:

java -jar compiler.jar
--compilation_level ADVANCED_OPTIMIZATIONS
--module app:1: --js app.js --module_wrapper app:"(function(){%s)}();"
--module plugin:1:app: --js plugin.js --module_wrapper plugin:"(function(){%s)}();"

How can the plugin module communicate with the app module when they are in separate closures?
Do I need to export some function that will let the plugins interact with the app?

Thanks

John Lenz

unread,
Nov 27, 2012, 8:01:20 PM11/27/12
to closure-compiler
there are 2 easy options:
1) there is a compiler option to rewrite "global" variables and function to be properties on a object you pass to the wrapper (you have one global name or if you are fancy you hide the object in a function that loads your other scripts.  The end result is that you simply need to worry about loading.
2) or you export a interface that your plugin communicates with.

pablo platt

unread,
Nov 27, 2012, 8:39:01 PM11/27/12
to closure-comp...@googlegroups.com
The first option is what I need.
I guess the code size and performance difference won't be noticeable.

I found a post in the mailing list that show how to use it.
Load the first module with:
__ns={};(function(a){%s})(__ns)
and than load the rest of the modules with
_(function(a){%s})(__ns)
https://groups.google.com/forum/?fromgroups=#!searchin/closure-compiler-discuss/global/closure-compiler-discuss/aajyOY8o6CU/XFV9nK-x0zkJ

I couldn't find the the compiler option that tells it to rename the variables in compiler.jar --help
Can you tell me how it is called?

Thanks

John Lenz

unread,
Nov 27, 2012, 11:28:19 PM11/27/12
to closure-compiler
seems that it hasn't been exposed as a command-line flag but the option is:

  CompilerOptions.renamePrefixNamespace

pablo platt

unread,
Nov 28, 2012, 10:01:23 AM11/28/12
to closure-comp...@googlegroups.com
I've tested it with plovr and it works well.
Reply all
Reply to author
Forward
0 new messages