虽然说只要高级语言能转换成 LLVM IR,就能被编译成 WebAssembly 字节码,官方也推荐c/c++的方式,但是让一个前端工程师去熟练使用c/c++显然是有点困难,那么TypeScript 的方式便是前端编写 WebAssembly 最佳选择。
要将TypeScript 编译为WebAssembly,就要用到
1 cnpm install --save-dev AssemblyScript/assemblyscript
执行过程如下,安装完成之后只有一个node_modules文件夹
1 E:\Code\assembly>cnpm install --save-dev AssemblyScript/assemblyscript 2 - [@AssemblyScript/assemblyscript] install from git github:AssemblyScript/assemblyscript, may be very slow, please keep patience 3 √ Installed 1 packages 4 √ Linked 15 latest versions 5 √ Run 0 scripts 6 Recently updated (since 2019-02-17): 1 packages (detail see file E:\Code\assembly\node_modules\.recently_updates.txt) 7 √ All packages installed (14 packages installed from npm registry, 1 packages installed from git, used 1m(network 1m), speed 10.27kB/s, json 14(31.43kB), tarball 707.23kB)
1 npx asinit .
就像使用express初始化项目一样,执行过程如下
1 E:\Code\assembly>npx asinit . 2 Version: 0.6.0 3 4 This command will make sure that the following files exist in the project 5 directory 'E:\Code\assembly': 6 7 ./assembly 8 Directory holding the AssemblyScript sources being compiled to WebAssembly. 9 10 ./assembly/tsconfig.json 11 TypeScript configuration inheriting recommended AssemblyScript settings. 12 13 ./assembly/index.ts 14 Exemplary entry file being compiled to WebAssembly to get you started. 15 16 ./build 17 Build artifact directory where compiled WebAssembly files are stored. 18 19 ./build/.gitignore 20 Git configuration that excludes compiled binaries from source control. 21 22 ./index.js 23 Main file loading the WebAssembly module and exporting its exports. 24 25 ./package.json 26 Package info containing the necessary commands to compile to WebAssembly. 27 28 The command will try to update exis