Refactor entrypoints

This commit is contained in:
Nick Gerleman
2022-12-23 23:49:35 -08:00
parent c829abf257
commit 0a82167c30
13 changed files with 48 additions and 22 deletions

View File

@@ -17,5 +17,5 @@ runs:
- name: yarn install
shell: bash
run: yarn install --frozen-lockfile --ignore-scripts
run: yarn install --frozen-lockfile
working-directory: javascript

View File

@@ -1,6 +1,6 @@
# 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).
This package provides prebuilt JavaScript bindings for the Yoga layout engine. Both WebAssembly and asm.js variants are packaged, with the optimal loaded based on platform.
## Usage
@@ -23,3 +23,30 @@ import Yoga, { ALIGN_CENTER } from "yoga-layout/sync";
const node = Yoga.Node.create();
node.setAlignContent(ALIGN_CENTER);
```
## Selecting WebAssembly or asm.js
For better performance and smaller packages, WebAssembly is preferred to asm.js where available. `yoga-layout` tries to provide the right default using [export maps](https://webpack.js.org/guides/package-exports/#conditional-syntax) so that platforms which can take advantage of WebAssembly use it by default.
A specific entrypoint may be specified on platforms which do not understand export conditions.
```ts
import { loadYoga } from "yoga-layout/entrypoint/wasm-async";
```
## Contributing
`yoga-layout` may be locally built by running the following set of commands. The build requires that some dependencies be installed first.
```bash
git clone https://github.com/facebook/yoga.git
cd yoga/javascript
yarn install
yarn build
```
### Requirements
1. CMake >= 3.13
1. (Optional) ninja, for faster builds

View File

@@ -9,12 +9,12 @@
module.exports = async () => {
if (process.env['SYNC'] == true && process.env['WASM'] == true) {
global.Yoga = require("./dist/variants/wasm-sync");
global.Yoga = require("./dist/entrypoint/wasm-sync");
} else if (process.env['SYNC'] == true) {
global.Yoga = require("./dist/variants/asmjs-sync");
global.Yoga = require("./dist/entrypoint/asmjs-sync");
} else if (process.env['WASM'] == true) {
global.Yoga = await require("./dist/variants/wasm-async").loadYoga();
global.Yoga = await require("./dist/entrypoint/wasm-async").loadYoga();
} else {
global.Yoga = await require("./dist/variants/asmjs-async").loadYoga();
global.Yoga = await require("./dist/entrypoint/asmjs-async").loadYoga();
}
}

View File

@@ -1,7 +1,7 @@
{
"name": "yoga-layout",
"version": "1.9.3",
"description": "Yoga is a cross-platform layout engine enabling maximum collaboration within your team by implementing an API many designers are familiar with, and opening it up to developers across different platforms.",
"version": "2.0.0-beta.1",
"description": "JavaScript bindings for the Yoga layout engine",
"license": "MIT",
"repository": {
"type": "git",
@@ -11,14 +11,14 @@
"types": "./dist/index.d.ts",
"exports": {
".": {
"browser": "./dist/variants/wasm-async.js",
"node": "./dist/variants/wasm-async.js",
"default": "./dist/variants/asmjs-async.js"
"browser": "./dist/entrypoint/wasm-async.js",
"node": "./dist/entrypoint/wasm-async.js",
"default": "./dist/entrypoint/asmjs-async.js"
},
"./sync": {
"browser": "./dist/variants/asmjs-sync.js",
"node": "./dist/variants/wasm-sync.js",
"default": "./dist/variants/asmjs-sync.js"
"browser": "./dist/entrypoint/asmjs-sync.js",
"node": "./dist/entrypoint/wasm-sync.js",
"default": "./dist/entrypoint/asmjs-sync.js"
}
},
"files": [
@@ -32,7 +32,6 @@
"build:copy-dts": "cd src_js && find . -name '*.d.ts' | cpio -pdm ../dist",
"build:js": "babel src_js --source-maps --out-dir dist && yarn build:copy-dts",
"build:native": "yarn build:configure && cmake --build build",
"postinstall": "yarn build",
"test": "yarn test:asmjs && yarn test:asmjs-sync && yarn test:wasm && yarn test:wasm-sync",
"test:asmjs": "WASM=0 SYNC=0 jest",
"test:asmjs-sync": "WASM=0 SYNC=1 jest",

View File

@@ -8,4 +8,4 @@
*/
const asm = require('../build/asmjs-async');
module.exports = require("../entryAsync")(asm);
module.exports = require("./entryAsync")(asm);

View File

@@ -8,4 +8,4 @@
*/
const asm = require('../build/asmjs-sync');
module.exports = require("../entrySync")(asm);
module.exports = require("./entrySync")(asm);

View File

@@ -8,4 +8,4 @@
*/
const asm = require('../build/wasm-async');
module.exports = require("../entryAsync")(asm);
module.exports = require("./entryAsync")(asm);

View File

@@ -8,4 +8,4 @@
*/
const asm = require('../build/wasm-sync');
module.exports = require("../entrySync")(asm);
module.exports = require("./entrySync")(asm);

View File

@@ -8,4 +8,4 @@
*/
// Fallback for when the export map is not followed
module.exports = require('./variants/asmjs-async');
module.exports = require('./entrypoint/asmjs-async');

View File

@@ -8,4 +8,4 @@
*/
// Fallback for when the export map is not followed
module.exports = require('./variants/asmjs-sync');
module.exports = require('./entrypoint/asmjs-sync');

View File

@@ -27,7 +27,7 @@ for (let type of ['asmjs', 'wasm']) {
vm.runInNewContext(
file,
Object.assign(Object.create(global), {
Yoga: require(type === 'asmjs' ? '../dist/variants/asmjs-sync' : '../dist/variants/wasm-sync'),
Yoga: require(type === 'asmjs' ? '../dist/entrypoint/asmjs-sync' : '../dist/entrypoint/wasm-sync'),
YGBENCHMARK: function(name, fn) {
let testEntry = testResults.get(name);