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

Webpack support #42

Open
arcanis opened this issue Dec 4, 2016 · 6 comments
Open

Webpack support #42

arcanis opened this issue Dec 4, 2016 · 6 comments

Comments

@arcanis
Copy link
Contributor

arcanis commented Dec 4, 2016

I just noticed that nbind apparently isn't compatible with Webpack yet. Supporting it would be a great addition, especially since the asm.js compilation already works !

Basically, in order to be webpack-compliant, the loading should be greatly simplified to remove most of require calls, especially dynamic ones, at least in the browser codepath (Webpack can look for a "browser" property in the package.json, instead of "main"). For example, maybe by allowing nbind.init() to take an argument that would actually be the asm.js file preloaded (so that all the requirement logic could be bypassed).

@jjrv
Copy link
Member

jjrv commented Dec 4, 2016

I think the compiled asm.js file itself should be webpack compatible. You could use the included copyasm tool to copy that to some known location, point the "browser" property to it, and that should be all, I hope.

If you compile with optimizations on (as you should), the Emscripten-generated .js.mem file might cause some issues... Let me know if you get it working, I haven't used webpack much (only SystemJS).

@arcanis
Copy link
Contributor Author

arcanis commented Dec 4, 2016

Thanks, I'll check this. It however seems that copyasm is copying my nbind.node file (instead of nbind.js and nbind.js.mem).

@jjrv
Copy link
Member

jjrv commented Dec 4, 2016

That sounds like an oversight, I've only tested it with asm.js output in the build directory. It should work if you remove the .node file.

@jjrv
Copy link
Member

jjrv commented Dec 5, 2016

I've been debugging your other issue in a browser, like this:

<script src="build/Release/nbind.js"></script>
<script src="test.js"></script>

test.js contains:

nbind.init(function(err, binding) {
  var lib = binding.lib;

  var point = new lib.Point(42, 66);
  // ...
  lib.test(point);
});

NOTE: I changed -O3 to -g4 inside nbind's src/nbind.gypi to generate source maps and avoid loading a .js.mem file.

Webpack shouldn't have a problem with those scripts. The asm.js output is wrapped in UMD format, so you don't need to work with a global nbind variable if you use a loader like Webpack.

@arcanis
Copy link
Contributor Author

arcanis commented Dec 17, 2016

Is there a synchronous API ? I'm not sure asynchronous APIs are Webpack cup of tea (ideally, it should be able to resolve every module at build time, and chose itself whether they should be loaded asynchronously or not, so that the user isn't aware of the actual asset loading process) :(

I'm currently using this code, but even if it works, it feels very hacky:

var setupExports = require('./entry-common');

var hasRan = false;

var bind = null, lib = {};
var asmjs = require('../build/Release/nbind.js')

asmjs(lib, function (err, parts) {

    if (err)
        return;

    hasRan = true;
    bind = parts.bind;

});

if (!hasRan)
    throw new Error('This module hasn\'t been correctly initialized');

setupExports(module, bind, lib);

@k2snowman69
Copy link

I know it's been a year since this issue had any work done on it but hoping you maybe able to help here... I'm trying to do an emscripten + webpack solution (which would likely be the same solution for nbind). That said, running into issues but I figure I would cross post and hopefully if my issues get resolved, maybe the solution can be applied here!
https://stackoverflow.com/questions/47957849/webpack-cannot-find-module-when-integrating-emscripten

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

No branches or pull requests

3 participants