Skip to content

Commit f841952

Browse files
author
hartsantler
committed
fixed Google Closure compiler, export functions to window global namespace.
1 parent a8ee64d commit f841952

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

pythonscript/pythonjs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def visit_FunctionDef(self, node):
7878
body.append(self.visit(child))
7979
buffer += '\n'.join(body)
8080
buffer += '\n}\n'
81+
buffer += 'window["%s"] = %s \n' % (node.name, node.name) ## export to global namespace so Closure will not remove them
8182
return buffer
8283

8384
def visit_Subscript(self, node):

tests/server.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,14 @@ def pythonjs_to_javascript( src, closure_compiler=False ):
4343
a = stdout.decode('utf-8')
4444

4545
if closure_compiler and os.path.isfile( PATHS['closure'] ):
46-
x = '/tmp/input.js'; y = '/tmp/output.js'
46+
x = '/tmp/input.js'; y = '/tmp/output.js';
4747
f = open(x, 'wb'); f.write( a.encode('utf-8') ); f.close()
48-
subprocess.call( ['java', '-jar', PATHS['closure'], '--compilation_level', 'ADVANCED_OPTIMIZATIONS', '--js', x, '--js_output_file', y] )
48+
subprocess.call([
49+
'java', '-jar', PATHS['closure'],
50+
'--compilation_level', 'ADVANCED_OPTIMIZATIONS',
51+
'--js', x, '--js_output_file', y,
52+
#'--create_name_map_files',
53+
])
4954
f = open(y, 'rb'); a = f.read().decode('utf-8'); f.close()
5055

5156
return a

tests/test_closure.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<html>
2+
<head>
3+
<script src="pythonscript.js"></script>
4+
5+
<script type="text/python" closure="true">
6+
def inline_me():
7+
return 1
8+
9+
def test():
10+
i = 0
11+
while i < 10:
12+
i += inline_me()
13+
14+
print 'hello world', i
15+
16+
</script>
17+
</head>
18+
19+
<body>
20+
<button onclick="test()">click me</button>
21+
</body>
22+
</html>

0 commit comments

Comments
 (0)