Skip to content

Improve Webpack loader #120

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

Closed
xtuc opened this issue May 23, 2018 · 10 comments
Closed

Improve Webpack loader #120

xtuc opened this issue May 23, 2018 · 10 comments

Comments

@xtuc
Copy link

xtuc commented May 23, 2018

With the new wasm pipeline in Webpack (> 4.8.0), loader can now directly emit a wasm binary.

I have a very simple demo here: https://github.com/xtuc/c-webpack-demo.

The idea would be to allow:

  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: "@assemblyscript/loader",
        type: "webassembly/experimental"
      }
    ]
  },

and the user would just:

import("./foo.ts").then(...)
@dcodeIO
Copy link
Member

dcodeIO commented May 23, 2018

Thanks for the insights! Does the import in this case return a WebAssembly.Module one can instantiate? Ultimately I'd like @assemblyscript/loader to provide the utility mentioned in the README and also integrate with webpack. The challenge here seems to be to return something that can a) be instantiated by providing the necessary imports and b) matches the .d.ts generated by the compiler by running the demangler on the WASM exports first.

@dcodeIO
Copy link
Member

dcodeIO commented May 23, 2018

Btw. here's a prototype for the webpack component, but I'm not quite content with the base64 encoding step because it breaks source map support.

@xtuc
Copy link
Author

xtuc commented May 23, 2018

Does the import in this case return a WebAssembly.Module one can instantiate

No it will return the WebAssembly.Module.exports basically.

Ultimately I'd like @assemblyscript/loader to provide the utility mentioned in the README

We'll use instantiateStreaming when available and fallback if not.

For demangle you have the object with the exports, is that ok?

be instantiated by providing the necessary imports

Webpack will do the linking, you need to emit and require local files.

prototype for the webpack

Yes I saw it, synchronous + inline buffer approach has many disadvantages. I wouldn't recommend it.

@dcodeIO
Copy link
Member

dcodeIO commented May 23, 2018

I see, thanks. So proper integration with webpack will require support for external imports in AssemblyScript first because there is no other way to specify imports :)

@xtuc
Copy link
Author

xtuc commented May 23, 2018

Yeah, maybe we can keep it internal just to test the integration but that would also enable a better integration with the user's JavaScript code.

@dcodeIO
Copy link
Member

dcodeIO commented May 23, 2018

Yeah, one of the reasons why it's currently using the declare syntax and picking imports from ambient contexts is that this way both import styles can be supported eventually, with imports mapping to what will become es module integration (as now in webpack, I suppose), and declare remaining as a more direct mechanism to specifiy import namespaces manually (i.e. for non-web embeddings). So webpack might be a good initial testing ground for es module integration.

@trusktr
Copy link
Member

trusktr commented Dec 20, 2019

Does the import in this case return a WebAssembly.Module one can instantiate?

I think it'd be great (if this isn't the case already) that an AssemblyScript module could export a class or function, and that the JS side could use it like a class or function with the same types as defined on the AssemblyScript side (internally the module would wrap the WASM Module and make the wrapper methods call into the WASM module, etc).

Eventually the internal implementation could be replaced with references and object types to make it fast.

@trusktr
Copy link
Member

trusktr commented Dec 20, 2019

Using the example from assemblyscript-typescript-loader, if we have some AssemblyScript code:

// ... code omitted for brevity ...
export function step(): void {
  // ... code omitted for brevity ...
}

then instead of importing it into regular TypeScript like this:

import asmPromise from "./assemblyscript/moduleEntry.ts";
asmPromise().then(function(asmModule){
  // here you can use the wasm.exports
  asmModule.step();
})

we'd import it like the following (which will work with top-level await):

import {step} from "./assemblyscript/moduleEntry.ts";

// use the wasm.exports
step()

@dcodeIO
Copy link
Member

dcodeIO commented May 27, 2020

Closing this issue as part of 2020 vacuum because it has been lingering for quite some time without being resolved. If it's still an issue important to you, feel free to open a new one!

@dcodeIO dcodeIO closed this as completed May 27, 2020
@jerrygreen
Copy link

jerrygreen commented Mar 31, 2021

Wait, does that mean I can setup my webpack config like that? Or there should be some glue-code from @assemblyscript/loader, too?

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

No branches or pull requests

5 participants