-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Check whether scriptDir is defined. #12832
Conversation
Avoid reference error in checking whether scriptDir is defined.
Thank you for submitting a pull request! If this is your first PR, make sure to add yourself to AUTHORS. |
I may be missing something, but I'm not sure how it could be undefined. A program with MODULARIZE should start with a definition var _scriptDir = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined; So it could have the value |
Thanks @kripken - maybe I'm not building this properly. In TF.js, we bundle the emscripten generated JS module along with the rest of our JS. Therefore we need to pass the emscripten JS module as a Blob to the emscripten generated worker (via The definition of _scriptDir is emitted - but we stringify the emscripten module after it has self-executed (https://github.com/tensorflow/tfjs/blob/master/tfjs-backend-wasm/src/backend_wasm.ts#L274), so the definition is not included. You can see that I define _scriptDir as part of the prefix, but this creates issues with minification. Sorry if I'm missing an obvious alternative! |
To make sure I understand: You are building with MODULARIZE, and then creating an instance from the factory function, and then stringifying that instance, then sending it somewhere, and then when it is evalled there, it is missing If I got that right, then a workaround for this could be to use a However, in the bigger picture, I'm confused as to why the stringification is needed. I hope that can be avoided, as it should be simpler and more efficient? But I'm probably not understanding something. And specifically I'm not sure what you mean by
? |
@kripken Thanks for the tip about --pre-js - I should have read the docs more carefully! |
Adding the type check avoids triggering a ReferenceError if _scriptDir has not been defined.