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

Including an emscripten module in browserify build process #5864

Closed
txusinho opened this issue Nov 28, 2017 · 2 comments
Closed

Including an emscripten module in browserify build process #5864

txusinho opened this issue Nov 28, 2017 · 2 comments

Comments

@txusinho
Copy link

txusinho commented Nov 28, 2017

#Hi all,

First of all, I would like to apologize if this is not the right place to ask for help to solve this issue. I'm a browserify user, and since I have problems just with a file create by emscripten, I assume the problem might live here.

Long story short, I'm importing an emscripten module in the frontend using browserify. At execution time, the module exists, but empty.

You can reproduce the issue as follows (requires emcc and browserify installed system wide):

  • Get the code from this repo
  • Compile this file using emcc. I've used this command: emcc main.cpp -o main.js -s EXPORTED_FUNCTIONS="['_int_sqrt']"
  • Create a bundle using browserify (which must be installed system wide): browserify index.js > bundle.js
  • Open index.html in a browser, and you'll see an error in the developer console and one array showing the content of the Module object (this is, an empty array)

I've already tested that the generated code is correct: running in a node environment, works as expected (there is a demonstration using mocha here

PD: I have the same test case, but with a more elaborated toolchain in this repo with a readme to reproduce all the steps

@curiousdannii
Copy link
Contributor

module.exports is set only when ENVIRONMENT_IS_NODE is true, and it won't be true if ENVIRONMENT_IS_WEB is true:

  ENVIRONMENT_IS_WEB = typeof window === 'object';
  ENVIRONMENT_IS_WORKER = typeof importScripts === 'function';
  ENVIRONMENT_IS_NODE = typeof process === 'object' && typeof require === 'function' && !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_WORKER;
  ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;

I think it would be best to always set module.exports (if module exists etc), like it does for MODULARIZE.

Turning MODULARIZE on will probably help you @txusinho, but you will need to access the exported functions differently.

@txusinho
Copy link
Author

txusinho commented Nov 28, 2017

@curiousdannii I appreciate your help, and the speed! The solution worked perfect on my side, and I'm able to use module as needed. The access to functions is not a problem if I have possibilities to access the Module element.

I made this question also in stackoverflow, I'll update the answer there so anyone with the same problem can come to a solution.

EDIT: Sorry for coming back, but after inspecting the code generated, I can't find in the instance of Module any reference to the function described. Is there something I might be missing?

EDIT 2: I replaced the flag instead of adding it. Forget what I said

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants