Skip to content

Commit 02c4e1e

Browse files
committed
Add an inline_js attribute for procedural macros
1 parent 32952b5 commit 02c4e1e

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

text/006-local-js-dependencies.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ on local JS files.
1717
}
1818
```
1919

20+
* The `inline_js` attribute can now be used to import JS modules inline:
21+
22+
```rust
23+
#[wasm_bindgen(inline_js = "export function foo() {}")]
24+
extern "C" {
25+
fn foo();
26+
}
27+
```
28+
2029
* The `--browser` flag is repurposed to generate an ES module for the browser
2130
and `--no-modules` is deprecated in favor of this flag.
2231

@@ -71,26 +80,33 @@ here.
7180
### New Syntactical Features
7281

7382
The most user-facing change proposed here is the reinterpretation of the
74-
`module` attribute inside of `#[wasm_bindgen]`. It can now be used to import
75-
local files like so:
83+
`module` attribute inside of `#[wasm_bindgen]` and the addition of an
84+
`inline_js` attribute. They can now be used to import local files and define
85+
local imports like so:
7686

7787
```rust
7888
#[wasm_bindgen(module = "/js/foo.js")]
7989
extern "C" {
8090
// ... definitions
8191
}
92+
93+
#[wasm_bindgen(inline_js = "export function foo() {}")]
94+
extern "C" {
95+
fn foo();
96+
}
8297
```
8398

84-
This declaration says that the block of functions and types and such are all
85-
imported from the `/js/foo.js` file, relative to the current file and rooted at
86-
the crate root. The following rules are proposed for interpreting a `module`
87-
attribute.
99+
The first declaration says that the block of functions and types and such are
100+
all imported from the `/js/foo.js` file, relative to the current file and rooted
101+
at the crate root. The second declaration lists the JS inline as a string
102+
literal and the `extern` block describes the exports of the inline module.
103+
104+
The following rules are proposed for interpreting a `module` attribute.
88105

89106
* If the strings starts with the platform-specific representation of an absolute
90107
path to the cargo build directory (identified by `$OUT_DIR`) then the string
91108
is interpreted as a file path in the output directory. This is intended for
92-
procedural macros or build scripts which generate JS files as part of the
93-
build.
109+
build scripts which generate JS files as part of the build.
94110

95111
* If the string starts with `/`, `./`, or `../` then it's considered a path to a
96112
local file. If not, then it's passed through verbatim as the ES module import.
@@ -104,6 +120,10 @@ attribute.
104120
This will hopefully roughly match what programmers expect as well as preexisting
105121
conventions in browsers and bundlers.
106122

123+
The `inline_js` attribute isn't really intended to be used for general-purpose
124+
development, but rather a way for procedural macros which can't currently today
125+
rely on the presence of `$OUT_DIR` to generate JS to import.
126+
107127
### Format of imported JS
108128

109129
All imported JS is required to written with ES module syntax. Initially the JS

0 commit comments

Comments
 (0)