Skip to content
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

Add ES6 exports #6483

Merged
merged 1 commit into from
May 30, 2018
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
23 changes: 16 additions & 7 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2468,26 +2468,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 @@ -613,6 +613,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