https://github.com/menci/wastyle
AStyle code formatter compiled to WebAssembly, for Node.js and the browser.
https://github.com/menci/wastyle
Last synced: about 1 year ago
JSON representation
AStyle code formatter compiled to WebAssembly, for Node.js and the browser.
- Host: GitHub
- URL: https://github.com/menci/wastyle
- Owner: Menci
- License: mit
- Created: 2020-03-04T08:50:31.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-18T21:09:12.000Z (about 3 years ago)
- Last Synced: 2025-04-18T15:17:31.831Z (over 1 year ago)
- Language: C++
- Homepage:
- Size: 572 KB
- Stars: 19
- Watchers: 3
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WAstyle
AStyle code formatter compiled to WebAssembly, for Node.js and the browser.
[](https://travis-ci.org/Menci/wastyle)
[](https://david-dm.org/Menci/wastyle)
[](http://commitizen.github.io/cz-cli/)
[](https://github.com/prettier/prettier)
[](LICENSE)
# Install
Install via NPM:
```bash
$ yarn add wastyle
```
# Usage
Firstly, you need to initialize AStyle WASM library with `init` function.
Unfortunately this step couldn't be done automatically since you need make Webpack emit the AStyle WASM binary to your dist directory and tell us its URL. Since we support running in browser mainly, so I don't want to write code to detect Node.js to increase your Webpack bundle size. So in both Node.js and Webpack this step must be done.
In Node.js, call `init` with a `Buffer` from `fs.readFile` or `fs.readFileSync`:
```javascript
const fs = require("fs");
const util = require("util");
const wastyle = require("wastyle");
util.promisify(fs.readFile)("wastyle/dist/astyle.wasm")
.then(data => wastyle.init(data))
.then(() => console.log("WAstyle is ready!"))
.catch(err => console.error(err));
```
In Webpack, I recommend you to use [`file-loader`](https://webpack.js.org/loaders/file-loader/) to emit the AStyle WASM binary to your dist directory and get its URL. See [NeekSandhu/onigasm#2](https://github.com/NeekSandhu/onigasm/issues/2).
Note that Webpack has a very simple built-in WASM loader, which will load a WASM file (detected by its magic header) as a compiled and instantiated WASM instance. But it doesn't support passing imports to WASM module. So it WILL fail since the AStyle WASM binary needs [WASI](https://wasi.dev/) to run. To disable the build-in WASM loader and force `file-loader` to produce the file URL, use this rule: ([webpack/webpack#6725](https://github.com/webpack/webpack/issues/6725))
```javascript
{
test: /\.wasm$/,
loader: "file-loader",
type: "javascript/auto"
}
```
In your code, initialize WAstyle with:
```javascript
import { init, format } from "wastyle";
import astyleBinaryUrl from "wastyle/dist/astyle.wasm";
init(astyleBinaryUrl).then(() => console.log("WAstyle is ready!"));
```
The `init` function will compile and instantiate the Astyle WASM binary asynchronously. Will throw an error if failed.
After initialization, call the synchronous function `format` to use Astyle:
```javascript
const [success, result] = format("#include \nint main(){int 🦄,a,*b=a,c=🦄*2,*d=nullptr;return -1;}", "pad-oper style=google");
console.log(result);
// #include
// int main() {
// int 🦄, a, *b = a, c = 🦄 * 2, *d = nullptr;
// return -1;
// }
```
Besides, you can use `wastyle/dist/astyle-optimize-size.wasm` to reduce your Webpack dist's size.
# Performance
Tested on Chrome 80.0.3987.122 on an i7-6600U laptop, formatting [this](https://paste.ubuntu.com/p/NtGx85z9BK/) C++ file takes 15ms with `wastyle/dist/astyle.wasm` and 40ms with `wastyle/dist/astyle-optimize-size.wasm` in average of 100 samples.
# Development
You'll need `emcc` to build the Astyle WASM binary. Follow [this guide](https://emscripten.org/docs/getting_started/downloads.html) to install it.
```bash
$ yarn build # Build TypeSrript
$ yarn build:wasm # Build WASM
```
# License
This project is licensed under the MIT license.
Astyle code formatter (Artistic Style) is also licensed under the MIT license. You can find its [license](astyle/LICENSE.md) in the [`astyle`](astyle) folder.