-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[file_packager.py] Add --export-es6 to file_packager.py #24737
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
Open
lkwinta
wants to merge
7
commits into
emscripten-core:main
Choose a base branch
from
lkwinta:file_packager_modularize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+106
−20
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be712ca
Add modularize to file_packager.py
lkwinta b277aea
simplify promise usage
lkwinta f669970
simplyfi help code
lkwinta ddb448e
simplyfi test
lkwinta 529e4a2
explicite promise resolving
lkwinta e0bc809
address @sbc100 review
lkwinta 9241eca
fix indentation
lkwinta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
|
||
Usage: | ||
|
||
file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node] [--help] | ||
file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node] [--export-es6] [--help] | ||
|
||
--preload , | ||
--embed See emcc --help for more details on those options. | ||
|
@@ -41,6 +41,8 @@ | |
|
||
--export-name=EXPORT_NAME Use custom export name (default is `Module`) | ||
|
||
--export-es6 Wrap generated code inside ES6 exported function | ||
|
||
--no-force Don't create output if no valid input file is specified. | ||
|
||
--use-preload-cache Stores package in IndexedDB so that subsequent loads don't need to do XHR. Checks package version. | ||
|
@@ -129,6 +131,7 @@ def __init__(self): | |
self.use_preload_plugins = False | ||
self.support_node = True | ||
self.wasm64 = False | ||
self.export_es6 = False | ||
|
||
|
||
class DataFile: | ||
|
@@ -362,7 +365,7 @@ def main(): # noqa: C901, PLR0912, PLR0915 | |
To revalidate these numbers, run `ruff check --select=C901,PLR091`. | ||
""" | ||
if len(sys.argv) == 1: | ||
err('''Usage: file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node] [--help] | ||
err('''Usage: file_packager TARGET [--preload A [B..]] [--embed C [D..]] [--exclude E [F..]] [--js-output=OUTPUT.js] [--no-force] [--use-preload-cache] [--indexedDB-name=EM_PRELOAD_CACHE] [--separate-metadata] [--lz4] [--use-preload-plugins] [--no-node] [--export-es6] [--help] | ||
Try 'file_packager --help' for more details.''') | ||
return 1 | ||
|
||
|
@@ -391,6 +394,9 @@ def main(): # noqa: C901, PLR0912, PLR0915 | |
elif arg == '--no-force': | ||
options.force = False | ||
leading = '' | ||
elif arg == '--export-es6': | ||
options.export_es6 = True | ||
leading = '' | ||
elif arg == '--use-preload-cache': | ||
options.use_preload_cache = True | ||
leading = '' | ||
|
@@ -485,6 +491,11 @@ def main(): # noqa: C901, PLR0912, PLR0915 | |
diagnostics.error('TARGET should not be the same value of --js-output') | ||
return 1 | ||
|
||
if options.from_emcc and options.export_es6: | ||
diagnostics.error('Can\'t use --export-es6 option together with --from-emcc since the code should be embedded ' | ||
'within emcc\'s code') | ||
lkwinta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 1 | ||
|
||
walked.append(__file__) | ||
for file_ in data_files: | ||
if not should_ignore(file_.srcpath): | ||
|
@@ -621,20 +632,38 @@ def generate_js(data_target, data_files, metadata): | |
if options.from_emcc: | ||
ret = '' | ||
else: | ||
ret = ''' | ||
if options.export_es6: | ||
ret = 'export default async function loadDataFile(Module) {\n' | ||
else: | ||
ret = ''' | ||
var Module = typeof %(EXPORT_NAME)s != 'undefined' ? %(EXPORT_NAME)s : {};\n''' % {"EXPORT_NAME": options.export_name} | ||
|
||
ret += ''' | ||
Module['expectedDataFileDownloads'] ??= 0; | ||
Module['expectedDataFileDownloads']++; | ||
(() => { | ||
Module['expectedDataFileDownloads']++;''' | ||
|
||
if not options.export_es6: | ||
ret += ''' | ||
(() => {''' | ||
|
||
ret += ''' | ||
// Do not attempt to redownload the virtual filesystem data when in a pthread or a Wasm Worker context. | ||
var isPthread = typeof ENVIRONMENT_IS_PTHREAD != 'undefined' && ENVIRONMENT_IS_PTHREAD; | ||
var isWasmWorker = typeof ENVIRONMENT_IS_WASM_WORKER != 'undefined' && ENVIRONMENT_IS_WASM_WORKER; | ||
if (isPthread || isWasmWorker) return;\n''' | ||
|
||
if options.support_node: | ||
ret += " var isNode = typeof process === 'object' && typeof process.versions === 'object' && typeof process.versions.node === 'string';\n" | ||
|
||
if options.support_node and options.export_es6: | ||
ret += '''if (isNode) { | ||
const { createRequire } = await import('module'); | ||
/** @suppress{duplicate} */ | ||
var require = createRequire(import.meta.url); | ||
}\n''' | ||
|
||
if options.export_es6: | ||
ret += 'return new Promise((loadDataResolve, loadDataReject) => {\n' | ||
ret += ' async function loadPackage(metadata) {\n' | ||
|
||
code = ''' | ||
|
@@ -684,6 +713,8 @@ def generate_js(data_target, data_files, metadata): | |
create_data = '''// canOwn this data in the filesystem, it is a slide into the heap that will never change | ||
Module['FS_createDataFile'](this.name, null, byteArray, true, true, true); | ||
Module['removeRunDependency'](`fp ${that.name}`);''' | ||
ready_promise = ''' | ||
loadDataResolve();''' | ||
|
||
if not options.lz4: | ||
# Data requests - for getting a block of data out of the big archive - have | ||
|
@@ -710,14 +741,14 @@ def generate_js(data_target, data_files, metadata): | |
finish: function(byteArray) { | ||
var that = this; | ||
%s | ||
this.requests[this.name] = null; | ||
this.requests[this.name] = null;%s | ||
} | ||
}; | ||
|
||
var files = metadata['files']; | ||
for (var i = 0; i < files.length; ++i) { | ||
new DataRequest(files[i]['start'], files[i]['end'], files[i]['audio'] || 0).open('GET', files[i]['filename']); | ||
}\n''' % (create_preloaded if options.use_preload_plugins else create_data) | ||
}\n''' % (create_preloaded if options.use_preload_plugins else create_data, ready_promise if options.export_es6 else '') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about:
Then use |
||
|
||
if options.has_embedded and not options.obj_output: | ||
diagnostics.warn('--obj-output is recommended when using --embed. This outputs an object file for linking directly into your application is more efficient than JS encoding') | ||
|
@@ -958,6 +989,9 @@ def generate_js(data_target, data_files, metadata): | |
return contents.buffer; | ||
}'''.strip() | ||
|
||
reject_promise = ''' | ||
loadDataReject();''' | ||
|
||
ret += ''' | ||
async function fetchRemotePackage(packageName, packageSize) { | ||
%(node_support_code)s | ||
|
@@ -1128,7 +1162,14 @@ def generate_js(data_target, data_files, metadata): | |
} | ||
loadPackage(%s);\n''' % json.dumps(metadata) | ||
|
||
ret += ''' | ||
if options.export_es6: | ||
ret += ''' | ||
lkwinta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} | ||
// END the loadDataFile function | ||
''' | ||
else: | ||
ret += ''' | ||
})();\n''' | ||
|
||
return ret | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need the
.catch
here. Doingreturn loadDataFile(..
should be enough to reject the promise if loadDataFile fails/rejects.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I think just using top-level-await might be more clear here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will apply that later, I have changed some logic to be more universal and to allow catching thrown error, take a look please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder, if I shouldn't add some throw error method to clear run dependencies - or maybe generate separate stub that allows to do that if we want ourselfs in external catch block
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be other PR tho, I have just observed anoying behaviour that when it fails, main module constantly spams run dependencies message, being able to clean those to graceful escape error would be nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that removing the run dependency is the way to fix that.. since that would result in the module running. If a dependency fails we want to block the running forever I think.. not just ignore it. But I think the existing code might not do that :(