@stylexjs/shared 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,6 +34,9 @@ export type StyleXOptions = Readonly<{
34
34
  classNamePrefix: string;
35
35
  debug: null | undefined | boolean;
36
36
  enableDebugClassNames?: null | undefined | boolean;
37
+ enableDebugDataProp?: null | undefined | boolean;
38
+ enableDevClassNames?: null | undefined | boolean;
39
+ enableMinifiedKeys?: null | undefined | boolean;
37
40
  definedStylexCSSVariables?: { [key: string]: unknown };
38
41
  dev: boolean;
39
42
  styleResolution:
@@ -39,6 +39,9 @@ export type StyleXOptions = $ReadOnly<{
39
39
  classNamePrefix: string,
40
40
  debug: ?boolean,
41
41
  enableDebugClassNames?: ?boolean,
42
+ enableDebugDataProp?: ?boolean,
43
+ enableDevClassNames?: ?boolean,
44
+ enableMinifiedKeys?: ?boolean,
42
45
  definedStylexCSSVariables?: { [key: string]: mixed },
43
46
  dev: boolean,
44
47
  styleResolution:
package/lib/hash.d.ts CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  declare const $$EXPORT_DEFAULT_DECLARATION$$: (str: string) => string;
11
11
  export default $$EXPORT_DEFAULT_DECLARATION$$;
12
+ export declare const createShortHash: (str: string) => string;
package/lib/hash.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = exports.createShortHash = void 0;
7
7
  function murmurhash2_32_gc(str) {
8
8
  let seed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
9
9
  let l = str.length,
@@ -34,4 +34,17 @@ function murmurhash2_32_gc(str) {
34
34
  return h >>> 0;
35
35
  }
36
36
  const hash = str => murmurhash2_32_gc(str, 1).toString(36);
37
- var _default = exports.default = hash;
37
+ var _default = exports.default = hash;
38
+ const base62Chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
39
+ function toBase62(num) {
40
+ let result = '';
41
+ let _num = num;
42
+ while (_num > 0) {
43
+ const remainder = _num % 62;
44
+ result = base62Chars[remainder] + result;
45
+ _num = Math.floor(_num / 62);
46
+ }
47
+ return result;
48
+ }
49
+ const createShortHash = str => toBase62(murmurhash2_32_gc(str, 1) % 62 ** 5);
50
+ exports.createShortHash = createShortHash;
package/lib/hash.js.flow CHANGED
@@ -8,3 +8,5 @@
8
8
  */
9
9
 
10
10
  declare export default (str: string) => string;
11
+
12
+ declare export const createShortHash: (str: string) => string;
package/lib/messages.d.ts CHANGED
@@ -39,3 +39,4 @@ export declare const NON_EXPORT_NAMED_DECLARATION: 'The return value of stylex.d
39
39
  export declare const ANONYMOUS_THEME: 'stylex.createTheme() must be bound to a named constant.';
40
40
  export declare const ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS: 'Only named parameters are allowed in Dynamic Style functions. Destructuring, spreading or default values are not allowed.';
41
41
  export declare const NON_CONTIGUOUS_VARS: 'All variables passed to `stylex.firstThatWorks` must be contiguous.';
42
+ export declare const NO_OBJECT_SPREADS: 'Object spreads are not allowed in stylex.create call.';
package/lib/messages.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.UNKNOWN_PROP_KEY = exports.UNKNOWN_NAMESPACE = exports.UNEXPECTED_ARGUMENT = exports.UNBOUND_STYLEX_CALL_VALUE = exports.ONLY_TOP_LEVEL = exports.ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS = exports.NO_PROJECT_ROOT_DIRECTORY = exports.NO_PARENT_PATH = exports.NO_CONDITIONAL_SHORTHAND = exports.NON_STATIC_VALUE = exports.NON_STATIC_KEYFRAME_VALUE = exports.NON_OBJECT_KEYFRAME = exports.NON_OBJECT_FOR_STYLEX_KEYFRAMES_CALL = exports.NON_OBJECT_FOR_STYLEX_CALL = exports.NON_EXPORT_NAMED_DECLARATION = exports.NON_CONTIGUOUS_VARS = exports.LOCAL_ONLY = exports.LINT_UNCLOSED_FUNCTION = exports.INVALID_SPREAD = exports.INVALID_PSEUDO_OR_AT_RULE = exports.INVALID_PSEUDO = exports.ILLEGAL_PROP_VALUE = exports.ILLEGAL_PROP_ARRAY_VALUE = exports.ILLEGAL_NESTED_PSEUDO = exports.ILLEGAL_NAMESPACE_VALUE = exports.ILLEGAL_NAMESPACE_TYPE = exports.ILLEGAL_ARG_LENGTH_FOR_KEYFRAMES = exports.ILLEGAL_ARGUMENT_LENGTH = exports.EXPECTED_FUNCTION_CALL = exports.ESCAPED_STYLEX_VALUE = exports.DUPLICATE_CONDITIONAL = exports.ANONYMOUS_THEME = void 0;
6
+ exports.UNKNOWN_PROP_KEY = exports.UNKNOWN_NAMESPACE = exports.UNEXPECTED_ARGUMENT = exports.UNBOUND_STYLEX_CALL_VALUE = exports.ONLY_TOP_LEVEL = exports.ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS = exports.NO_PROJECT_ROOT_DIRECTORY = exports.NO_PARENT_PATH = exports.NO_OBJECT_SPREADS = exports.NO_CONDITIONAL_SHORTHAND = exports.NON_STATIC_VALUE = exports.NON_STATIC_KEYFRAME_VALUE = exports.NON_OBJECT_KEYFRAME = exports.NON_OBJECT_FOR_STYLEX_KEYFRAMES_CALL = exports.NON_OBJECT_FOR_STYLEX_CALL = exports.NON_EXPORT_NAMED_DECLARATION = exports.NON_CONTIGUOUS_VARS = exports.LOCAL_ONLY = exports.LINT_UNCLOSED_FUNCTION = exports.INVALID_SPREAD = exports.INVALID_PSEUDO_OR_AT_RULE = exports.INVALID_PSEUDO = exports.ILLEGAL_PROP_VALUE = exports.ILLEGAL_PROP_ARRAY_VALUE = exports.ILLEGAL_NESTED_PSEUDO = exports.ILLEGAL_NAMESPACE_VALUE = exports.ILLEGAL_NAMESPACE_TYPE = exports.ILLEGAL_ARG_LENGTH_FOR_KEYFRAMES = exports.ILLEGAL_ARGUMENT_LENGTH = exports.EXPECTED_FUNCTION_CALL = exports.ESCAPED_STYLEX_VALUE = exports.DUPLICATE_CONDITIONAL = exports.ANONYMOUS_THEME = void 0;
7
7
  const ILLEGAL_ARGUMENT_LENGTH = exports.ILLEGAL_ARGUMENT_LENGTH = 'stylex.create() should have 1 argument.';
8
8
  const ILLEGAL_ARG_LENGTH_FOR_KEYFRAMES = exports.ILLEGAL_ARG_LENGTH_FOR_KEYFRAMES = 'stylex.keyframes() should have 1 argument.';
9
9
  const NON_STATIC_VALUE = exports.NON_STATIC_VALUE = 'Only static values are allowed inside of a stylex.create() call.';
@@ -35,4 +35,5 @@ const NO_PROJECT_ROOT_DIRECTORY = exports.NO_PROJECT_ROOT_DIRECTORY = 'The proje
35
35
  const NON_EXPORT_NAMED_DECLARATION = exports.NON_EXPORT_NAMED_DECLARATION = 'The return value of stylex.defineVars() must be bound to a named export.';
36
36
  const ANONYMOUS_THEME = exports.ANONYMOUS_THEME = 'stylex.createTheme() must be bound to a named constant.';
37
37
  const ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS = exports.ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS = 'Only named parameters are allowed in Dynamic Style functions. Destructuring, spreading or default values are not allowed.';
38
- const NON_CONTIGUOUS_VARS = exports.NON_CONTIGUOUS_VARS = 'All variables passed to `stylex.firstThatWorks` must be contiguous.';
38
+ const NON_CONTIGUOUS_VARS = exports.NON_CONTIGUOUS_VARS = 'All variables passed to `stylex.firstThatWorks` must be contiguous.';
39
+ const NO_OBJECT_SPREADS = exports.NO_OBJECT_SPREADS = 'Object spreads are not allowed in stylex.create call.';
@@ -42,3 +42,4 @@ declare export const NON_EXPORT_NAMED_DECLARATION: 'The return value of stylex.d
42
42
  declare export const ANONYMOUS_THEME: 'stylex.createTheme() must be bound to a named constant.';
43
43
  declare export const ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS: 'Only named parameters are allowed in Dynamic Style functions. Destructuring, spreading or default values are not allowed.';
44
44
  declare export const NON_CONTIGUOUS_VARS: 'All variables passed to `stylex.firstThatWorks` must be contiguous.';
45
+ declare export const NO_OBJECT_SPREADS: 'Object spreads are not allowed in stylex.create call.';
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ export type ConstsConfigValue = string | number;
11
+ export type ConstsConfig = Readonly<{ [$$Key$$: string]: ConstsConfigValue }>;
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ export type ConstsConfigValue = string | number;
11
+
12
+ export type ConstsConfig = $ReadOnly<{
13
+ [string]: ConstsConfigValue,
14
+ }>;
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = styleXCreateSet;
7
+ var _hash = require("./hash");
7
8
  var _defaultOptions = require("./utils/default-options");
8
9
  var _flattenRawStyleObj = require("./preprocess-rules/flatten-raw-style-obj");
9
10
  var _basicValidation = require("./preprocess-rules/basic-validation");
@@ -27,7 +28,13 @@ function styleXCreateSet(namespaces) {
27
28
  }, []);
28
29
  const compiledNamespaceTuples = flattenedNamespace.map(_ref => {
29
30
  let [key, value] = _ref;
30
- return [key, value.compiled(options)];
31
+ if (options.enableMinifiedKeys === true && !key.startsWith('--')) {
32
+ const hashedKey = (0, _hash.createShortHash)('<>' + key);
33
+ const displayKey = options.debug === true ? `${key}-k${hashedKey}` : `k${hashedKey}`;
34
+ return [displayKey, value.compiled(options)];
35
+ } else {
36
+ return [key, value.compiled(options)];
37
+ }
31
38
  });
32
39
  const namespaceObj = {};
33
40
  for (const [key, value] of compiledNamespaceTuples) {
@@ -13,7 +13,6 @@ import type {
13
13
  StyleXOptions,
14
14
  FlatCompiledStyles,
15
15
  } from './common-types';
16
-
17
16
  type ClassPathsInNamespace = {
18
17
  +[className: string]: $ReadOnlyArray<string>,
19
18
  };
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ import type { InjectableConstStyle, StyleXOptions } from './common-types';
11
+ import type { ConstsConfig } from './stylex-consts-utils';
12
+ declare function styleXDefineConsts<Vars extends ConstsConfig>(
13
+ constants: Vars,
14
+ options: Readonly<
15
+ Omit<Partial<StyleXOptions>, keyof ({ themeName: string })> & {
16
+ themeName: string;
17
+ }
18
+ >,
19
+ ): [
20
+ { [$$Key$$: string]: string | number },
21
+ { [$$Key$$: string]: InjectableConstStyle },
22
+ ];
23
+ export default styleXDefineConsts;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = styleXDefineConsts;
7
+ var _defaultOptions = require("./utils/default-options");
8
+ var messages = _interopRequireWildcard(require("./messages"));
9
+ var _hash = _interopRequireDefault(require("./hash"));
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
12
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
13
+ function styleXDefineConsts(constants, options) {
14
+ const {
15
+ classNamePrefix,
16
+ themeName,
17
+ debug,
18
+ enableDebugClassNames
19
+ } = {
20
+ ..._defaultOptions.defaultOptions,
21
+ ...options
22
+ };
23
+ const jsOutput = {};
24
+ const injectableStyles = {};
25
+ for (const [key, value] of Object.entries(constants)) {
26
+ if (key.startsWith('--')) {
27
+ throw new Error(messages.INVALID_CONST_KEY);
28
+ }
29
+ const varSafeKey = (key[0] >= '0' && key[0] <= '9' ? `_${key}` : key).replace(/[^a-zA-Z0-9]/g, '_');
30
+ const nameHash = debug && enableDebugClassNames ? `${varSafeKey}-${classNamePrefix}${(0, _hash.default)(`${themeName}.${key}`)}` : `${classNamePrefix}${(0, _hash.default)(`${themeName}.${key}`)}`;
31
+ jsOutput[key] = value;
32
+ injectableStyles[nameHash] = {
33
+ key: nameHash,
34
+ constKey: varSafeKey,
35
+ constVal: value,
36
+ priority: 0,
37
+ ltr: '',
38
+ rtl: null
39
+ };
40
+ }
41
+ return [jsOutput, injectableStyles];
42
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ import type { InjectableConstStyle, StyleXOptions } from './common-types';
11
+ import type { ConstsConfig } from './stylex-consts-utils';
12
+ declare export default function styleXDefineConsts<Vars: ConstsConfig>(
13
+ constants: Vars,
14
+ options: $ReadOnly<{ ...Partial<StyleXOptions>, themeName: string, ... }>,
15
+ ): [
16
+ { [string]: string | number }, // jsOutput JS output
17
+ { [string]: InjectableConstStyle }, // metadata for registerinjectableStyles
18
+ ];
@@ -14,7 +14,8 @@ function styleXDefineVars(variables, options) {
14
14
  const {
15
15
  classNamePrefix,
16
16
  themeName,
17
- debug
17
+ debug,
18
+ enableDebugClassNames
18
19
  } = {
19
20
  ..._defaultOptions.defaultOptions,
20
21
  ...options
@@ -22,7 +23,8 @@ function styleXDefineVars(variables, options) {
22
23
  const themeNameHash = classNamePrefix + (0, _hash.default)(themeName);
23
24
  const typedVariables = {};
24
25
  const variablesMap = (0, _objectUtils.objMap)(variables, (value, key) => {
25
- const nameHash = key.startsWith('--') ? key.slice(2) : debug ? key + '-' + classNamePrefix + (0, _hash.default)(`${themeName}.${key}`) : classNamePrefix + (0, _hash.default)(`${themeName}.${key}`);
26
+ const varSafeKey = (key[0] >= '0' && key[0] <= '9' ? `_${key}` : key).replace(/[^a-zA-Z0-9]/g, '_');
27
+ const nameHash = key.startsWith('--') ? key.slice(2) : debug && enableDebugClassNames ? varSafeKey + '-' + classNamePrefix + (0, _hash.default)(`${themeName}.${key}`) : classNamePrefix + (0, _hash.default)(`${themeName}.${key}`);
26
28
  if ((0, _types.isCSSType)(value)) {
27
29
  const v = value;
28
30
  typedVariables[nameHash] = {
@@ -5,11 +5,13 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.defaultOptions = void 0;
7
7
  const defaultOptions = exports.defaultOptions = {
8
+ classNamePrefix: 'x',
8
9
  dev: false,
9
10
  debug: false,
10
11
  enableDebugClassNames: true,
12
+ enableDebugDataProp: true,
13
+ enableMinifiedKeys: true,
11
14
  useRemForFontSize: true,
12
- test: false,
13
- classNamePrefix: 'x',
14
- styleResolution: 'application-order'
15
+ styleResolution: 'application-order',
16
+ test: false
15
17
  };
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@stylexjs/shared",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "main": "lib/index.js",
5
- "repository": "https://www.github.com/facebook/stylex",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/facebook/stylex.git"
8
+ },
6
9
  "license": "MIT",
7
10
  "scripts": {
8
11
  "prebuild": "gen-types -i src/ -o lib/",
@@ -13,7 +16,16 @@
13
16
  "postcss-value-parser": "^4.1.0"
14
17
  },
15
18
  "devDependencies": {
16
- "@stylexjs/scripts": "0.11.0"
19
+ "@babel/cli": "^7.26.4",
20
+ "@babel/core": "^7.26.8",
21
+ "@babel/eslint-parser": "^7.26.8",
22
+ "@babel/plugin-syntax-flow": "^7.26.0",
23
+ "@babel/preset-env": "^7.26.8",
24
+ "@babel/preset-flow": "^7.25.9",
25
+ "@babel/preset-react": "^7.26.3",
26
+ "@babel/preset-typescript": "^7.26.0",
27
+ "babel-plugin-syntax-hermes-parser": "^0.26.0",
28
+ "scripts": "0.12.0"
17
29
  },
18
30
  "jest": {
19
31
  "snapshotFormat": {