diff --git a/AUTHORS b/AUTHORS index 66458cb7e9941..56faf4e662ff7 100644 --- a/AUTHORS +++ b/AUTHORS @@ -295,4 +295,4 @@ a license to everyone to use it as detailed in LICENSE.) * Inseok Lee * Yair Levinson (copyright owned by Autodesk, Inc.) * Matjaž Drolc - +* James Swift (copyright owned by PSPDFKit GmbH) diff --git a/emcc.py b/emcc.py index af468e87fc451..824469bf9cb7b 100755 --- a/emcc.py +++ b/emcc.py @@ -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 diff --git a/tests/test_other.py b/tests/test_other.py index 18bf7c256a1ba..ec88876271c4d 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -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):