Skip to content

Export module for Node when MODULARIZE=1 #5239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,4 @@ a license to everyone to use it as detailed in LICENSE.)
* Inseok Lee <dlunch@gmail.com>
* Yair Levinson (copyright owned by Autodesk, Inc.)
* Matjaž Drolc <mdrolc@gmail.com>

* James Swift <james@3dengineer.com> (copyright owned by PSPDFKit GmbH)
4 changes: 4 additions & 0 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,10 @@ def modularize(final):
f.write('\n')
f.write(' return ' + shared.Settings.EXPORT_NAME + ';\n')
f.write('};\n')
# Export the function if this is for Node otherwise it is lost.
f.write('if (typeof module === \'object\' && module.exports) {\n')
f.write(" module['exports'] = " + shared.Settings.EXPORT_NAME + ';\n')
f.write('};\n')
f.close()
if DEBUG: save_intermediate('modularized', 'js')
return final
Expand Down
12 changes: 12 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -5298,6 +5298,18 @@ def test_require(self):
Building.emcc(inname, output_filename='a.out.js')
output = Popen(NODE_JS + ['-e', 'require("./a.out.js")'], stdout=PIPE, stderr=PIPE).communicate()
assert output == ('hello, world!\n', ''), 'expected no output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output

def test_require_modularize(self):
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-s', 'MODULARIZE=1']).communicate()
src = open('a.out.js').read()
assert "module['exports'] = Module;" in src
output = Popen(NODE_JS + ['-e', 'var m = require("./a.out.js"); m();'], stdout=PIPE, stderr=PIPE).communicate()
assert output == ('hello, world!\n', ''), 'expected output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-s', 'MODULARIZE=1', '-s', 'EXPORT_NAME="NotModule"']).communicate()
src = open('a.out.js').read()
assert "module['exports'] = NotModule;" in src
output = Popen(NODE_JS + ['-e', 'var m = require("./a.out.js"); m();'], stdout=PIPE, stderr=PIPE).communicate()
assert output == ('hello, world!\n', ''), 'expected output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output

def test_native_optimizer(self):
def test(args, expected):
Expand Down