goog.exportSymbol to CommonJS

749 views
Skip to first unread message

Leo Lloyd Andrade

unread,
Nov 4, 2016, 6:13:35 AM11/4/16
to Closure Compiler Discuss
I've written some code using closure library having goog.exportSymbol() statements in my entry point. Now I wish to use advanced optimization of closure compiler to output a CommonJS module, where the exportSymbol() calls map to module.exports. My goal is to use the modules in webpack. Any suggestions how this can be achieved?

Chad Killingsworth

unread,
Nov 4, 2016, 12:15:29 PM11/4/16
to Closure Compiler Discuss
It sounds like you just need to use an output_wrapper:

"%output%;module.exports.foo = foo";

Leo Lloyd Andrade

unread,
Nov 4, 2016, 12:46:38 PM11/4/16
to Closure Compiler Discuss
wow. didn't realize it could be that simple. Thanks.

Leo Lloyd Andrade

unread,
Nov 4, 2016, 1:53:05 PM11/4/16
to Closure Compiler Discuss
In the compiled code "foo" is set as such:

function a(){}var b=["foo"],c=this;b[0]in c||!c.execScript...

and therefore not available to be assigned in the output_wrapper to module.exports Am I missing something?


On Friday, November 4, 2016 at 8:15:29 PM UTC+4, Chad Killingsworth wrote:

Erik Neumann

unread,
Nov 4, 2016, 5:10:09 PM11/4/16
to closure-comp...@googlegroups.com
That compiled code should wind up defining "foo" as a property of window and therefore it is available as a global.  Did you try it and it didn't work?  If so what is the error?
--ErikN


--

---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-discuss+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/closure-compiler-discuss/f620a728-e9a6-45c4-a4e5-5b73957e1cca%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Leo Lloyd Andrade

unread,
Nov 4, 2016, 8:12:04 PM11/4/16
to Closure Compiler Discuss
It returned "foo" is undefined. I managed to get it to work by enclosing %output% in a self-executing function. Then I "require" the compiled file into a webpack module and finally used window.foo.call() to access the exported function.
Reply all
Reply to author
Forward
0 new messages