Skip to content

Commit

Permalink
Add an option for ES6 exporting (fixes #6284) (#6483)
Browse files Browse the repository at this point in the history
  • Loading branch information
curiousdannii authored and kripken committed May 30, 2018
1 parent 5874b9e commit 13b8307
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
23 changes: 16 additions & 7 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2503,26 +2503,35 @@ def modularize():
src = open(final).read()
final = final + '.modular.js'
f = open(final, 'w')

# Included code may refer to Module (e.g. from file packager), so alias it
# Export the function as Node module, otherwise it is lost when loaded in Node.js or similar environments
f.write('''var %(EXPORT_NAME)s = function(%(EXPORT_NAME)s) {
%(EXPORT_NAME)s = %(EXPORT_NAME)s || {};
%(src)s
return %(EXPORT_NAME)s;
}%(instantiate)s;
if (typeof exports === 'object' && typeof module === 'object')
module.exports = %(EXPORT_NAME)s;
else if (typeof define === 'function' && define['amd'])
define([], function() { return %(EXPORT_NAME)s; });
else if (typeof exports === 'object')
exports["%(EXPORT_NAME)s"] = %(EXPORT_NAME)s;
''' % {
'EXPORT_NAME': shared.Settings.EXPORT_NAME,
'src': src,
'instantiate': '()' if shared.Settings.MODULARIZE_INSTANCE else ''
})

# Export using a UMD style export, or ES6 exports if selected
if shared.Settings.EXPORT_ES6:
f.write('''export default %s;''' % shared.Settings.EXPORT_NAME)
else:
f.write('''if (typeof exports === 'object' && typeof module === 'object')
module.exports = %(EXPORT_NAME)s;
else if (typeof define === 'function' && define['amd'])
define([], function() { return %(EXPORT_NAME)s; });
else if (typeof exports === 'object')
exports["%(EXPORT_NAME)s"] = %(EXPORT_NAME)s;
''' % {
'EXPORT_NAME': shared.Settings.EXPORT_NAME
})

f.close()
if DEBUG: save_intermediate('modularized', 'js')

Expand Down
2 changes: 2 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ var MODULARIZE_INSTANCE = 0; // Similar to MODULARIZE, but while that mode expor
// Note that the promise-like API MODULARIZE provides isn't
// available here (since you arean't creating the instance
// yourself).
var EXPORT_ES6 = 0; // Export using an ES6 Module export rather than a UMD export.
// MODULARIZE must be enabled for ES6 exports.

var BENCHMARK = 0; // If 1, will just time how long main() takes to execute, and not
// print out anything at all whatsoever. This is useful for benchmarking.
Expand Down

0 comments on commit 13b8307

Please sign in to comment.