-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implement the local JS snippets RFC #1295
Conversation
8b0811a
to
bf9fcd9
Compare
Awesome! I'll try to port |
bf9fcd9
to
2746805
Compare
ccad626
to
712bad2
Compare
This commit is an implementation of [RFC 6] which enables crates to inline local JS snippets into the final output artifact of `wasm-bindgen`. This is accompanied with a few minor breaking changes which are intended to be relatively minor in practice: * The `module` attribute disallows paths starting with `./` and `../`. It requires paths starting with `/` to actually exist on the filesystem. * The `--browser` flag no longer emits bundler-compatible code, but rather emits an ES module that can be natively loaded into a browser. Otherwise be sure to check out [the RFC][RFC 6] for more details, and otherwise this should implement at least the MVP version of the RFC! Notably at this time JS snippets with `--nodejs` or `--no-modules` are not supported and will unconditionally generate an error. [RFC 6]: rustwasm/rfcs#6 Closes rustwasm#1311
The cwd is different for workspaces, so use the manifest env var instead.
When importing a file across multiple locations in a module make sure it doesn't trip an assert and it works as expected.
712bad2
to
33494a2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @alexcrichton :)
if contents.starts_with("class") { | ||
format!("{1}\n__exports.{0} = {0};\n", name, contents) | ||
} else { | ||
format!("__exports.{} = {};\n", name, contents) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't actually need to have two different cases here, since this is valid JS:
__exports.MyClass = class MyClass { ... };
Same for the other bundling modes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was unwaware! When I started to do this though it looks like that idiom doesn't also define a MyClass
identifier in the current scope which the codegen currently relies on (but export class Foo {... }
does declare Foo
in the local scope which is what we need)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha.
Co-Authored-By: alexcrichton <[email protected]>
Co-Authored-By: alexcrichton <[email protected]>
It's already hidden from docs!
Using `unsafe` was just a little too eager there so let's use an off-the-shelf solution for solving the actual problem we have, which is to allocate strings with a lifetime of `Interner` rather than deduplicating strings.
Use the same crate identifier for manually included snippets as well as inline snippets to help with debugging.
Thanks for the thorough review @fitzgen! (and so quick!) |
This looks like a substantial change. Maybe it should be mentioned in the 'Unreleased' section of the changelog. |
@Vlad-Shcherbina indeed yeah! We typically don't do a great job of keeping the changelog up to date as things land, but this'll definitely be mentioned for the next release! |
Whatever process works for you, obviously. |
This commit is an implementation of RFC 6 which enables crates to
inline local JS snippets into the final output artifact of
wasm-bindgen
. This is accompanied with a few minor breaking changeswhich are intended to be relatively minor in practice:
module
attribute disallows paths starting with./
and../
.It requires paths starting with
/
to actually exist on the filesystem.--browser
flag no longer emits bundler-compatible code, butrather emits an ES module that can be natively loaded into a browser.
Otherwise be sure to check out the RFC for more details, and
otherwise this should implement at least the MVP version of the RFC!
Notably at this time JS snippets with
--nodejs
or--no-modules
arenot supported and will unconditionally generate an error.