Skip to content

Commit 0b09280

Browse files
phraemerkripken
authored andcommitted
Export module for Node when MODULARIZE=1 (#5239)
* Export module in Node when MODULARIZE=1 * Test NodeJS export if MODULARIZE=1 * Use UMD style detection
1 parent 7c06dd5 commit 0b09280

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

AUTHORS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,4 @@ a license to everyone to use it as detailed in LICENSE.)
295295
* Inseok Lee <dlunch@gmail.com>
296296
* Yair Levinson (copyright owned by Autodesk, Inc.)
297297
* Matjaž Drolc <mdrolc@gmail.com>
298-
298+
* James Swift <james@3dengineer.com> (copyright owned by PSPDFKit GmbH)

emcc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,10 @@ def modularize(final):
23392339
f.write('\n')
23402340
f.write(' return ' + shared.Settings.EXPORT_NAME + ';\n')
23412341
f.write('};\n')
2342+
# Export the function if this is for Node otherwise it is lost.
2343+
f.write('if (typeof module === \'object\' && module.exports) {\n')
2344+
f.write(" module['exports'] = " + shared.Settings.EXPORT_NAME + ';\n')
2345+
f.write('};\n')
23422346
f.close()
23432347
if DEBUG: save_intermediate('modularized', 'js')
23442348
return final

tests/test_other.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5298,6 +5298,18 @@ def test_require(self):
52985298
Building.emcc(inname, output_filename='a.out.js')
52995299
output = Popen(NODE_JS + ['-e', 'require("./a.out.js")'], stdout=PIPE, stderr=PIPE).communicate()
53005300
assert output == ('hello, world!\n', ''), 'expected no output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output
5301+
5302+
def test_require_modularize(self):
5303+
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-s', 'MODULARIZE=1']).communicate()
5304+
src = open('a.out.js').read()
5305+
assert "module['exports'] = Module;" in src
5306+
output = Popen(NODE_JS + ['-e', 'var m = require("./a.out.js"); m();'], stdout=PIPE, stderr=PIPE).communicate()
5307+
assert output == ('hello, world!\n', ''), 'expected output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output
5308+
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-s', 'MODULARIZE=1', '-s', 'EXPORT_NAME="NotModule"']).communicate()
5309+
src = open('a.out.js').read()
5310+
assert "module['exports'] = NotModule;" in src
5311+
output = Popen(NODE_JS + ['-e', 'var m = require("./a.out.js"); m();'], stdout=PIPE, stderr=PIPE).communicate()
5312+
assert output == ('hello, world!\n', ''), 'expected output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output
53015313

53025314
def test_native_optimizer(self):
53035315
def test(args, expected):

0 commit comments

Comments
 (0)