Cargo xtask

Cargo xtask

I rewrote the matrix operation for mine sweeper app. Before 2020 May, The app used pure javascript library for matrix operation. On May 2020, I changed the matrix operation with web assembly. The web assembly would run matrix operation faster than javascript, for not checking data type in every operation.

The language for wasm

I chose Rust language for web-assembly. I knew that rust could generate web-assembly. I was interested in the rust language at that time. Because I saw an article said Rust is in higher ranking among software developers. In my opinion, it is like modern style c++ language. Rust has standard package manager “Cargo”. The cargo manage dependencies some libraries for your app or library. It is just like npm in node, composer in php or pip in python. You don’t have to prepare some libraries in your system by hand.

Customizing wasm-bindgen

If you have web-assembly, you want to use wasm-pack. I also try to use wasm-pack. I got a web-assembly and javascript interface for it by wasm-pack. I needed to use this web-assembly with kotlin application. Kotlin is static strong typed language. With type declaration files, you will have the benefit of kotlin language. Rust is also strong typed language. It is easy to have type declaration files with wasm-pack. But the generated typed declaration files did not satisfy me. The generated file did not compile web-assembly lately. I want to fetch web-assembly file, compile it and use it lately. I had to customize wasm-pack to generate type declation file for my requirements. I investigated some source files in wasm-pack, I understood that type declaration file was generated by not wasm-pack but wasm-bindgen. Wasm-pack is simple wrapper wasm-bindgen. I customized wasm-bindgen. I would like to make wasm-pack use my customized wasm-bindgen, but I did not do it. Wasm-pack was written hard coded url to get for wasm-bindgen. At that time, I understood the wasm-bindgen well, I did not need wasm-pack anymore.

Run wasm-bindgen after build lib

You need to run wasm-bindgen after you build library as wasm32-unkonwn-unknown. Wasm-bindgen seemed to get some informantion from the library to build web-assembly and type declaration file. I tried to find Cargo standard way to run some command after build library, but I could not. Cargo offers only the way to run some commands before you build library. I find some Cargo tools to run after you build library. I found “cargo-make” to do my requirement but I have to learn another syntax about it. It’s ok at that time. But I would forget the syntax 6 months later for the another language project I focus on. I would like to use rust language to run some custom commands after you build a library. I found a discussion about post running commands on Cargo build system. I thought that it take long time to make Cargo have the way to run some commands automatically after cargo build. In the discussion, I found “xtask” idea. The xtask is not a tool like cargo-make. It is the way to make cargo run additional tasks. The xtask is a workspace for cargo. It is nice for me to run some custom tasks with cargo. I could use rust language to make custom task.

Release web-assembly and use it

I could push my project on github.com and release the web-assembly in npm package directory. The library is used in Mine sweeper app now.