Skip to content

RFC: Import attributes for external module FFIs #6500

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
cometkim opened this issue Nov 29, 2023 · 5 comments
Closed

RFC: Import attributes for external module FFIs #6500

cometkim opened this issue Nov 29, 2023 · 5 comments
Milestone

Comments

@cometkim
Copy link
Member

cometkim commented Nov 29, 2023

Background

ESM Import Attributes has reached stage 3 (Import Assertion is deprecated, so it will never get standardized)

This is important because it can be massively used by various tools around the JavaScript ecosystem and even native ESM modules.

Previously, if we wanted to modify module semantics, we had to customize the complex configuration of the tool like bundlers. But now it can be done at module level.

For example, in the import statement for an image asset we could do this:

import imageDataUrl from './image.png' with { loader: 'url-loader' }
import imageUrl from './image.png' with { loader: 'file-loader' }

Another example is importing WebAssembly assets:

const wasmInstance = await import("foo.wasm", { type: "module", with: { type: "webassembly" } });

// or directly create a module worker
new Worker("foo.wasm", { type: "module", with: { type: "webassembly" } });

ReScript @module FFI currently lacks the resolution for this.

Detailed Design

This proposes a way to express import attributes by extending the existing FFI syntax.

@module syntax

If import attribute is needed, add { with: { ... } } argument to the @module directive.

The argument payload should be validated as a record type

type modulePayload<'a> = {
  from?: string,
  with?: ({..} as 'a),
}
// ModuleBindings.res
@module({ from: "foo.wasm", with: { "type": "webassembly" } }) @val
external wasmInstance: WebAssembly.Instance.t = "default"

@module({ from: "schema.json", with: { "type": "json" } }) @val
external jsonSchema: Json.t = "default"

// allow using it on non-default target
@module({ with: { "my-custom-loader": "i18n" } }) @val
external messages: Messages.t = "i18n.js"

should be compiled to:

import WasmInstance from "foo.wasm" with { type: "webassembly" };
import SchemaJson from "schema.json" with { type: "json" };
import * as Messages from "i18n.js" with { "my-custom-loader": "i18n" };

var wasmInstance = Instance;
var jsonSchema = SchemaJson;
var messages = Messages;

export {
  wasmInstance,
  jsonSchema,
  messages,
};

Dynamic imports

Dynamic imports include full module import (module mod = await MyModule) and partial import syntax (Js.import(MyModule.fn)).

Because ReScript modules are always JavaScript modules, the dynamic imports syntax doesn't need import attributes.

Note on dynamic imports of external modules
await Js.import(ModuleBindings.wasmInstance);

will be compiled to

await import("./ModuleBindings.bs.js").then(function (m) {
  return m.wasmInstance;
});

Which is legit.

However, its behavior isn't semantically equivalent to await import("foo.wasm", { type: "module", with: { type: "webassembly" } }). The ReScript version always requires additional loading of JavaScript modules, which adds unnecessary waterfall.

Maybe we need to reconsider its design or extra syntax to directly import assets.

CommonJS

There is no equivalent semantics in CommonJS. Users can still use the syntax, but the output will not change.

Exports

There are no specific use cases for exports attributes. Since ReScript's expression and output are always about JavaScript.

Maybe useful some integration cases

https://twitter.com/KrComet/status/1750541534074925441

@zth
Copy link
Collaborator

zth commented Dec 5, 2023

This is good stuff, and definitively needed! Would be good to assess how much work/complexity implementing this would be.

@cometkim

This comment was marked as resolved.

@zth
Copy link
Collaborator

zth commented Dec 29, 2023

You could theoretically make it a tuple but I don't think that really helps.

@cometkim cometkim changed the title RFC: Import attributes for external module FFIs [RFC] Import attributes for external module FFIs Jan 3, 2024
@zth
Copy link
Collaborator

zth commented Feb 1, 2024

@cometkim is there a spec somewhere for what's allowed in import attributes?

@zth zth added this to the v12 milestone Feb 1, 2024
@cometkim
Copy link
Member Author

cometkim commented Feb 1, 2024

@zth zth mentioned this issue Feb 1, 2024
@cometkim cometkim changed the title [RFC] Import attributes for external module FFIs RFC: Import attributes for external module FFIs Feb 1, 2024
@cometkim cometkim closed this as completed Feb 7, 2024
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

2 participants