-
Notifications
You must be signed in to change notification settings - Fork 70
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
Various quality of life improvements. #63
Conversation
@xtuc Ping. |
I have this on my radar, sorry for the delay. |
Thanks for your PR!
I'm not sure to understand what is breaking? You mean people that passed |
Yes, that does cause breakage. But the other breakage is that it now defaults to So for example, if somebody has a package called That means basically all existing users of |
By the way, I've been thinking about how to support Rust crates which have multiple binaries. So there will probably be some more breaking changes soon, so we should probably hold off on merging this PR until that's done. |
wasm-pack emits a |
It seems that I have an outdate version of
Would it be possible, if you know, to document the versin needed? Thanks! I'm not able to make the example work again, but your PR give better UX! |
There is no requirement for the user to use the For example, in the
This works because the package's name is But that will now break with this PR, since it now generates a
It seems to be 0.8.0, released on April 2nd. I'll update the PR. |
So I did some experiments with making Both Webpack and wasm-pack aren't really designed for that use-case, and so I think Webpack plugins are a bad fit. Instead, I created a Webpack loader in ~200 lines of code which works fantastic:
Basically, it's better in essentially every way, with no downsides (currently). I'm interested in uploading the code to I still think this PR has merit, since it helps out the current users of |
(I added in a note about the minimum wasm-pack version) |
Really cool! I recreated the repo and added you as collab. At first I initially created a loader but had issues with the hotreloading If I remember correctly. I'm happy to switch back to a loader if it adds improvements and better UX, thanks for your work! |
I'm not able to run the example because it's upgrading some deps and I'm running into rustwasm/wasm-bindgen#1613. I'm generally ok with your PR, I just want to upgrade the example and test it. |
Great, thanks! I'll make sure to test hotreloading to see if it works correctly. You can fix that wasm-bindgen error by setting this in your [dependencies]
wasm-bindgen = "=0.2.45" Then wasm-pack should auto-download the right version of wasm-bindgen. |
So I tested hot reloading with |
Thanks, I will try to take a look at it. I went ahead and updated the demo on your branch. |
} | ||
|
||
apply(compiler) { | ||
this.isDebug = this.forceMode ? this.forceMode === "development" : compiler.options.mode === "development"; | ||
|
||
// This fixes an error in Webpack where it cannot find | ||
// the `pkg/index.js` file if Rust compilation errors. | ||
this._makeEmpty(); |
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.
If you already imported an non-existing module with webpack, it will tell you in the console and at runtime, It has quite a good UX IMO. We could create a file with a ThrowStatement with the compilation error in it.
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 don't think the UX is good. This is what you get with the old version:
error[E0599]: no function or associated item named `from_str2` found for type `wasm_bindgen::JsValue` in the current scope
--> src\lib.rs:24:30
|
24 | console::log_1(&JsValue::from_str2("Hello world!"));
| ^^^^^^^^^
| |
| function or associated item not found in `wasm_bindgen::JsValue`
| help: there is a method with a similar name: `from_str`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0599`.
error: Could not compile `rust-webpack-template`.
To learn more, run the command again with --verbose.
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit code: 101
wasm-pack error: undefined
Hash: 3b93c8acec8101b6a7f9
Version: webpack 4.35.2
Time: 1818ms
Built at: 07/02/2019 2:46:07 PM
1 asset
Entrypoint index = index.js
[0] ./js/index.js 49 bytes {0} [built]
ERROR in ./js/index.js
Module not found: Error: Can't resolve '../pkg/index.js' in 'C:\Users\Pauan\Shared Folders\NixOS\tmp\my-app\js'
@ ./js/index.js 1:0-25
error Command failed with exit code 2.
This part here is useless, and it actually pushes away the useful information:
ERROR in ./js/index.js
Module not found: Error: Can't resolve '../pkg/index.js' in 'C:\Users\Pauan\Shared Folders\NixOS\tmp\my-app\js'
@ ./js/index.js 1:0-25
So my changes gets rid of it. You still get an error message, just without the useless stuff.
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.
Sorry, I typed the comment too fast. I just wanted to say that we could show the error on the browser-side as well, by injecting code with a ThrowStatement in ./pkg/index.js
.
Also, the relevant source is a couple of line below.
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.
As for putting the compilation error into a file (or whatever), I tried that but it's not possible.
When you spawn a command (like wasm-pack build
) you have two options: you can pipe the output, or you can inherit the stdin
/stdout
/stderr
.
If you pipe the output, then you can store it in a file, but then you don't get any colors from cargo build
.
If you inherit the output, then you get nice colors with cargo build
, but then you can't retrieve the output, so you can't put it into a file.
I tried all sorts of workarounds, nothing worked. So I gave up.
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.
Thank you for your work!
|
@xtuc The error was Webpack saying it can't resolve the file (even though the file clearly existed). And whenever the file changed, Webpack wouldn't pick up the changes, it would keep using the old stale file. This only happened with hot module reloading. Otherwise it worked perfectly. |
Here is a list of changes in this PR:
Adding in
outDir
andoutName
options.It now watches
Cargo.toml
for changes.It fixes Webpack erroring about
pkg/index.js
ifwasm-pack
errors.Changing to use Webpack's error system, which causes Webpack compilation to gracefully stop if there is an error.
The end result is a much nicer experience for the end user:
It no longer spews out a lot of useless Webpack information when compilation fails.
It works perfectly in watch mode, with full auto-reloading, even in the case that there are errors in the Rust code.
The user can now edit
Cargo.toml
and it will auto-rebuild.However, this is a BREAKING CHANGE, because it now defaults
--out-dir
topkg
, and defaults--out-name
toindex
. This is necessary in order to fix a Webpack error. Therefore, this will need a major version bump.