diff --git a/enums.py b/enums.py index d8e7758e..26b605ca 100644 --- a/enums.py +++ b/enums.py @@ -222,7 +222,7 @@ for name, values in sorted(ENUMS.items()): f.write("}\n") # write out javascript file -with open(root + "/javascript/sources/YGEnums.js", "w") as f: +with open(root + "/javascript/src_js/generated/YGEnums.js", "w") as f: f.write(get_license("js")) f.write("module.exports = {\n") items = sorted(ENUMS.items()) @@ -243,7 +243,7 @@ with open(root + "/javascript/sources/YGEnums.js", "w") as f: f.write("\n") f.write("};\n") -with open(root + "/javascript/sources/YGEnums.d.ts", "w") as f: +with open(root + "/javascript/src_js/generated/YGEnums.d.ts", "w") as f: f.write(get_license("js")) for name, values in sorted(ENUMS.items()): diff --git a/javascript/CMakeLists.txt b/javascript/CMakeLists.txt index 4cc3f69e..00757fad 100644 --- a/javascript/CMakeLists.txt +++ b/javascript/CMakeLists.txt @@ -10,7 +10,7 @@ project(yoga) file(GLOB SOURCES ../yoga/*.cpp ../yoga/**/*.cpp - sources/*.cc) + src_native/*.cc) include_directories(..) @@ -49,7 +49,7 @@ link_libraries(embind) add_library(yogaObjLib OBJECT ${SOURCES}) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/dist) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/dist/build) add_executable(asmjs-sync $) target_link_options(asmjs-sync PUBLIC diff --git a/javascript/jest.setup.js b/javascript/jest.setup.js index 3a64ee47..e38327c2 100644 --- a/javascript/jest.setup.js +++ b/javascript/jest.setup.js @@ -9,12 +9,12 @@ module.exports = async () => { if (process.env['SYNC'] == true && process.env['WASM'] == true) { - global.Yoga = require("./dist/sync.wasm"); + global.Yoga = require("./dist/variants/wasm-sync"); } else if (process.env['SYNC'] == true) { - global.Yoga = require("./dist/sync.asmjs"); + global.Yoga = require("./dist/variants/asmjs-sync"); } else if (process.env['WASM'] == true) { - global.Yoga = await require("./dist/index.wasm").loadYoga(); + global.Yoga = await require("./dist/variants/wasm-async").loadYoga(); } else { - global.Yoga = await require("./dist/index.asmjs").loadYoga(); + global.Yoga = await require("./dist/variants/asmjs-async").loadYoga(); } } diff --git a/javascript/package.json b/javascript/package.json index 12e46de0..28bf1849 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -7,35 +7,37 @@ "type": "git", "url": "git@github.com:facebook/yoga.git" }, + "main": "./dist/index.js", + "types": "./dist/index.d.ts", "exports": { ".": { - "browser": "./dist/index.wasm.js", - "node": "./dist/index.wasm.js", - "react-native": "./dist/index.asmjs.js", - "default": "./dist/index.asmjs.js" + "browser": "./dist/variants/wasm-async.js", + "node": "./dist/variants/wasm-async.js", + "default": "./dist/variants/asmjs-async.js" }, "./sync": { - "browser": "./dist/sync.asmks.js", - "node": "./dist/sync.wasm.js", - "react-native": "./dist/sync.asmjs.js", - "default": "./dist/sync.asmjs.js" + "browser": "./dist/variants/asmjs-sync.js", + "node": "./dist/variants/wasm-sync.js", + "default": "./dist/variants/asmjs-sync.js" } }, "files": [ "dist/**", - "sources/**" + "src_js/**" ], "scripts": { + "benchmark": "node tests/run-bench $(find tests/Benchmarks -name '*.js')", "build": "yarn build:native && yarn build:js", - "build:native": "yarn build:native-project && cmake --build build", - "build:native-project": "which ninja && emcmake cmake -S . -B build -G Ninja || emcmake cmake -S . -B build", - "build:js": "babel sources --source-maps --out-dir dist && cp -r sources/*.d.ts dist", + "build:configure": "which ninja && emcmake cmake -S . -B build -G Ninja || emcmake cmake -S . -B build", + "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", "test:wasm": "WASM=1 SYNC=0 jest", - "test:wasm-sync": "WASM=1 SYNC=1 jest", - "benchmark": "node tests/run-bench $(find tests/Benchmarks -name '*.js')" + "test:wasm-sync": "WASM=1 SYNC=1 jest" }, "devDependencies": { "@babel/cli": "^7.20.7", diff --git a/javascript/sources/index.asmjs.js b/javascript/sources/index.asmjs.js deleted file mode 100644 index 6831b01e..00000000 --- a/javascript/sources/index.asmjs.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @format - */ - -const wrapAsm = require('./wrapAsm'); -const loadAsm = require('./asmjs-async'); - -module.exports = { - loadYoga: () => loadAsm().then(wrapAsm), - ...require('./YGEnums'), -} diff --git a/javascript/sources/index.wasm.js b/javascript/src_js/entryAsync.js similarity index 76% rename from javascript/sources/index.wasm.js rename to javascript/src_js/entryAsync.js index f9696b2d..f9957efa 100644 --- a/javascript/sources/index.wasm.js +++ b/javascript/src_js/entryAsync.js @@ -8,9 +8,8 @@ */ const wrapAsm = require('./wrapAsm'); -const loadAsm = require('./wasm-async'); -module.exports = { +module.exports = (loadAsm) => ({ loadYoga: () => loadAsm().then(wrapAsm), - ...require('./YGEnums'), -} + ...require('./generated/YGEnums'), +}); diff --git a/javascript/sources/sync.wasm.js b/javascript/src_js/entrySync.js similarity index 63% rename from javascript/sources/sync.wasm.js rename to javascript/src_js/entrySync.js index 87280bc3..b1cfe71c 100644 --- a/javascript/sources/sync.wasm.js +++ b/javascript/src_js/entrySync.js @@ -7,7 +7,5 @@ * @format */ -const wrapAsm = require('./wrapAsm'); -const loadAsm = require('./wasm-sync'); - -module.exports = wrapAsm(loadAsm()); + const wrapAsm = require('./wrapAsm'); + module.exports = (asm) => wrapAsm(asm()); diff --git a/javascript/sources/YGEnums.d.ts b/javascript/src_js/generated/YGEnums.d.ts similarity index 100% rename from javascript/sources/YGEnums.d.ts rename to javascript/src_js/generated/YGEnums.d.ts diff --git a/javascript/sources/YGEnums.js b/javascript/src_js/generated/YGEnums.js similarity index 100% rename from javascript/sources/YGEnums.js rename to javascript/src_js/generated/YGEnums.js diff --git a/javascript/sources/index.d.ts b/javascript/src_js/index.d.ts similarity index 89% rename from javascript/sources/index.d.ts rename to javascript/src_js/index.d.ts index 64bdf899..fb2830e4 100644 --- a/javascript/sources/index.d.ts +++ b/javascript/src_js/index.d.ts @@ -9,7 +9,7 @@ import type {Yoga} from './wrapAsm'; -export * from './YGEnums'; +export * from './generated/YGEnums'; export * from './wrapAsm'; export function loadYoga(): Promise; diff --git a/javascript/sources/sync.asmjs.js b/javascript/src_js/index.js similarity index 63% rename from javascript/sources/sync.asmjs.js rename to javascript/src_js/index.js index bfb929fc..2864db0c 100644 --- a/javascript/sources/sync.asmjs.js +++ b/javascript/src_js/index.js @@ -7,7 +7,5 @@ * @format */ -const wrapAsm = require('./wrapAsm'); -const loadAsm = require('./asmjs-sync'); - -module.exports = wrapAsm(loadAsm()); +// Fallback for when the export map is not followed +module.exports = require('./variants/asmjs-async'); diff --git a/javascript/src_js/sync.d.ts b/javascript/src_js/sync.d.ts new file mode 100644 index 00000000..27bd8ecc --- /dev/null +++ b/javascript/src_js/sync.d.ts @@ -0,0 +1,16 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +import type {Yoga} from './wrapAsm'; + +export * from './generated/YGEnums'; +export * from './wrapAsm'; + +declare const yoga: Yoga; +export default yoga; diff --git a/javascript/sources/sync.d.ts b/javascript/src_js/sync.js similarity index 58% rename from javascript/sources/sync.d.ts rename to javascript/src_js/sync.js index 350f9492..5a79514e 100644 --- a/javascript/sources/sync.d.ts +++ b/javascript/src_js/sync.js @@ -7,10 +7,5 @@ * @format */ - import type {Yoga} from './wrapAsm'; - - export * from './YGEnums'; - export * from './wrapAsm'; - - declare const yoga: Yoga; - export default yoga; +// Fallback for when the export map is not followed +module.exports = require('./variants/asmjs-sync'); diff --git a/javascript/src_js/variants/asmjs-async.js b/javascript/src_js/variants/asmjs-async.js new file mode 100644 index 00000000..02d4097c --- /dev/null +++ b/javascript/src_js/variants/asmjs-async.js @@ -0,0 +1,11 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +const asm = require('../build/asmjs-async'); +module.exports = require("../entryAsync")(asm); diff --git a/javascript/src_js/variants/asmjs-sync.js b/javascript/src_js/variants/asmjs-sync.js new file mode 100644 index 00000000..6bcf004f --- /dev/null +++ b/javascript/src_js/variants/asmjs-sync.js @@ -0,0 +1,11 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +const asm = require('../build/asmjs-sync'); +module.exports = require("../entrySync")(asm); diff --git a/javascript/src_js/variants/wasm-async.js b/javascript/src_js/variants/wasm-async.js new file mode 100644 index 00000000..02906e31 --- /dev/null +++ b/javascript/src_js/variants/wasm-async.js @@ -0,0 +1,11 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +const asm = require('../build/wasm-async'); +module.exports = require("../entryAsync")(asm); diff --git a/javascript/src_js/variants/wasm-sync.js b/javascript/src_js/variants/wasm-sync.js new file mode 100644 index 00000000..f74af006 --- /dev/null +++ b/javascript/src_js/variants/wasm-sync.js @@ -0,0 +1,11 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +const asm = require('../build/wasm-sync'); +module.exports = require("../entrySync")(asm); diff --git a/javascript/sources/wrapAsm.d.ts b/javascript/src_js/wrapAsm.d.ts similarity index 97% rename from javascript/sources/wrapAsm.d.ts rename to javascript/src_js/wrapAsm.d.ts index 0e67d5b4..805ac812 100644 --- a/javascript/sources/wrapAsm.d.ts +++ b/javascript/src_js/wrapAsm.d.ts @@ -7,7 +7,7 @@ * @format */ - import type { +import type { Edge, Wrap, Align, @@ -19,9 +19,9 @@ Justify, Display, ExperimentalFeature, -} from './YGEnums'; +} from './generated/YGEnums'; -import type * as YGEnums from './YGEnums'; +import type * as YGEnums from './generated/YGEnums'; type Layout = { left: number; diff --git a/javascript/sources/wrapAsm.js b/javascript/src_js/wrapAsm.js similarity index 98% rename from javascript/sources/wrapAsm.js rename to javascript/src_js/wrapAsm.js index 263578b9..f6e52e5b 100644 --- a/javascript/sources/wrapAsm.js +++ b/javascript/src_js/wrapAsm.js @@ -7,7 +7,7 @@ * @format */ -const CONSTANTS = require('./YGEnums'); +const CONSTANTS = require('./generated/YGEnums'); module.exports = (lib) => { function patch(prototype, name, fn) { diff --git a/javascript/sources/Config.cc b/javascript/src_native/Config.cc similarity index 100% rename from javascript/sources/Config.cc rename to javascript/src_native/Config.cc diff --git a/javascript/sources/Config.hh b/javascript/src_native/Config.hh similarity index 100% rename from javascript/sources/Config.hh rename to javascript/src_native/Config.hh diff --git a/javascript/sources/Layout.hh b/javascript/src_native/Layout.hh similarity index 100% rename from javascript/sources/Layout.hh rename to javascript/src_native/Layout.hh diff --git a/javascript/sources/Node.cc b/javascript/src_native/Node.cc similarity index 100% rename from javascript/sources/Node.cc rename to javascript/src_native/Node.cc diff --git a/javascript/sources/Node.hh b/javascript/src_native/Node.hh similarity index 100% rename from javascript/sources/Node.hh rename to javascript/src_native/Node.hh diff --git a/javascript/sources/Size.hh b/javascript/src_native/Size.hh similarity index 100% rename from javascript/sources/Size.hh rename to javascript/src_native/Size.hh diff --git a/javascript/sources/Value.hh b/javascript/src_native/Value.hh similarity index 100% rename from javascript/sources/Value.hh rename to javascript/src_native/Value.hh diff --git a/javascript/sources/embind.cc b/javascript/src_native/embind.cc similarity index 100% rename from javascript/sources/embind.cc rename to javascript/src_native/embind.cc diff --git a/javascript/tests/run-bench.js b/javascript/tests/run-bench.js index 0475b5e0..02ca7dae 100644 --- a/javascript/tests/run-bench.js +++ b/javascript/tests/run-bench.js @@ -27,7 +27,7 @@ for (let type of ['asmjs', 'wasm']) { vm.runInNewContext( file, Object.assign(Object.create(global), { - Yoga: require(type === 'asmjs' ? '../dist/sync.asmjs' : '../dist/sync.wasm'), + Yoga: require(type === 'asmjs' ? '../dist/variants/asmjs-sync' : '../dist/variants/wasm-sync'), YGBENCHMARK: function(name, fn) { let testEntry = testResults.get(name);