From aeffaf93a723396834880a35b85b26f5bcf5924d Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 9 May 2023 21:36:00 -0700 Subject: [PATCH 1/2] Namespaced and TypeScript Enums Summary: Enums are currently exposed to the JS package as constants (e.g. `import {ERRATA_NONE} from 'yoga-layout'`). This exports enums in the form of `import {Errata} from 'yoga-layout'` then `Errata.None`. It would be more ergonomic for these to be string union based enums instead, but right now it is a pretty thin wrapper around the native API, we need ordinal values to do things with bit masks, and folks have wanted to serialize them before. Differential Revision: https://internalfb.com/D45570417 fbshipit-source-id: c9c40f4b4dd6cb0c46bc05e558dded39dd299cf0 --- enums.py | 126 ++++------ javascript/README.md | 10 +- javascript/src/generated/YGEnums.d.ts | 347 -------------------------- javascript/src/generated/YGEnums.js | 97 ------- javascript/src/generated/YGEnums.ts | 208 +++++++++++++++ javascript/src/wrapAsm.d.ts | 2 +- javascript/src/wrapAsm.js | 23 +- 7 files changed, 265 insertions(+), 548 deletions(-) mode change 100644 => 100755 enums.py delete mode 100644 javascript/src/generated/YGEnums.d.ts delete mode 100644 javascript/src/generated/YGEnums.js create mode 100644 javascript/src/generated/YGEnums.ts diff --git a/enums.py b/enums.py old mode 100644 new mode 100755 index ddc9c6b2..883daf25 --- a/enums.py +++ b/enums.py @@ -1,13 +1,11 @@ +#!/usr/bin/env python3 # 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. -from __future__ import absolute_import, division, print_function, unicode_literals - import os - ENUMS = { "Direction": ["Inherit", "LTR", "RTL"], "Unit": ["Undefined", "Point", "Percent", "Auto"], @@ -78,50 +76,44 @@ ENUMS = { ], } -# Generated Java enums used to emit @DoNotStrip, but D17519844 removed them -# manually from all but YogaLogLevel. TODO: Is it safe to remove from it as -# well? DO_NOT_STRIP = ["LogLevel"] BITSET_ENUMS = ["PrintOptions", "Errata"] def get_license(ext): - prologue = "/**" if ext == "js" else "/*" - return """{} + return f"""{"/**" if ext == "js" else "/*"} * 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. */ -// @{} by enums.py +// @{"generated"} by enums.py -""".format( - prologue, "generated" - ) +""" + + +def _format_name(symbol, delimiter=None, transform=None): + symbol = str(symbol) + out = "" + for i in range(0, len(symbol)): + c = symbol[i] + if str.istitle(c) and i != 0 and not str.istitle(symbol[i - 1]): + out += delimiter or "" + if transform is None: + out += c + else: + out += getattr(c, transform)() + return out def to_java_upper(symbol): - symbol = str(symbol) - out = "" - for i in range(0, len(symbol)): - c = symbol[i] - if str.istitle(c) and i is not 0 and not str.istitle(symbol[i - 1]): - out += "_" - out += c.upper() - return out + return _format_name(symbol, "_", "upper") -def to_log_lower(symbol): - symbol = str(symbol) - out = "" - for i in range(0, len(symbol)): - c = symbol[i] - if str.istitle(c) and i is not 0 and not str.istitle(symbol[i - 1]): - out += "-" - out += c.lower() - return out +def to_hyphenated_lower(symbol): + return _format_name(symbol, "-", "lower") root = os.path.dirname(os.path.abspath(__file__)) @@ -167,10 +159,10 @@ with open(root + "/yoga/YGEnums.cpp", "w") as f: for value in values: if isinstance(value, tuple): f.write(" case YG%s%s:\n" % (name, value[0])) - f.write(' return "%s";\n' % to_log_lower(value[0])) + f.write(' return "%s";\n' % to_hyphenated_lower(value[0])) else: f.write(" case YG%s%s:\n" % (name, value)) - f.write(' return "%s";\n' % to_log_lower(value)) + f.write(' return "%s";\n' % to_hyphenated_lower(value)) f.write(" }\n") f.write(' return "unknown";\n') f.write("}\n") @@ -246,61 +238,27 @@ for name, values in sorted(ENUMS.items()): f.write(" }\n") f.write("}\n") -# write out javascript file -with open(root + "/javascript/src/generated/YGEnums.js", "w") as f: +# write out TypeScript file +with open(root + "/javascript/src/generated/YGEnums.ts", "w") as f: f.write(get_license("js")) - items = sorted(ENUMS.items()) - for name, values in items: - base = 0 - for value in values: - value_arg = value[0] if isinstance(value, tuple) else value - ordinal_arg = value[1] if isinstance(value, tuple) else base + enums = sorted(ENUMS.items()) + for enum_name, ordinals in enums: + f.write(f"export enum {enum_name} {{\n") + for ordinal_index, ordinal in enumerate(ordinals): + ordinal_name = ordinal[0] if isinstance(ordinal, tuple) else ordinal + ordinal_value = ordinal[1] if isinstance(ordinal, tuple) else ordinal_index + f.write(f" {ordinal_name} = {ordinal_value},\n") + f.write("}\n\n") + + f.write("const constants = {\n") + for enum_name, ordinals in enums: + for ordinal in ordinals: + ordinal_name = ordinal[0] if isinstance(ordinal, tuple) else ordinal + ordinal_value = ordinal[1] if isinstance(ordinal, tuple) else ordinal_index f.write( - "exports.%s_%s = %d;\n" - % (to_java_upper(name), to_java_upper(value_arg), ordinal_arg) - ) - base = ordinal_arg + 1 - - if name != items[-1][0]: - f.write("\n") - -with open(root + "/javascript/src/generated/YGEnums.d.ts", "w") as f: - f.write(get_license("js")) - - for name, values in sorted(ENUMS.items()): - base = 0 - for value in values: - value_arg = value[0] if isinstance(value, tuple) else value - ordinal_arg = value[1] if isinstance(value, tuple) else base - - f.write( - ( - "type {name}_{value} = {ordinal} & ['{name}']\n" - + "export declare const {name}_{value}: {name}_{value};\n\n" - ).format( - name=to_java_upper(name), - value=to_java_upper(value_arg), - ordinal=ordinal_arg, - ) + f" {to_java_upper(enum_name)}_{to_java_upper(ordinal_name)}: {enum_name}.{ordinal_name},\n" ) - base = ordinal_arg + 1 - - f.write("\n") - - for name, values in sorted(ENUMS.items()): - f.write("export type {} =\n".format(name)) - for value in values: - unpackedValue = value[0] if isinstance(value, tuple) else value - f.write( - " | typeof {}_{}".format( - to_java_upper(name), to_java_upper(unpackedValue) - ) - ) - if values[-1] == value: - f.write(";\n") - else: - f.write("\n") - - f.write("\n") + f.write("}\n") + f.write("export default constants") diff --git a/javascript/README.md b/javascript/README.md index 6f7b4d1a..ceeb50ac 100644 --- a/javascript/README.md +++ b/javascript/README.md @@ -7,21 +7,21 @@ This package provides prebuilt JavaScript bindings for the Yoga layout engine. B The default entrypoint provides an asynchronous loader function to return a Yoga instance. ```ts -import { loadYoga, ALIGN_CENTER } from "yoga-layout"; +import {loadYoga, Align} from 'yoga-layout'; const Yoga = await loadYoga(); const node = Yoga.Node.create(); -node.setAlignContent(ALIGN_CENTER); +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"; +import Yoga, {Align} from 'yoga-layout/sync'; const node = Yoga.Node.create(); -node.setAlignContent(ALIGN_CENTER); +node.setAlignContent(Align.Center); ``` Objects created by `Yoga.<>.create()` are not automatically garbage collected and should be freed once they are no longer in use. @@ -44,7 +44,7 @@ For better performance and smaller packages, WebAssembly is preferred to asm.js A specific entrypoint may be specified on platforms which do not understand export conditions. ```ts -import { loadYoga } from "yoga-layout/dist/entrypoint/wasm-async"; +import {loadYoga} from 'yoga-layout/dist/entrypoint/wasm-async'; ``` diff --git a/javascript/src/generated/YGEnums.d.ts b/javascript/src/generated/YGEnums.d.ts deleted file mode 100644 index 0881bd2f..00000000 --- a/javascript/src/generated/YGEnums.d.ts +++ /dev/null @@ -1,347 +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. - */ - -// @generated by enums.py - -type ALIGN_AUTO = 0 & ['ALIGN'] -export declare const ALIGN_AUTO: ALIGN_AUTO; - -type ALIGN_FLEX_START = 1 & ['ALIGN'] -export declare const ALIGN_FLEX_START: ALIGN_FLEX_START; - -type ALIGN_CENTER = 2 & ['ALIGN'] -export declare const ALIGN_CENTER: ALIGN_CENTER; - -type ALIGN_FLEX_END = 3 & ['ALIGN'] -export declare const ALIGN_FLEX_END: ALIGN_FLEX_END; - -type ALIGN_STRETCH = 4 & ['ALIGN'] -export declare const ALIGN_STRETCH: ALIGN_STRETCH; - -type ALIGN_BASELINE = 5 & ['ALIGN'] -export declare const ALIGN_BASELINE: ALIGN_BASELINE; - -type ALIGN_SPACE_BETWEEN = 6 & ['ALIGN'] -export declare const ALIGN_SPACE_BETWEEN: ALIGN_SPACE_BETWEEN; - -type ALIGN_SPACE_AROUND = 7 & ['ALIGN'] -export declare const ALIGN_SPACE_AROUND: ALIGN_SPACE_AROUND; - - -type DIMENSION_WIDTH = 0 & ['DIMENSION'] -export declare const DIMENSION_WIDTH: DIMENSION_WIDTH; - -type DIMENSION_HEIGHT = 1 & ['DIMENSION'] -export declare const DIMENSION_HEIGHT: DIMENSION_HEIGHT; - - -type DIRECTION_INHERIT = 0 & ['DIRECTION'] -export declare const DIRECTION_INHERIT: DIRECTION_INHERIT; - -type DIRECTION_LTR = 1 & ['DIRECTION'] -export declare const DIRECTION_LTR: DIRECTION_LTR; - -type DIRECTION_RTL = 2 & ['DIRECTION'] -export declare const DIRECTION_RTL: DIRECTION_RTL; - - -type DISPLAY_FLEX = 0 & ['DISPLAY'] -export declare const DISPLAY_FLEX: DISPLAY_FLEX; - -type DISPLAY_NONE = 1 & ['DISPLAY'] -export declare const DISPLAY_NONE: DISPLAY_NONE; - - -type EDGE_LEFT = 0 & ['EDGE'] -export declare const EDGE_LEFT: EDGE_LEFT; - -type EDGE_TOP = 1 & ['EDGE'] -export declare const EDGE_TOP: EDGE_TOP; - -type EDGE_RIGHT = 2 & ['EDGE'] -export declare const EDGE_RIGHT: EDGE_RIGHT; - -type EDGE_BOTTOM = 3 & ['EDGE'] -export declare const EDGE_BOTTOM: EDGE_BOTTOM; - -type EDGE_START = 4 & ['EDGE'] -export declare const EDGE_START: EDGE_START; - -type EDGE_END = 5 & ['EDGE'] -export declare const EDGE_END: EDGE_END; - -type EDGE_HORIZONTAL = 6 & ['EDGE'] -export declare const EDGE_HORIZONTAL: EDGE_HORIZONTAL; - -type EDGE_VERTICAL = 7 & ['EDGE'] -export declare const EDGE_VERTICAL: EDGE_VERTICAL; - -type EDGE_ALL = 8 & ['EDGE'] -export declare const EDGE_ALL: EDGE_ALL; - - -type ERRATA_NONE = 0 & ['ERRATA'] -export declare const ERRATA_NONE: ERRATA_NONE; - -type ERRATA_STRETCH_FLEX_BASIS = 1 & ['ERRATA'] -export declare const ERRATA_STRETCH_FLEX_BASIS: ERRATA_STRETCH_FLEX_BASIS; - -type ERRATA_ALL = 2147483647 & ['ERRATA'] -export declare const ERRATA_ALL: ERRATA_ALL; - -type ERRATA_CLASSIC = 2147483646 & ['ERRATA'] -export declare const ERRATA_CLASSIC: ERRATA_CLASSIC; - - -type EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS = 0 & ['EXPERIMENTAL_FEATURE'] -export declare const EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS: EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS; - -type EXPERIMENTAL_FEATURE_ABSOLUTE_PERCENTAGE_AGAINST_PADDING_EDGE = 1 & ['EXPERIMENTAL_FEATURE'] -export declare const EXPERIMENTAL_FEATURE_ABSOLUTE_PERCENTAGE_AGAINST_PADDING_EDGE: EXPERIMENTAL_FEATURE_ABSOLUTE_PERCENTAGE_AGAINST_PADDING_EDGE; - -type EXPERIMENTAL_FEATURE_FIX_ABSOLUTE_TRAILING_COLUMN_MARGIN = 2 & ['EXPERIMENTAL_FEATURE'] -export declare const EXPERIMENTAL_FEATURE_FIX_ABSOLUTE_TRAILING_COLUMN_MARGIN: EXPERIMENTAL_FEATURE_FIX_ABSOLUTE_TRAILING_COLUMN_MARGIN; - - -type FLEX_DIRECTION_COLUMN = 0 & ['FLEX_DIRECTION'] -export declare const FLEX_DIRECTION_COLUMN: FLEX_DIRECTION_COLUMN; - -type FLEX_DIRECTION_COLUMN_REVERSE = 1 & ['FLEX_DIRECTION'] -export declare const FLEX_DIRECTION_COLUMN_REVERSE: FLEX_DIRECTION_COLUMN_REVERSE; - -type FLEX_DIRECTION_ROW = 2 & ['FLEX_DIRECTION'] -export declare const FLEX_DIRECTION_ROW: FLEX_DIRECTION_ROW; - -type FLEX_DIRECTION_ROW_REVERSE = 3 & ['FLEX_DIRECTION'] -export declare const FLEX_DIRECTION_ROW_REVERSE: FLEX_DIRECTION_ROW_REVERSE; - - -type GUTTER_COLUMN = 0 & ['GUTTER'] -export declare const GUTTER_COLUMN: GUTTER_COLUMN; - -type GUTTER_ROW = 1 & ['GUTTER'] -export declare const GUTTER_ROW: GUTTER_ROW; - -type GUTTER_ALL = 2 & ['GUTTER'] -export declare const GUTTER_ALL: GUTTER_ALL; - - -type JUSTIFY_FLEX_START = 0 & ['JUSTIFY'] -export declare const JUSTIFY_FLEX_START: JUSTIFY_FLEX_START; - -type JUSTIFY_CENTER = 1 & ['JUSTIFY'] -export declare const JUSTIFY_CENTER: JUSTIFY_CENTER; - -type JUSTIFY_FLEX_END = 2 & ['JUSTIFY'] -export declare const JUSTIFY_FLEX_END: JUSTIFY_FLEX_END; - -type JUSTIFY_SPACE_BETWEEN = 3 & ['JUSTIFY'] -export declare const JUSTIFY_SPACE_BETWEEN: JUSTIFY_SPACE_BETWEEN; - -type JUSTIFY_SPACE_AROUND = 4 & ['JUSTIFY'] -export declare const JUSTIFY_SPACE_AROUND: JUSTIFY_SPACE_AROUND; - -type JUSTIFY_SPACE_EVENLY = 5 & ['JUSTIFY'] -export declare const JUSTIFY_SPACE_EVENLY: JUSTIFY_SPACE_EVENLY; - - -type LOG_LEVEL_ERROR = 0 & ['LOG_LEVEL'] -export declare const LOG_LEVEL_ERROR: LOG_LEVEL_ERROR; - -type LOG_LEVEL_WARN = 1 & ['LOG_LEVEL'] -export declare const LOG_LEVEL_WARN: LOG_LEVEL_WARN; - -type LOG_LEVEL_INFO = 2 & ['LOG_LEVEL'] -export declare const LOG_LEVEL_INFO: LOG_LEVEL_INFO; - -type LOG_LEVEL_DEBUG = 3 & ['LOG_LEVEL'] -export declare const LOG_LEVEL_DEBUG: LOG_LEVEL_DEBUG; - -type LOG_LEVEL_VERBOSE = 4 & ['LOG_LEVEL'] -export declare const LOG_LEVEL_VERBOSE: LOG_LEVEL_VERBOSE; - -type LOG_LEVEL_FATAL = 5 & ['LOG_LEVEL'] -export declare const LOG_LEVEL_FATAL: LOG_LEVEL_FATAL; - - -type MEASURE_MODE_UNDEFINED = 0 & ['MEASURE_MODE'] -export declare const MEASURE_MODE_UNDEFINED: MEASURE_MODE_UNDEFINED; - -type MEASURE_MODE_EXACTLY = 1 & ['MEASURE_MODE'] -export declare const MEASURE_MODE_EXACTLY: MEASURE_MODE_EXACTLY; - -type MEASURE_MODE_AT_MOST = 2 & ['MEASURE_MODE'] -export declare const MEASURE_MODE_AT_MOST: MEASURE_MODE_AT_MOST; - - -type NODE_TYPE_DEFAULT = 0 & ['NODE_TYPE'] -export declare const NODE_TYPE_DEFAULT: NODE_TYPE_DEFAULT; - -type NODE_TYPE_TEXT = 1 & ['NODE_TYPE'] -export declare const NODE_TYPE_TEXT: NODE_TYPE_TEXT; - - -type OVERFLOW_VISIBLE = 0 & ['OVERFLOW'] -export declare const OVERFLOW_VISIBLE: OVERFLOW_VISIBLE; - -type OVERFLOW_HIDDEN = 1 & ['OVERFLOW'] -export declare const OVERFLOW_HIDDEN: OVERFLOW_HIDDEN; - -type OVERFLOW_SCROLL = 2 & ['OVERFLOW'] -export declare const OVERFLOW_SCROLL: OVERFLOW_SCROLL; - - -type POSITION_TYPE_STATIC = 0 & ['POSITION_TYPE'] -export declare const POSITION_TYPE_STATIC: POSITION_TYPE_STATIC; - -type POSITION_TYPE_RELATIVE = 1 & ['POSITION_TYPE'] -export declare const POSITION_TYPE_RELATIVE: POSITION_TYPE_RELATIVE; - -type POSITION_TYPE_ABSOLUTE = 2 & ['POSITION_TYPE'] -export declare const POSITION_TYPE_ABSOLUTE: POSITION_TYPE_ABSOLUTE; - - -type PRINT_OPTIONS_LAYOUT = 1 & ['PRINT_OPTIONS'] -export declare const PRINT_OPTIONS_LAYOUT: PRINT_OPTIONS_LAYOUT; - -type PRINT_OPTIONS_STYLE = 2 & ['PRINT_OPTIONS'] -export declare const PRINT_OPTIONS_STYLE: PRINT_OPTIONS_STYLE; - -type PRINT_OPTIONS_CHILDREN = 4 & ['PRINT_OPTIONS'] -export declare const PRINT_OPTIONS_CHILDREN: PRINT_OPTIONS_CHILDREN; - - -type UNIT_UNDEFINED = 0 & ['UNIT'] -export declare const UNIT_UNDEFINED: UNIT_UNDEFINED; - -type UNIT_POINT = 1 & ['UNIT'] -export declare const UNIT_POINT: UNIT_POINT; - -type UNIT_PERCENT = 2 & ['UNIT'] -export declare const UNIT_PERCENT: UNIT_PERCENT; - -type UNIT_AUTO = 3 & ['UNIT'] -export declare const UNIT_AUTO: UNIT_AUTO; - - -type WRAP_NO_WRAP = 0 & ['WRAP'] -export declare const WRAP_NO_WRAP: WRAP_NO_WRAP; - -type WRAP_WRAP = 1 & ['WRAP'] -export declare const WRAP_WRAP: WRAP_WRAP; - -type WRAP_WRAP_REVERSE = 2 & ['WRAP'] -export declare const WRAP_WRAP_REVERSE: WRAP_WRAP_REVERSE; - - -export type Align = - | typeof ALIGN_AUTO - | typeof ALIGN_FLEX_START - | typeof ALIGN_CENTER - | typeof ALIGN_FLEX_END - | typeof ALIGN_STRETCH - | typeof ALIGN_BASELINE - | typeof ALIGN_SPACE_BETWEEN - | typeof ALIGN_SPACE_AROUND; - -export type Dimension = - | typeof DIMENSION_WIDTH - | typeof DIMENSION_HEIGHT; - -export type Direction = - | typeof DIRECTION_INHERIT - | typeof DIRECTION_LTR - | typeof DIRECTION_RTL; - -export type Display = - | typeof DISPLAY_FLEX - | typeof DISPLAY_NONE; - -export type Edge = - | typeof EDGE_LEFT - | typeof EDGE_TOP - | typeof EDGE_RIGHT - | typeof EDGE_BOTTOM - | typeof EDGE_START - | typeof EDGE_END - | typeof EDGE_HORIZONTAL - | typeof EDGE_VERTICAL - | typeof EDGE_ALL; - -export type Errata = - | typeof ERRATA_NONE - | typeof ERRATA_STRETCH_FLEX_BASIS - | typeof ERRATA_ALL - | typeof ERRATA_CLASSIC; - -export type ExperimentalFeature = - | typeof EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS - | typeof EXPERIMENTAL_FEATURE_ABSOLUTE_PERCENTAGE_AGAINST_PADDING_EDGE - | typeof EXPERIMENTAL_FEATURE_FIX_ABSOLUTE_TRAILING_COLUMN_MARGIN; - -export type FlexDirection = - | typeof FLEX_DIRECTION_COLUMN - | typeof FLEX_DIRECTION_COLUMN_REVERSE - | typeof FLEX_DIRECTION_ROW - | typeof FLEX_DIRECTION_ROW_REVERSE; - -export type Gutter = - | typeof GUTTER_COLUMN - | typeof GUTTER_ROW - | typeof GUTTER_ALL; - -export type Justify = - | typeof JUSTIFY_FLEX_START - | typeof JUSTIFY_CENTER - | typeof JUSTIFY_FLEX_END - | typeof JUSTIFY_SPACE_BETWEEN - | typeof JUSTIFY_SPACE_AROUND - | typeof JUSTIFY_SPACE_EVENLY; - -export type LogLevel = - | typeof LOG_LEVEL_ERROR - | typeof LOG_LEVEL_WARN - | typeof LOG_LEVEL_INFO - | typeof LOG_LEVEL_DEBUG - | typeof LOG_LEVEL_VERBOSE - | typeof LOG_LEVEL_FATAL; - -export type MeasureMode = - | typeof MEASURE_MODE_UNDEFINED - | typeof MEASURE_MODE_EXACTLY - | typeof MEASURE_MODE_AT_MOST; - -export type NodeType = - | typeof NODE_TYPE_DEFAULT - | typeof NODE_TYPE_TEXT; - -export type Overflow = - | typeof OVERFLOW_VISIBLE - | typeof OVERFLOW_HIDDEN - | typeof OVERFLOW_SCROLL; - -export type PositionType = - | typeof POSITION_TYPE_STATIC - | typeof POSITION_TYPE_RELATIVE - | typeof POSITION_TYPE_ABSOLUTE; - -export type PrintOptions = - | typeof PRINT_OPTIONS_LAYOUT - | typeof PRINT_OPTIONS_STYLE - | typeof PRINT_OPTIONS_CHILDREN; - -export type Unit = - | typeof UNIT_UNDEFINED - | typeof UNIT_POINT - | typeof UNIT_PERCENT - | typeof UNIT_AUTO; - -export type Wrap = - | typeof WRAP_NO_WRAP - | typeof WRAP_WRAP - | typeof WRAP_WRAP_REVERSE; - diff --git a/javascript/src/generated/YGEnums.js b/javascript/src/generated/YGEnums.js deleted file mode 100644 index 69c86f4a..00000000 --- a/javascript/src/generated/YGEnums.js +++ /dev/null @@ -1,97 +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. - */ - -// @generated by enums.py - -exports.ALIGN_AUTO = 0; -exports.ALIGN_FLEX_START = 1; -exports.ALIGN_CENTER = 2; -exports.ALIGN_FLEX_END = 3; -exports.ALIGN_STRETCH = 4; -exports.ALIGN_BASELINE = 5; -exports.ALIGN_SPACE_BETWEEN = 6; -exports.ALIGN_SPACE_AROUND = 7; - -exports.DIMENSION_WIDTH = 0; -exports.DIMENSION_HEIGHT = 1; - -exports.DIRECTION_INHERIT = 0; -exports.DIRECTION_LTR = 1; -exports.DIRECTION_RTL = 2; - -exports.DISPLAY_FLEX = 0; -exports.DISPLAY_NONE = 1; - -exports.EDGE_LEFT = 0; -exports.EDGE_TOP = 1; -exports.EDGE_RIGHT = 2; -exports.EDGE_BOTTOM = 3; -exports.EDGE_START = 4; -exports.EDGE_END = 5; -exports.EDGE_HORIZONTAL = 6; -exports.EDGE_VERTICAL = 7; -exports.EDGE_ALL = 8; - -exports.ERRATA_NONE = 0; -exports.ERRATA_STRETCH_FLEX_BASIS = 1; -exports.ERRATA_ALL = 2147483647; -exports.ERRATA_CLASSIC = 2147483646; - -exports.EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS = 0; -exports.EXPERIMENTAL_FEATURE_ABSOLUTE_PERCENTAGE_AGAINST_PADDING_EDGE = 1; -exports.EXPERIMENTAL_FEATURE_FIX_ABSOLUTE_TRAILING_COLUMN_MARGIN = 2; - -exports.FLEX_DIRECTION_COLUMN = 0; -exports.FLEX_DIRECTION_COLUMN_REVERSE = 1; -exports.FLEX_DIRECTION_ROW = 2; -exports.FLEX_DIRECTION_ROW_REVERSE = 3; - -exports.GUTTER_COLUMN = 0; -exports.GUTTER_ROW = 1; -exports.GUTTER_ALL = 2; - -exports.JUSTIFY_FLEX_START = 0; -exports.JUSTIFY_CENTER = 1; -exports.JUSTIFY_FLEX_END = 2; -exports.JUSTIFY_SPACE_BETWEEN = 3; -exports.JUSTIFY_SPACE_AROUND = 4; -exports.JUSTIFY_SPACE_EVENLY = 5; - -exports.LOG_LEVEL_ERROR = 0; -exports.LOG_LEVEL_WARN = 1; -exports.LOG_LEVEL_INFO = 2; -exports.LOG_LEVEL_DEBUG = 3; -exports.LOG_LEVEL_VERBOSE = 4; -exports.LOG_LEVEL_FATAL = 5; - -exports.MEASURE_MODE_UNDEFINED = 0; -exports.MEASURE_MODE_EXACTLY = 1; -exports.MEASURE_MODE_AT_MOST = 2; - -exports.NODE_TYPE_DEFAULT = 0; -exports.NODE_TYPE_TEXT = 1; - -exports.OVERFLOW_VISIBLE = 0; -exports.OVERFLOW_HIDDEN = 1; -exports.OVERFLOW_SCROLL = 2; - -exports.POSITION_TYPE_STATIC = 0; -exports.POSITION_TYPE_RELATIVE = 1; -exports.POSITION_TYPE_ABSOLUTE = 2; - -exports.PRINT_OPTIONS_LAYOUT = 1; -exports.PRINT_OPTIONS_STYLE = 2; -exports.PRINT_OPTIONS_CHILDREN = 4; - -exports.UNIT_UNDEFINED = 0; -exports.UNIT_POINT = 1; -exports.UNIT_PERCENT = 2; -exports.UNIT_AUTO = 3; - -exports.WRAP_NO_WRAP = 0; -exports.WRAP_WRAP = 1; -exports.WRAP_WRAP_REVERSE = 2; diff --git a/javascript/src/generated/YGEnums.ts b/javascript/src/generated/YGEnums.ts new file mode 100644 index 00000000..3ddaac9b --- /dev/null +++ b/javascript/src/generated/YGEnums.ts @@ -0,0 +1,208 @@ +/** + * 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. + */ + +// @generated by enums.py + +export enum Align { + Auto = 0, + FlexStart = 1, + Center = 2, + FlexEnd = 3, + Stretch = 4, + Baseline = 5, + SpaceBetween = 6, + SpaceAround = 7, +} + +export enum Dimension { + Width = 0, + Height = 1, +} + +export enum Direction { + Inherit = 0, + LTR = 1, + RTL = 2, +} + +export enum Display { + Flex = 0, + None = 1, +} + +export enum Edge { + Left = 0, + Top = 1, + Right = 2, + Bottom = 3, + Start = 4, + End = 5, + Horizontal = 6, + Vertical = 7, + All = 8, +} + +export enum Errata { + None = 0, + StretchFlexBasis = 1, + All = 2147483647, + Classic = 2147483646, +} + +export enum ExperimentalFeature { + WebFlexBasis = 0, + AbsolutePercentageAgainstPaddingEdge = 1, + FixAbsoluteTrailingColumnMargin = 2, +} + +export enum FlexDirection { + Column = 0, + ColumnReverse = 1, + Row = 2, + RowReverse = 3, +} + +export enum Gutter { + Column = 0, + Row = 1, + All = 2, +} + +export enum Justify { + FlexStart = 0, + Center = 1, + FlexEnd = 2, + SpaceBetween = 3, + SpaceAround = 4, + SpaceEvenly = 5, +} + +export enum LogLevel { + Error = 0, + Warn = 1, + Info = 2, + Debug = 3, + Verbose = 4, + Fatal = 5, +} + +export enum MeasureMode { + Undefined = 0, + Exactly = 1, + AtMost = 2, +} + +export enum NodeType { + Default = 0, + Text = 1, +} + +export enum Overflow { + Visible = 0, + Hidden = 1, + Scroll = 2, +} + +export enum PositionType { + Static = 0, + Relative = 1, + Absolute = 2, +} + +export enum PrintOptions { + Layout = 1, + Style = 2, + Children = 4, +} + +export enum Unit { + Undefined = 0, + Point = 1, + Percent = 2, + Auto = 3, +} + +export enum Wrap { + NoWrap = 0, + Wrap = 1, + WrapReverse = 2, +} + +const constants = { + ALIGN_AUTO: Align.Auto, + ALIGN_FLEX_START: Align.FlexStart, + ALIGN_CENTER: Align.Center, + ALIGN_FLEX_END: Align.FlexEnd, + ALIGN_STRETCH: Align.Stretch, + ALIGN_BASELINE: Align.Baseline, + ALIGN_SPACE_BETWEEN: Align.SpaceBetween, + ALIGN_SPACE_AROUND: Align.SpaceAround, + DIMENSION_WIDTH: Dimension.Width, + DIMENSION_HEIGHT: Dimension.Height, + DIRECTION_INHERIT: Direction.Inherit, + DIRECTION_LTR: Direction.LTR, + DIRECTION_RTL: Direction.RTL, + DISPLAY_FLEX: Display.Flex, + DISPLAY_NONE: Display.None, + EDGE_LEFT: Edge.Left, + EDGE_TOP: Edge.Top, + EDGE_RIGHT: Edge.Right, + EDGE_BOTTOM: Edge.Bottom, + EDGE_START: Edge.Start, + EDGE_END: Edge.End, + EDGE_HORIZONTAL: Edge.Horizontal, + EDGE_VERTICAL: Edge.Vertical, + EDGE_ALL: Edge.All, + ERRATA_NONE: Errata.None, + ERRATA_STRETCH_FLEX_BASIS: Errata.StretchFlexBasis, + ERRATA_ALL: Errata.All, + ERRATA_CLASSIC: Errata.Classic, + EXPERIMENTAL_FEATURE_WEB_FLEX_BASIS: ExperimentalFeature.WebFlexBasis, + EXPERIMENTAL_FEATURE_ABSOLUTE_PERCENTAGE_AGAINST_PADDING_EDGE: ExperimentalFeature.AbsolutePercentageAgainstPaddingEdge, + EXPERIMENTAL_FEATURE_FIX_ABSOLUTE_TRAILING_COLUMN_MARGIN: ExperimentalFeature.FixAbsoluteTrailingColumnMargin, + FLEX_DIRECTION_COLUMN: FlexDirection.Column, + FLEX_DIRECTION_COLUMN_REVERSE: FlexDirection.ColumnReverse, + FLEX_DIRECTION_ROW: FlexDirection.Row, + FLEX_DIRECTION_ROW_REVERSE: FlexDirection.RowReverse, + GUTTER_COLUMN: Gutter.Column, + GUTTER_ROW: Gutter.Row, + GUTTER_ALL: Gutter.All, + JUSTIFY_FLEX_START: Justify.FlexStart, + JUSTIFY_CENTER: Justify.Center, + JUSTIFY_FLEX_END: Justify.FlexEnd, + JUSTIFY_SPACE_BETWEEN: Justify.SpaceBetween, + JUSTIFY_SPACE_AROUND: Justify.SpaceAround, + JUSTIFY_SPACE_EVENLY: Justify.SpaceEvenly, + LOG_LEVEL_ERROR: LogLevel.Error, + LOG_LEVEL_WARN: LogLevel.Warn, + LOG_LEVEL_INFO: LogLevel.Info, + LOG_LEVEL_DEBUG: LogLevel.Debug, + LOG_LEVEL_VERBOSE: LogLevel.Verbose, + LOG_LEVEL_FATAL: LogLevel.Fatal, + MEASURE_MODE_UNDEFINED: MeasureMode.Undefined, + MEASURE_MODE_EXACTLY: MeasureMode.Exactly, + MEASURE_MODE_AT_MOST: MeasureMode.AtMost, + NODE_TYPE_DEFAULT: NodeType.Default, + NODE_TYPE_TEXT: NodeType.Text, + OVERFLOW_VISIBLE: Overflow.Visible, + OVERFLOW_HIDDEN: Overflow.Hidden, + OVERFLOW_SCROLL: Overflow.Scroll, + POSITION_TYPE_STATIC: PositionType.Static, + POSITION_TYPE_RELATIVE: PositionType.Relative, + POSITION_TYPE_ABSOLUTE: PositionType.Absolute, + PRINT_OPTIONS_LAYOUT: PrintOptions.Layout, + PRINT_OPTIONS_STYLE: PrintOptions.Style, + PRINT_OPTIONS_CHILDREN: PrintOptions.Children, + UNIT_UNDEFINED: Unit.Undefined, + UNIT_POINT: Unit.Point, + UNIT_PERCENT: Unit.Percent, + UNIT_AUTO: Unit.Auto, + WRAP_NO_WRAP: Wrap.NoWrap, + WRAP_WRAP: Wrap.Wrap, + WRAP_WRAP_REVERSE: Wrap.WrapReverse, +} +export default constants \ No newline at end of file diff --git a/javascript/src/wrapAsm.d.ts b/javascript/src/wrapAsm.d.ts index 49b00e6c..d3dc069d 100644 --- a/javascript/src/wrapAsm.d.ts +++ b/javascript/src/wrapAsm.d.ts @@ -24,7 +24,7 @@ import type { Wrap, } from './generated/YGEnums'; -import type * as YGEnums from './generated/YGEnums'; +import YGEnums from './generated/YGEnums'; type Layout = { left: number; diff --git a/javascript/src/wrapAsm.js b/javascript/src/wrapAsm.js index f0280f1f..0df42074 100644 --- a/javascript/src/wrapAsm.js +++ b/javascript/src/wrapAsm.js @@ -7,7 +7,7 @@ * @format */ -const CONSTANTS = require('./generated/YGEnums'); +import YGEnums, {Unit, Direction} from './generated/YGEnums'; module.exports = lib => { function patch(prototype, name, fn) { @@ -31,9 +31,9 @@ module.exports = lib => { 'setPadding', ]) { const methods = { - [CONSTANTS.UNIT_POINT]: lib.Node.prototype[fnName], - [CONSTANTS.UNIT_PERCENT]: lib.Node.prototype[`${fnName}Percent`], - [CONSTANTS.UNIT_AUTO]: lib.Node.prototype[`${fnName}Auto`], + [Unit.Point]: lib.Node.prototype[fnName], + [Unit.Percent]: lib.Node.prototype[`${fnName}Percent`], + [Unit.Auto]: lib.Node.prototype[`${fnName}Auto`], }; patch(lib.Node.prototype, fnName, function (original, ...args) { @@ -44,7 +44,7 @@ module.exports = lib => { let unit, asNumber; if (value === 'auto') { - unit = CONSTANTS.UNIT_AUTO; + unit = Unit.Auto; asNumber = undefined; } else if (typeof value === 'object') { unit = value.unit; @@ -52,8 +52,8 @@ module.exports = lib => { } else { unit = typeof value === 'string' && value.endsWith('%') - ? CONSTANTS.UNIT_PERCENT - : CONSTANTS.UNIT_POINT; + ? Unit.Percent + : Unit.Point; asNumber = parseFloat(value); if (!Number.isNaN(value) && Number.isNaN(asNumber)) { throw new Error(`Invalid value ${value} for ${fnName}`); @@ -132,12 +132,7 @@ module.exports = lib => { patch( lib.Node.prototype, 'calculateLayout', - function ( - original, - width = NaN, - height = NaN, - direction = CONSTANTS.DIRECTION_LTR, - ) { + function (original, width = NaN, height = NaN, direction = Direction.LTR) { // Just a small patch to add support for the function default parameters return original.call(this, width, height, direction); }, @@ -146,6 +141,6 @@ module.exports = lib => { return { Config: lib.Config, Node: lib.Node, - ...CONSTANTS, + ...YGEnums, }; }; -- 2.50.1.windows.1 From c1d37fcf69d01543697853331f8b3ce93c5f4997 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 9 May 2023 21:36:38 -0700 Subject: [PATCH 2/2] Fixup TypeScript with export maps (#1284) Summary: Pull Request resolved: https://github.com/facebook/yoga/pull/1284 This makes TypeScript resolution play nicely with export maps, and converts the entrypoints to TypeScript. We remove the non-export-map fallbacks as well, so the export maps are always followed. Tests are moved to load yoga from its external export, testing the entrypoints. This moves the only untyped bit to the binary wrapper, which another diff will move. Reviewed By: yungsters Differential Revision: D45713689 fbshipit-source-id: a255b4faff57d6b579ef86cbcae6f0f5ee0fde1a --- javascript/.eslintrc.js | 54 +++++++++---------- javascript/.gitignore | 1 + javascript/CMakeLists.txt | 2 +- javascript/README.md | 2 +- javascript/jest.setup.ts | 8 +-- javascript/just.config.ts | 33 ++++++++++-- javascript/package.json | 24 +++++---- javascript/src/entrypoint/_entryAsync.js | 15 ------ javascript/src/entrypoint/_entrySync.js | 11 ---- javascript/src/entrypoint/asmjs-async.js | 11 ---- javascript/src/entrypoint/asmjs-async.ts | 26 +++++++++ javascript/src/entrypoint/asmjs-sync.js | 11 ---- javascript/src/entrypoint/asmjs-sync.ts | 23 ++++++++ javascript/src/entrypoint/wasm-async.js | 11 ---- javascript/src/entrypoint/wasm-async.ts | 26 +++++++++ javascript/src/entrypoint/wasm-sync.js | 11 ---- javascript/src/entrypoint/wasm-sync.ts | 23 ++++++++ javascript/src/index.d.ts | 15 ------ javascript/src/index.js | 11 ---- javascript/src/sync.d.ts | 16 ------ javascript/src/sync.js | 11 ---- .../src/{wrapAsm.d.ts => wrapAssembly.d.ts} | 2 +- .../src/{wrapAsm.js => wrapAssembly.js} | 0 javascript/tests/bin/run-bench.ts | 7 +-- javascript/tsconfig.json | 5 +- 25 files changed, 183 insertions(+), 176 deletions(-) delete mode 100644 javascript/src/entrypoint/_entryAsync.js delete mode 100644 javascript/src/entrypoint/_entrySync.js delete mode 100644 javascript/src/entrypoint/asmjs-async.js create mode 100644 javascript/src/entrypoint/asmjs-async.ts delete mode 100644 javascript/src/entrypoint/asmjs-sync.js create mode 100644 javascript/src/entrypoint/asmjs-sync.ts delete mode 100644 javascript/src/entrypoint/wasm-async.js create mode 100644 javascript/src/entrypoint/wasm-async.ts delete mode 100644 javascript/src/entrypoint/wasm-sync.js create mode 100644 javascript/src/entrypoint/wasm-sync.ts delete mode 100644 javascript/src/index.d.ts delete mode 100644 javascript/src/index.js delete mode 100644 javascript/src/sync.d.ts delete mode 100644 javascript/src/sync.js rename javascript/src/{wrapAsm.d.ts => wrapAssembly.d.ts} (99%) rename javascript/src/{wrapAsm.js => wrapAssembly.js} (100%) diff --git a/javascript/.eslintrc.js b/javascript/.eslintrc.js index 2baa5f0f..a350868b 100644 --- a/javascript/.eslintrc.js +++ b/javascript/.eslintrc.js @@ -7,20 +7,20 @@ * @format */ -const path = require("path"); +const path = require('path'); module.exports = { root: true, - ignorePatterns: ["dist/**", "tests/generated/**"], - extends: ["eslint:recommended", "plugin:prettier/recommended"], - plugins: ["prettier"], + ignorePatterns: ['dist/**', 'tests/generated/**'], + extends: ['eslint:recommended', 'plugin:prettier/recommended'], + plugins: ['prettier'], rules: { - "no-var": "error", - "prefer-arrow-callback": "error", - "prefer-const": "error", - "prefer-object-spread": "error", - "prefer-spread": "error", - "require-await": "error", + 'no-var': 'error', + 'prefer-arrow-callback': 'error', + 'prefer-const': 'error', + 'prefer-object-spread': 'error', + 'prefer-spread': 'error', + 'require-await': 'error', }, env: { commonjs: true, @@ -28,44 +28,44 @@ module.exports = { }, overrides: [ { - files: ["**/*.js"], - parser: "@babel/eslint-parser", + files: ['**/*.js'], + parser: '@babel/eslint-parser', parserOptions: { babelOptions: { - configFile: path.join(__dirname, ".babelrc.js"), + configFile: path.join(__dirname, '.babelrc.js'), }, }, }, { - files: ["**/*.ts"], - extends: ["plugin:@typescript-eslint/recommended"], - parser: "@typescript-eslint/parser", + files: ['**/*.ts'], + extends: ['plugin:@typescript-eslint/recommended'], + parser: '@typescript-eslint/parser', parserOptions: { - project: path.join(__dirname, "tsconfig.json"), + project: path.join(__dirname, 'tsconfig.json'), }, - plugins: ["@typescript-eslint"], + plugins: ['@typescript-eslint'], rules: { - "@typescript-eslint/no-var-requires": "off", + '@typescript-eslint/no-var-requires': 'off', }, }, { - files: ["**/.eslintrc.js", "**/just.config.js"], + files: ['**/.eslintrc.js', '**/just.config.js'], env: { node: true, }, }, { - files: ["jest.*", "tests/**"], + files: ['jest.*', 'tests/**'], env: { node: true, }, - extends: ["plugin:jest/recommended"], + extends: ['plugin:jest/recommended'], globals: { - getMeasureCounter: "writable", - getMeasureCounterMax: "writable", - getMeasureCounterMin: "writable", - Yoga: "writable", - YGBENCHMARK: "writable", + getMeasureCounter: 'writable', + getMeasureCounterMax: 'writable', + getMeasureCounterMin: 'writable', + Yoga: 'writable', + YGBENCHMARK: 'writable', }, }, ], diff --git a/javascript/.gitignore b/javascript/.gitignore index 29751e5c..198af7f7 100644 --- a/javascript/.gitignore +++ b/javascript/.gitignore @@ -1,3 +1,4 @@ +/binaries /build /dist /node_modules diff --git a/javascript/CMakeLists.txt b/javascript/CMakeLists.txt index 01177fcc..afb54bb5 100644 --- a/javascript/CMakeLists.txt +++ b/javascript/CMakeLists.txt @@ -49,7 +49,7 @@ link_libraries(embind) add_library(yogaObjLib OBJECT ${SOURCES}) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/dist/build) +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/binaries) add_executable(asmjs-sync $) target_link_options(asmjs-sync PUBLIC diff --git a/javascript/README.md b/javascript/README.md index ceeb50ac..87b07a2e 100644 --- a/javascript/README.md +++ b/javascript/README.md @@ -44,7 +44,7 @@ For better performance and smaller packages, WebAssembly is preferred to asm.js A specific entrypoint may be specified on platforms which do not understand export conditions. ```ts -import {loadYoga} from 'yoga-layout/dist/entrypoint/wasm-async'; +import {loadYoga} from 'yoga-layout/wasm-async'; ``` diff --git a/javascript/jest.setup.ts b/javascript/jest.setup.ts index 13639817..7ec11a77 100644 --- a/javascript/jest.setup.ts +++ b/javascript/jest.setup.ts @@ -9,13 +9,13 @@ module.exports = async () => { if (process.env['SYNC'] === '1' && process.env['WASM'] === '1') { - globalThis.Yoga = require('./dist/entrypoint/wasm-sync'); + globalThis.Yoga = require('yoga-layout/wasm-sync').default; } else if (process.env['SYNC'] === '1') { - globalThis.Yoga = require('./dist/entrypoint/asmjs-sync'); + globalThis.Yoga = require('yoga-layout/asmjs-sync').default; } else if (process.env['WASM'] === '1') { - globalThis.Yoga = await require('./dist/entrypoint/wasm-async').loadYoga(); + globalThis.Yoga = await require('yoga-layout/wasm-async').loadYoga(); } else { - globalThis.Yoga = await require('./dist/entrypoint/asmjs-async').loadYoga(); + globalThis.Yoga = await require('yoga-layout/asmjs-async').loadYoga(); } }; diff --git a/javascript/just.config.ts b/javascript/just.config.ts index dcc7776e..11d45988 100644 --- a/javascript/just.config.ts +++ b/javascript/just.config.ts @@ -21,6 +21,8 @@ import { tscTask, } from 'just-scripts'; +import {readFile, writeFile} from 'fs/promises'; + import glob from 'glob'; import path from 'path'; import which from 'which'; @@ -84,15 +86,38 @@ task( ), ); +task('prepack-package-json', async () => { + const packageJsonPath = path.join(__dirname, 'package.json'); + const packageJsonContents = await readFile(packageJsonPath); + const packageJson = JSON.parse(packageJsonContents.toString('utf-8')); + + recursiveReplace(packageJson, /(.\/src\/.*)\.ts/, '$1.js'); + await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)); +}); + task( - 'prepublish', - parallel( - 'build', - tscTask({emitDeclarationOnly: true}), + 'prepack', + series( + parallel('build', tscTask({emitDeclarationOnly: true})), babelTransformTask({dir: 'src'}), + 'prepack-package-json', ), ); +function recursiveReplace( + obj: Record, + pattern: RegExp, + replacement: string, +) { + for (const [key, value] of Object.entries(obj)) { + if (typeof value === 'string') { + obj[key] = value.replace(pattern, replacement); + } else if (typeof value === 'object' && value != null) { + recursiveReplace(value as Record, pattern, replacement); + } + } +} + function babelTransformTask(opts: {dir: string}) { return () => { const args = [ diff --git a/javascript/package.json b/javascript/package.json index 683ec3b3..b1f9d913 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -7,22 +7,24 @@ "type": "git", "url": "git@github.com:facebook/yoga.git" }, - "main": "./src/index.js", - "types": "./src/index.d.ts", "exports": { ".": { - "browser": "./src/entrypoint/wasm-async.js", - "node": "./src/entrypoint/wasm-async.js", - "default": "./src/entrypoint/asmjs-async.js" + "browser": "./src/entrypoint/wasm-async.ts", + "node": "./src/entrypoint/wasm-async.ts", + "default": "./src/entrypoint/asmjs-async.ts" }, "./sync": { - "browser": "./src/entrypoint/asmjs-sync.js", - "node": "./src/entrypoint/wasm-sync.js", - "default": "./src/entrypoint/asmjs-sync.js" - } + "browser": "./src/entrypoint/asmjs-sync.ts", + "node": "./src/entrypoint/wasm-sync.ts", + "default": "./src/entrypoint/asmjs-sync.ts" + }, + "./asmjs-async": "./src/entrypoint/asmjs-async.ts", + "./asmjs-sync": "./src/entrypoint/asmjs-sync.ts", + "./wasm-async": "./src/entrypoint/wasm-async.ts", + "./wasm-sync": "./src/entrypoint/wasm-sync.ts" }, "files": [ - "dist/**", + "binaries/**", "src/**" ], "scripts": { @@ -31,7 +33,7 @@ "clean": "just clean", "lint": "just lint", "lint:fix": "just lint --fix", - "prepublish": "just prepublish", + "prepack": "just prepack", "test": "just test", "test:asmjs-async": "just test:asmjs-async", "test:asmjs-sync": "just test:asmjs-sync", diff --git a/javascript/src/entrypoint/_entryAsync.js b/javascript/src/entrypoint/_entryAsync.js deleted file mode 100644 index 7d46c388..00000000 --- a/javascript/src/entrypoint/_entryAsync.js +++ /dev/null @@ -1,15 +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'); - -module.exports = loadAsm => ({ - loadYoga: () => loadAsm().then(wrapAsm), - ...require('../generated/YGEnums'), -}); diff --git a/javascript/src/entrypoint/_entrySync.js b/javascript/src/entrypoint/_entrySync.js deleted file mode 100644 index 9495d98c..00000000 --- a/javascript/src/entrypoint/_entrySync.js +++ /dev/null @@ -1,11 +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'); -module.exports = asm => wrapAsm(asm()); diff --git a/javascript/src/entrypoint/asmjs-async.js b/javascript/src/entrypoint/asmjs-async.js deleted file mode 100644 index eb1b52d7..00000000 --- a/javascript/src/entrypoint/asmjs-async.js +++ /dev/null @@ -1,11 +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 asm = require('../build/asmjs-async'); -module.exports = require('./_entryAsync')(asm); diff --git a/javascript/src/entrypoint/asmjs-async.ts b/javascript/src/entrypoint/asmjs-async.ts new file mode 100644 index 00000000..5ac7c71a --- /dev/null +++ b/javascript/src/entrypoint/asmjs-async.ts @@ -0,0 +1,26 @@ +/** + * 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 wrapAssembly from '../wrapAssembly'; +import type {Yoga} from '../wrapAssembly'; + +export * from '../generated/YGEnums'; +export type { + Config, + DirtiedFunction, + MeasureFunction, + Node, + Yoga, +} from '../wrapAssembly'; + +const loadAssembly = require('../../binaries/asmjs-async'); + +export async function loadYoga(): Promise { + return wrapAssembly(await loadAssembly()); +} diff --git a/javascript/src/entrypoint/asmjs-sync.js b/javascript/src/entrypoint/asmjs-sync.js deleted file mode 100644 index 69197714..00000000 --- a/javascript/src/entrypoint/asmjs-sync.js +++ /dev/null @@ -1,11 +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 asm = require('../build/asmjs-sync'); -module.exports = require('./_entrySync')(asm); diff --git a/javascript/src/entrypoint/asmjs-sync.ts b/javascript/src/entrypoint/asmjs-sync.ts new file mode 100644 index 00000000..710dbdbe --- /dev/null +++ b/javascript/src/entrypoint/asmjs-sync.ts @@ -0,0 +1,23 @@ +/** + * 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 wrapAssembly from '../wrapAssembly'; + +export * from '../generated/YGEnums'; +export type { + Config, + DirtiedFunction, + MeasureFunction, + Node, + Yoga, +} from '../wrapAssembly'; + +const loadAssembly = require('../../binaries/asmjs-sync'); +const Yoga = wrapAssembly(loadAssembly()); +export default Yoga; diff --git a/javascript/src/entrypoint/wasm-async.js b/javascript/src/entrypoint/wasm-async.js deleted file mode 100644 index ad5be421..00000000 --- a/javascript/src/entrypoint/wasm-async.js +++ /dev/null @@ -1,11 +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 asm = require('../build/wasm-async'); -module.exports = require('./_entryAsync')(asm); diff --git a/javascript/src/entrypoint/wasm-async.ts b/javascript/src/entrypoint/wasm-async.ts new file mode 100644 index 00000000..752f759d --- /dev/null +++ b/javascript/src/entrypoint/wasm-async.ts @@ -0,0 +1,26 @@ +/** + * 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 wrapAssembly from '../wrapAssembly'; +import type {Yoga} from '../wrapAssembly'; + +export * from '../generated/YGEnums'; +export type { + Config, + DirtiedFunction, + MeasureFunction, + Node, + Yoga, +} from '../wrapAssembly'; + +const loadAssembly = require('../../binaries/wasm-async'); + +export async function loadYoga(): Promise { + return wrapAssembly(await loadAssembly()); +} diff --git a/javascript/src/entrypoint/wasm-sync.js b/javascript/src/entrypoint/wasm-sync.js deleted file mode 100644 index 88983508..00000000 --- a/javascript/src/entrypoint/wasm-sync.js +++ /dev/null @@ -1,11 +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 asm = require('../build/wasm-sync'); -module.exports = require('./_entrySync')(asm); diff --git a/javascript/src/entrypoint/wasm-sync.ts b/javascript/src/entrypoint/wasm-sync.ts new file mode 100644 index 00000000..ff9bf702 --- /dev/null +++ b/javascript/src/entrypoint/wasm-sync.ts @@ -0,0 +1,23 @@ +/** + * 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 wrapAssembly from '../wrapAssembly'; + +export * from '../generated/YGEnums'; +export type { + Config, + DirtiedFunction, + MeasureFunction, + Node, + Yoga, +} from '../wrapAssembly'; + +const loadAssembly = require('../../binaries/wasm-sync'); +const Yoga = wrapAssembly(loadAssembly()); +export default Yoga; diff --git a/javascript/src/index.d.ts b/javascript/src/index.d.ts deleted file mode 100644 index fb2830e4..00000000 --- a/javascript/src/index.d.ts +++ /dev/null @@ -1,15 +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 - */ - -import type {Yoga} from './wrapAsm'; - -export * from './generated/YGEnums'; -export * from './wrapAsm'; - -export function loadYoga(): Promise; diff --git a/javascript/src/index.js b/javascript/src/index.js deleted file mode 100644 index 06a348c4..00000000 --- a/javascript/src/index.js +++ /dev/null @@ -1,11 +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 - */ - -// Fallback for when the export map is not followed -module.exports = require('./entrypoint/asmjs-async'); diff --git a/javascript/src/sync.d.ts b/javascript/src/sync.d.ts deleted file mode 100644 index 27bd8ecc..00000000 --- a/javascript/src/sync.d.ts +++ /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 - */ - -import type {Yoga} from './wrapAsm'; - -export * from './generated/YGEnums'; -export * from './wrapAsm'; - -declare const yoga: Yoga; -export default yoga; diff --git a/javascript/src/sync.js b/javascript/src/sync.js deleted file mode 100644 index 6ac610ef..00000000 --- a/javascript/src/sync.js +++ /dev/null @@ -1,11 +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 - */ - -// Fallback for when the export map is not followed -module.exports = require('./entrypoint/asmjs-sync'); diff --git a/javascript/src/wrapAsm.d.ts b/javascript/src/wrapAssembly.d.ts similarity index 99% rename from javascript/src/wrapAsm.d.ts rename to javascript/src/wrapAssembly.d.ts index d3dc069d..0b0b38b2 100644 --- a/javascript/src/wrapAsm.d.ts +++ b/javascript/src/wrapAssembly.d.ts @@ -187,5 +187,5 @@ export type Yoga = { }; } & typeof YGEnums; -declare const wrapAsm: () => Yoga; +declare const wrapAsm: (assembly: unknown) => Yoga; export default wrapAsm; diff --git a/javascript/src/wrapAsm.js b/javascript/src/wrapAssembly.js similarity index 100% rename from javascript/src/wrapAsm.js rename to javascript/src/wrapAssembly.js diff --git a/javascript/tests/bin/run-bench.ts b/javascript/tests/bin/run-bench.ts index 836f7f0b..d5c4d088 100644 --- a/javascript/tests/bin/run-bench.ts +++ b/javascript/tests/bin/run-bench.ts @@ -10,6 +10,9 @@ import path from 'path'; +import YogaAsmjs from 'yoga-layout/asmjs-sync'; +import YogaWasm from 'yoga-layout/wasm-sync'; + const WARMUP_ITERATIONS = 3; const BENCHMARK_ITERATIONS = 10; @@ -18,9 +21,7 @@ const testFiles = process.argv.slice(2); const testResults = new Map>(); for (const type of ['asmjs', 'wasm']) { - globalThis.Yoga = require(type === 'asmjs' - ? '../../dist/entrypoint/asmjs-sync' - : '../../dist/entrypoint/wasm-sync'); + globalThis.Yoga = type === 'asmjs' ? YogaAsmjs : YogaWasm; for (const file of testFiles) { globalThis.YGBENCHMARK = (name: string, fn: () => void) => { diff --git a/javascript/tsconfig.json b/javascript/tsconfig.json index 2568d1e1..ba6074ad 100644 --- a/javascript/tsconfig.json +++ b/javascript/tsconfig.json @@ -6,8 +6,11 @@ "declaration": true, "esModuleInterop": true, "skipLibCheck": true, - "forceConsistentCasingInFileNames": true, + // TODO: moduleResolution: "nodenext" is buggy with this if the absolute + // path contains any capital letters + "forceConsistentCasingInFileNames": false, "baseUrl": ".", + "moduleResolution": "nodenext", "paths": { "yoga-layout": ["src"] } -- 2.50.1.windows.1