In the case of this application, building refers to compiling the lib/main.c to a .wasm file using the emcc command. Since this is a Node.js project, we can use the scripts key in our package.json file to define Tasks. You can still use VS Code's Tasks feature because it automatically detects the scripts from your package.json file and presents them in the list of tasks when you select Tasks | Run Task... from the menu. The following code contains the contents of the scripts section in this project's package.json file:
"scripts": {
"prebuild": "rimraf src/assets/main.wasm",
"build": "emcc lib/main.c -Os -s WASM=1 -s SIDE_MODULE=1
-s BINARYEN_ASYNC_COMPILATION=0 -s ALLOW_MEMORY_GROWTH=1
-o src/assets/main.wasm",
"start": "node src/index.js",
"watch": "nodemon src/* --exec 'npm start'"
},
The build script was split across multiple lines for display purposes, so you'd have to combine those lines for valid JSON. The prebuild script removes the existing Wasm file, and the build script runs the emcc command with the required flags to compile lib/main.c and output the result to src/assets/main.wasm. To run the script, open a terminal within the /server-example folder and run the following command:
npm run build
If the /src/assets folder contains a file named main.wasm, the build completed successfully. If an error has occurred, the terminal should provide a description of the error, as well as a stack trace.