# 入门

¥Getting started

为你的第一个 AssemblyScript 模块铺平道路。

¥Paving the way to your first AssemblyScript module.

# 设置新项目

¥Setting up a new project

确保安装了 最新版本的 Node.js (opens new window) 及其包管理器 npm(Node.js 附带的),然后切换到新目录并像往常一样初始化新的 Node.js 模块:

¥Make sure that a recent version of Node.js (opens new window) and its package manager npm (that comes with Node.js) are installed, then switch to a new directory and initialize a new Node.js module as usual:

npm init

安装 AssemblyScript 编译器。假设生产中不需要它,并将其设为开发依赖:

¥Install the AssemblyScript compiler. Let's assume that it is not required in production and make it a development dependency:

npm install --save-dev assemblyscript

安装后,编译器提供了一个方便的脚手架实用程序来快速设置新项目,在当前目录中:

¥Once installed, the compiler provides a handy scaffolding utility to quickly set up a new project, here in the current directory:

npx asinit .

asinit 命令自动创建推荐的目录结构和配置文件:

¥The asinit command automatically creates the recommended directory structure and configuration files:

  ./assembly
  Directory holding the AssemblyScript sources being compiled to WebAssembly.

  ./assembly/tsconfig.json
  TypeScript configuration inheriting recommended AssemblyScript settings.

  ./assembly/index.ts
  Example entry file being compiled to WebAssembly to get you started.

  ./build
  Build artifact directory where compiled WebAssembly files are stored.

  ./build/.gitignore
  Git configuration that excludes compiled binaries from source control.

  ./asconfig.json
  Configuration file defining both a 'debug' and a 'release' target.

  ./package.json
  Package info containing the necessary commands to compile to WebAssembly.

  ./tests/index.js
  Starter test to check that the module is functioning.

  ./index.html
  Starter HTML file that loads the module in a browser.

为了完整起见,asinit 支持以下选项:

¥For completeness, asinit supports the following options:

Sets up a new AssemblyScript project or updates an existing one.

SYNTAX
  asinit directory [options]

EXAMPLES
  asinit .
  asinit ./newProject -y

OPTIONS
  --help, -h            Prints this help message.
  --yes, -y             Answers all questions with their default option
                        for non-interactive usage.

# 使用你的模块

¥Working with your module

现在可以通过调用构建命令将 assembly/index.ts 中的示例编译为 WebAssembly:

¥The example in assembly/index.ts can now be compiled to WebAssembly by invoking the build command:

npm run asbuild

这样做会将编译的二进制文件、绑定和定义文件发送到 build/ 目录。

¥Doing so will emit the compiled binaries, bindings and definition files to the build/ directory.

tests/index.js 中生成的测试用例可以通过以下方式执行:

¥The generated test case in tests/index.js can be executed with:

npm test

构建后,该目录包含像任何其他现代 Node.js ESM 模块一样使用该模块的所有位:

¥Once built, the directory contains all the bits to use the module like any other modern Node.js ESM module:

import * as myModule from "myModule";

生成的 index.html 显示了如何在 Web 上使用该模块。为模块目录提供服务的 Web 服务器,默认显示 index.html,可以通过以下命令启动:

¥The generated index.html shows how the module can be used on the Web. A web server serving the module directory, defaulting to display index.html, can be started with:

npm start

请注意,根据用例,并非所有文件都是必需的,并且删除不需要的文件是安全的。如果出现问题,可以再次执行 asinit,这将恢复已删除的默认文件,同时保留已编辑的文件。

¥Note that not all of the files may be required depending on the use case, and it is safe to delete what's not needed. If anything goes wrong, asinit can be executed again, which would restore the deleted default files while keeping already edited ones.

# 未来的旅程

¥The journey ahead

到目前为止,一切都很好!现在是时候开始编辑项目了,这通常涉及:

¥So far, so good! Now it is time to start editing the project of course, which typically involves:

  • assembly/ 目录中编辑和添加源文件并更新 tests/ 中的测试。

    ¥Editing and adding source files within the assembly/ directory and updating the tests in tests/.

  • 调整 asconfig.json 中的 编译器选项 以满足你的项目需求。

    ¥Tweaking compiler options in asconfig.json to fit your project's needs.

  • 意识到 WebAssembly 仍有很长的路要走 🙂

    ¥Realizing that WebAssembly still has a ways to go 🙂

但快速入门就到此为止了。继续阅读 了解更多

¥But that's it already for a quick start. Read on to learn more!