Add simple readme and rename APIs

This commit is contained in:
Nick Gerleman
2022-12-23 03:08:21 -08:00
parent aaadf57b19
commit 51a2a3856e
7 changed files with 36 additions and 11 deletions

25
javascript/README.md Normal file
View File

@@ -0,0 +1,25 @@
# yoga-layout
This package provides JavaScript bindings for the Yoga layout engine as either WebAssembly (for browsers, Node) or asm.js (other clients or clients unable to read package export maps).
## Usage
The default entrypoint provides an asynchronous loader function to return a Yoga instance.
```ts
import { loadYoga, ALIGN_CENTER } from "yoga-layout";
const Yoga = await loadYoga();
const node = Yoga.Node.create();
node.setAlignContent(ALIGN_CENTER);
```
An alternative synchronous API is provided for compatibility, but requires using asm.js in browsers instead of WebAssembly, leading to worse performance and larger assets.
```ts
import Yoga, { ALIGN_CENTER } from "yoga-layout/sync";
const node = Yoga.Node.create();
node.setAlignContent(ALIGN_CENTER);
```

View File

@@ -13,8 +13,8 @@ module.exports = async () => {
} else if (process.env['SYNC']) { } else if (process.env['SYNC']) {
global.Yoga = require("./dist/sync.asmjs"); global.Yoga = require("./dist/sync.asmjs");
} else if (process.env['WASM']) { } else if (process.env['WASM']) {
global.Yoga = await require("./dist/index.wasm").load(); global.Yoga = await require("./dist/index.wasm").loadYoga();
} else { } else {
global.Yoga = await require("./dist/index.asmjs").load(); global.Yoga = await require("./dist/index.asmjs").loadYoga();
} }
} }

View File

@@ -8,9 +8,9 @@
*/ */
const wrapAsm = require('./wrapAsm'); const wrapAsm = require('./wrapAsm');
const loadYoga = require('./asmjs-async'); const loadAsm = require('./asmjs-async');
module.exports = { module.exports = {
load: () => loadYoga().then(wrapAsm), loadYoga: () => loadAsm().then(wrapAsm),
...require('./YGEnums'), ...require('./YGEnums'),
} }

View File

@@ -12,4 +12,4 @@ import type {Yoga} from './wrapAsm';
export * from './YGEnums'; export * from './YGEnums';
export * from './wrapAsm'; export * from './wrapAsm';
export function load(): Promise<Yoga>; export function loadYoga(): Promise<Yoga>;

View File

@@ -8,9 +8,9 @@
*/ */
const wrapAsm = require('./wrapAsm'); const wrapAsm = require('./wrapAsm');
const loadYoga = require('./wasm-async'); const loadAsm = require('./wasm-async');
module.exports = { module.exports = {
load: () => loadYoga().then(wrapAsm), loadYoga: () => loadAsm().then(wrapAsm),
...require('./YGEnums'), ...require('./YGEnums'),
} }

View File

@@ -8,6 +8,6 @@
*/ */
const wrapAsm = require('./wrapAsm'); const wrapAsm = require('./wrapAsm');
const loadYoga = require('./asmjs-sync'); const loadAsm = require('./asmjs-sync');
module.exports = wrapAsm(loadYoga()); module.exports = wrapAsm(loadAsm());

View File

@@ -8,6 +8,6 @@
*/ */
const wrapAsm = require('./wrapAsm'); const wrapAsm = require('./wrapAsm');
const loadYoga = require('./wasm-sync'); const loadAsm = require('./wasm-sync');
module.exports = wrapAsm(loadYoga()); module.exports = wrapAsm(loadAsm());