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

How should I load JS libraries from within WASM? #55806

Open
nex3 opened this issue May 22, 2024 · 3 comments
Open

How should I load JS libraries from within WASM? #55806

nex3 opened this issue May 22, 2024 · 3 comments
Labels
area-dart2wasm Issues for the dart2wasm compiler. area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. web-js-interop Issues that impact all js interop

Comments

@nex3
Copy link
Member

nex3 commented May 22, 2024

(Potentially related to #55733)

Currently, it's unclear what the "well-paved road" is for loading JavaScript libraries within a WASM context. For built-in JS APIs, it's easy: I use dart:js_interop to define the shape of the JS API I want to use and tell Dart what its global name is, and then it gets wired up for me in the generated mjs wrapper. But what if I want to access a name that's not globally available in every context?

Any global name I write via dart:js_interop gets translated in the mjs wrapper to globalThis.<name>. But adding values to this isn't very ergonomic; for example, you can't write:

import * as lib from 'library';
globalThis.lib = lib;
import {instantiate, invoke} from './myapp.mjs';

myapp.mjs won't see globalThis.lib. You'd have to put that assignment in a third library in order for it to work. Is that really the expected pattern here? Or should there be a better way?

@osa1 osa1 added area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. area-dart2wasm Issues for the dart2wasm compiler. labels May 22, 2024
@srujzs srujzs added the web-js-interop Issues that impact all js interop label May 22, 2024
@srujzs
Copy link
Contributor

srujzs commented May 22, 2024

Would importing libraries via the dynamic import make this easier? You can define an interface for the resulting JSObject module.

@nex3
Copy link
Member Author

nex3 commented May 22, 2024

Asynchronous loads are not feasible in this case.

@medz
Copy link

medz commented Jul 11, 2024

We urgently need an unsafe static definition of methods such as' import xxx from xxx 'and' export xxx '. When I learned about the implementation of dynamic pouring, it seemed that it was very easy for Dart SDK to implement it.

With true top attribute access, we can use Dart to develop any content that complies with CJS or ESM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dart2wasm Issues for the dart2wasm compiler. area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. web-js-interop Issues that impact all js interop
Projects
None yet
Development

No branches or pull requests

4 participants