9 WebAssembly for refactoring JavaScript
This chapter covers
- Writing a Rust library to be used in JavaScript
- Integrating WebAssembly into an existing JavaScript project and component
- Writing a web component entirely in Rust and importing it into an existing project
Finding a single language with which to develop all parts of an application has been a goal for many who create programming languages. “Write once, run anywhere” was a tagline for Java because, at the time, it seemed like as long as a system could run Java’s virtual machine, your application would run there, too. Obviously, this had its limitations, but in essence, it was what made Java such a popular platform, even to this day. This idea of cross-platform software isn’t new; in fact, it was a goal of early compilers to allow programmers to write an application once and compile it to run on other machines.
Rust, as we have seen, follows this same pattern. Instead of working like Java—that is, having a virtual machine to run an application—Rust uses different compile targets. Additionally, the examples we have looked at so far have relied, on some level, on Rust’s C integration for importing libraries. In this chapter, we are going to explore a new approach to “write once, run anywhere,” but instead of writing Java (breathe a sigh of relief), we will be working with a technology that was built to be portable for the web.