# @massalabs/as-transformer

> Massa AS transformer

Latest version **0.4.0** (published 2024-06-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install @massalabs/as-transformer
pnpm add @massalabs/as-transformer
yarn add @massalabs/as-transformer
bun add @massalabs/as-transformer
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2024-06-11 |
| First published | 2022-10-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 21 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | glibert, aurelienft, damip, evgenip, qdr |

## Links

- npm: https://www.npmjs.com/package/@massalabs/as-transformer
- npm.io page: https://npm.io/package/@massalabs/as-transformer

## Dependencies (8)

- [yaml](https://npm.io/package/yaml.md) ^2.3.1
- [husky](https://npm.io/package/husky.md) ^8.0.3
- [rimraf](https://npm.io/package/rimraf.md) ^5.0.5
- [ts-morph](https://npm.io/package/ts-morph.md) ^19.0.0
- [visitor-as](https://npm.io/package/visitor-as.md) ^0.11.4
- [@types/jest](https://npm.io/package/@types/jest.md) ^29.5.1
- [assemblyscript](https://npm.io/package/assemblyscript.md) ^0.27.18
- [@massalabs/as-types](https://npm.io/package/@massalabs/as-types.md) ^2.1.0

## Recent versions

- 0.4.0 (latest) — 2024-06-11
- 0.4.1-dev.20260923091402 (dev) — 2026-09-23
- 0.2.1 (buildnet) — 2023-06-26
- 0.4.1-dev.20240611142606 — 2024-06-11
- 0.4.1-dev.20240611142528 — 2024-06-11
- 0.3.3-dev.20240606143326 — 2024-06-06
- 0.3.3-dev.20240606142538 — 2024-06-06
- 0.3.3-dev.20240529131344 — 2024-05-29
- 0.3.3-dev.20240529122344 — 2024-05-29
- 0.3.3-dev.20240524080933 — 2024-05-24
- 0.3.3-dev.20240521132240 — 2024-05-21
- 0.3.3-dev.20240510154659 — 2024-05-10
- 0.3.3-dev.20240322102305 — 2024-03-22
- 0.3.3-dev.20240322102220 — 2024-03-22
- 0.3.3-dev.20240219101218 — 2024-02-19
- … 178 more at https://npm.io/package/@massalabs/as-transformer/versions

## README

# `as-transformer`

**MassaLabs** as-transformer library.

An [AssemblyScript transformer](https://www.assemblyscript.org/compiler.html#transforms) helper module.

The complete documentation of all available functions and objects can be found here:

- [`as-transformer documentation`](https://as-transformer.docs.massa.net)

## Install

To install this module, run the following command at your project root directory :

```shell
npm install -D @massalabs/as-transformer
```

## Usage

Tell your editor where to find the transformer types in a typing file. For example `assembly/types.d.ts`.

```typescript
/// <reference types="@massalabs/as-transformer" />
```

You can use this transformer by adding `--transform @massalabs/as-transformer` to your asc command.

For instance, to compile `assembly/my_sc.ts` with this transformer you will execute:

```shell
npx asc --transform @massalabs/as-transformer assembly/my_sc.ts --target release --exportRuntime -o build/my_sc.wasm
```

### file2ByteArray

This transformer loads the given file, encodes it to `StaticArray<u8>` and then replace the call to `file2ByteArray` by the encoded content.

Example:

```typescript
export function main(_args: string): i32 {
    const bytes = fileToByteArray('./build/sc.wasm'); // will read `build/sc.wasm`, will encode it in array and then put the result in a string used to initialize `bytes`.
    const sc_addr = createSC(bytes);
    call(sc_addr, "advance", "", 0);
    generateEvent("SC deployed at addr: " + sc_addr);
    return 0;
}
```

### @massaExport

This transformer allows the creation of function without having to worry about the serialization/deserialization of the arguments or the return value.

By adding the decorator `@massaExport` the transformer will generate a wrapper that takes care of it.

Example:
```typescript
@massaExport()
export function sayHello(accountName: string) : string {
    return "Hello, " + accountName + "!";
}
```
Instead of:
```typescript
export function sayHello(args: StaticArray<u8>) : StaticArray<u8> {
    let deserArgs = new Args(args);
    const accountName = deserArgs.nextString();
    const message = "Hello, " + accountName + "!";

    return stringToBytes(message);
}
```

#### Known limitation(s)

At the moment only the smart contract main file can use this transformer.
Any functions outside this file, that are imported and re-exported can not use this decorator.

⚠️ @massaExport cannot be used with the constructor function.
This is because the deployer uses "Args" to interact with functions during deployment. This problem should be solved soon.


## Contributing
We welcome contributions from the community!

If you would like to contribute to as-transformer, please read the [CONTRIBUTING file](CONTRIBUTING.md).

## License
as-transformer is released under the [MIT License](LICENSE).

## Powered By
as-transformer is developed with love by MassaLabs and powered by a variety of [open-source projects](powered-by.md).

---
_Source: https://npm.io/package/@massalabs/as-transformer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
