skoppe / spasm

Write single page applications in D that compile to webassembly
MIT License
218 stars 17 forks source link

LinkError: import object field 'getRoot' is not a Function #31

Open WebFreak001 opened 5 years ago

WebFreak001 commented 5 years ago

tried code

import spasm.spa;

@safe:
nothrow:

struct App
{
    mixin Node!"div";
    @prop innerText = "Hello World!";
}

mixin Spa!App;

in a fresh project. I tried looking around for hints in the readme but didn't really find anything (the README doesn't even tell me to import spasm.spa;)

I then found the function in the spasm/modules/spa.js but had no idea how to even use that and why it's not added. Checked out the spasm/modules/index.js because that looked promising and at the top it told me it's generated with dub run spasm:webpack-bootstrap

I tried running dub run spasm:webpack-bootstrap then and it said that doesn't exist as it is in fact dub run spasm:bootstrap-webpack

Looking for this in the README it says to run dub spasm:webidl -- --bindgen when adding JS bindings.

I tried that and dub complained that it's an invalid command (it should be dub run spasm:webidl -- --bindgen)

Now after it's generated with that file it says the file is generated using that bindgen command and some new "bindings.js" got added but I have no idea where that code even came from and what's the difference from not having it.

Could you maybe add some more information about the index.js file and what the bootstrap-webpack and idl binding generator actually do? Do they parse my D code to determine the libraries or how do they work?

skoppe commented 5 years ago

You are probably right, I need to write out some more stuff and what it does.

Spasm needs js glue code to function. dub run spasm:bootstrap-webpack generates the required js files. They are put into the spasm folder (plus a dev-server.js, package.json and a webpack.config.js in the root folder required for bundling and running the dev server).

A simple npm run start will load up the dev server on localhost:3000. a npx webpack will generate a production ready bundled index.html.

The spasm.bindings.* modules require more js glue code. The glue code gets created when you run dub run spasm:webidl -- --bindgen. It will read the wasm binary that you get after compiling and will use the webidl definition files to generate js glue code and put all that in the bindings.js file. It is only necessary when you actually call any of the spasm.bindings.* functions.

See for instance the generated bindings.js file in the dom example https://github.com/skoppe/spasm/blob/master/examples/dom/spasm/modules/bindings.js#L50