In this lecture, we are going to configure Web pack to compile rust, our file is ready to be sent
over to rust.
There's just one problem.
Rust does not run in the browser.
We can't compile rust to an executable file.
Rust needs to be compiled into a web assembly file.
We can perform this process manually, but I would rather automate this process.
Web Pack is capable of refreshing the application whenever we perform changes in our JavaScript files.
It would be convenient if it can compile our Rust program into Web Assembly two.
In the resource section of this lecture, I provide a link to a plugin called Web Assembly Pack.
This project was developed to compile rust projects into web assembly.
It works out of the box.
Best of all, it ships a plugin for Web Pack.
We can integrate this tool into our build flow to compile rust into web assembly.
If you click on the install link, you will be provided with various installation methods.
This tool can be installed with npm.
Let's give that a try.
Switch over to your terminal.
Inside the command line, run the following command and p.m. install minus D at WASC tool slash WAC
impact plug in.
Next, we can start configuring this plugin with Web pack.
Open the Web pack config dot s file.
At the top of the file import, the WASC pack plug in with the required function.
Lastly, let's register the plug in with Web pack inside the plugins array, add a new instance of the
WSM pack plug in object.
Our plug in is going to need more information about our project specifically, it needs to know where
to find our robust project.
It just so happens that our Rust and JavaScript apps are written in the same directory.
We can configure the location of our project by passing in an object in this object at a property called
Create Directory.
The value for this property must be a false system path.
We have an idea of how to generate a full system path with the path dark resolve function.
Will pass in the underscore, underscore directory name, Constance, with a dot.
Overall, this function to return the root directory of our project.
This plugin will search for the cargo dot t0 Ml file.
Make sure you're pointing the plug into the root directory of the Rust Project.
Some beginners will try to point it directly to the source directory.
There's one more modification we need to make.
Open the cargo n'keal EML file.
We are compiling rust to Web assembly by doing so, you can define functions that are accessible via
JavaScript by default.
Rust assumes we're going to be calling rust functions with rust code.
We should tell Rust that we'll be able to call our functions from other languages.
We can do so by adding a table called library.
Under this table, set the create type option to CD, why lib?
The word crate is synonymous with the words library or package in other languages during compilation.
Rust produces a crate.
We're telling Rust to produce a crate with support for external languages.
We're finished configuring rust.
It's time to give our compiler a test inside the command line.
Run the npm run serve command.
After a couple of sections, Web pack will compile our project, it's also going to make changes to
our projects.
We have a new directory called Package, which is abbreviated to pcgg.
This folder will contain a copy of our web assembly files.
It's possible you may not see a WebAssembly file that's perfectly fine.
We haven't written code for WebAssembly.
As long as your project is being compiled, you should be good to go.
If you receive warnings from web pack, that's fine too.
It will go away as we work on our project.
In the next lecture, we're going to start writing some web assembly code.