tailwindcss 0.0.0-oxide-insiders.a7fe2fe → 0.0.0-oxide-insiders.759a8c2

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.
Files changed (108) hide show
  1. package/lib/cli/build/deps.js +34 -18
  2. package/lib/cli/build/index.js +45 -12
  3. package/lib/cli/build/plugin.js +273 -90
  4. package/lib/cli/build/utils.js +56 -24
  5. package/lib/cli/build/watching.js +156 -38
  6. package/lib/cli/help/index.js +61 -16
  7. package/lib/cli/index.js +121 -62
  8. package/lib/cli/init/index.js +47 -23
  9. package/lib/cli-peer-dependencies.js +23 -13
  10. package/lib/cli.js +5 -1
  11. package/lib/corePluginList.js +7 -4
  12. package/lib/corePlugins.js +855 -386
  13. package/lib/featureFlags.js +43 -18
  14. package/lib/lib/cacheInvalidation.js +78 -30
  15. package/lib/lib/collapseAdjacentRules.js +46 -23
  16. package/lib/lib/collapseDuplicateDeclarations.js +70 -27
  17. package/lib/lib/content.js +134 -41
  18. package/lib/lib/defaultExtractor.js +201 -96
  19. package/lib/lib/detectNesting.js +38 -12
  20. package/lib/lib/evaluateTailwindFunctions.js +192 -103
  21. package/lib/lib/expandApplyAtRules.js +488 -181
  22. package/lib/lib/expandTailwindAtRules.js +240 -70
  23. package/lib/lib/findAtConfigPath.js +34 -15
  24. package/lib/lib/generateRules.js +775 -353
  25. package/lib/lib/getModuleDependencies.js +67 -29
  26. package/lib/lib/load-config.js +27 -17
  27. package/lib/lib/normalizeTailwindDirectives.js +73 -27
  28. package/lib/lib/offsets.js +227 -62
  29. package/lib/lib/partitionApplyAtRules.js +53 -29
  30. package/lib/lib/regex.js +50 -28
  31. package/lib/lib/remap-bitfield.js +86 -9
  32. package/lib/lib/resolveDefaultsAtRules.js +123 -53
  33. package/lib/lib/setupContextUtils.js +1117 -584
  34. package/lib/lib/setupTrackingContext.js +149 -58
  35. package/lib/lib/sharedState.js +71 -23
  36. package/lib/lib/substituteScreenAtRules.js +19 -9
  37. package/lib/oxide/cli/build/deps.js +46 -23
  38. package/lib/oxide/cli/build/index.js +44 -12
  39. package/lib/oxide/cli/build/plugin.js +271 -89
  40. package/lib/oxide/cli/build/utils.js +55 -24
  41. package/lib/oxide/cli/build/watching.js +153 -36
  42. package/lib/oxide/cli/help/index.js +60 -16
  43. package/lib/oxide/cli/index.js +128 -67
  44. package/lib/oxide/cli/init/index.js +40 -19
  45. package/lib/oxide/cli.js +3 -2
  46. package/lib/plugin.js +58 -31
  47. package/lib/postcss-plugins/nesting/index.js +13 -7
  48. package/lib/postcss-plugins/nesting/plugin.js +65 -20
  49. package/lib/processTailwindFeatures.js +41 -10
  50. package/lib/public/colors.js +25 -16
  51. package/lib/public/create-plugin.js +11 -6
  52. package/lib/public/default-config.js +12 -6
  53. package/lib/public/default-theme.js +12 -6
  54. package/lib/public/load-config.js +9 -5
  55. package/lib/public/resolve-config.js +10 -6
  56. package/lib/util/applyImportantSelector.js +28 -17
  57. package/lib/util/bigSign.js +9 -6
  58. package/lib/util/buildMediaQuery.js +19 -10
  59. package/lib/util/cloneDeep.js +19 -9
  60. package/lib/util/cloneNodes.js +29 -14
  61. package/lib/util/color.js +87 -47
  62. package/lib/util/colorNames.js +752 -0
  63. package/lib/util/configurePlugins.js +18 -9
  64. package/lib/util/createPlugin.js +17 -8
  65. package/lib/util/createUtilityPlugin.js +30 -16
  66. package/lib/util/dataTypes.js +189 -38
  67. package/lib/util/defaults.js +21 -9
  68. package/lib/util/escapeClassName.js +15 -9
  69. package/lib/util/escapeCommas.js +9 -6
  70. package/lib/util/flattenColorPalette.js +11 -7
  71. package/lib/util/formatVariantSelector.js +209 -60
  72. package/lib/util/getAllConfigs.js +44 -35
  73. package/lib/util/hashConfig.js +12 -8
  74. package/lib/util/isKeyframeRule.js +10 -7
  75. package/lib/util/isPlainObject.js +14 -9
  76. package/lib/util/isSyntacticallyValidPropertyValue.js +45 -14
  77. package/lib/util/log.js +20 -9
  78. package/lib/util/nameClass.js +31 -10
  79. package/lib/util/negateValue.js +28 -10
  80. package/lib/util/normalizeConfig.js +242 -63
  81. package/lib/util/normalizeScreens.js +145 -70
  82. package/lib/util/parseAnimationValue.js +64 -14
  83. package/lib/util/parseBoxShadowValue.js +63 -14
  84. package/lib/util/parseDependency.js +39 -13
  85. package/lib/util/parseGlob.js +25 -8
  86. package/lib/util/parseObjectStyles.js +29 -18
  87. package/lib/util/pluginUtils.js +196 -77
  88. package/lib/util/prefixSelector.js +31 -12
  89. package/lib/util/pseudoElements.js +116 -34
  90. package/lib/util/removeAlphaVariables.js +23 -8
  91. package/lib/util/resolveConfig.js +224 -105
  92. package/lib/util/resolveConfigPath.js +45 -15
  93. package/lib/util/responsive.js +10 -6
  94. package/lib/util/splitAtTopLevelOnly.js +46 -10
  95. package/lib/util/tap.js +11 -7
  96. package/lib/util/toColorValue.js +10 -7
  97. package/lib/util/toPath.js +28 -8
  98. package/lib/util/transformThemeValue.js +49 -14
  99. package/lib/util/validateConfig.js +25 -15
  100. package/lib/util/validateFormalSyntax.js +14 -6
  101. package/lib/util/withAlphaVariable.js +54 -30
  102. package/package.json +18 -22
  103. package/peers/index.js +57849 -43101
  104. package/src/lib/expandTailwindAtRules.js +1 -1
  105. package/src/lib/setupTrackingContext.js +1 -1
  106. package/src/util/color.js +1 -1
  107. package/src/util/colorNames.js +150 -0
  108. package/src/util/pseudoElements.js +4 -4
@@ -1,12 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", {
3
- value: !0
4
- }), Object.defineProperty(exports, "default", {
5
- enumerable: !0,
6
- get: ()=>getModuleDependencies
3
+ value: true
7
4
  });
8
- const _fs = _interopRequireDefault(require("fs")), _path = _interopRequireDefault(require("path"));
9
- function _interopRequireDefault(obj) {
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getModuleDependencies;
9
+ }
10
+ });
11
+ const _fs = /*#__PURE__*/ _interop_require_default(require("fs"));
12
+ const _path = /*#__PURE__*/ _interop_require_default(require("path"));
13
+ function _interop_require_default(obj) {
10
14
  return obj && obj.__esModule ? obj : {
11
15
  default: obj
12
16
  };
@@ -15,7 +19,18 @@ let jsExtensions = [
15
19
  ".js",
16
20
  ".cjs",
17
21
  ".mjs"
18
- ], jsResolutionOrder = [
22
+ ];
23
+ // Given the current file `a.ts`, we want to make sure that when importing `b` that we resolve
24
+ // `b.ts` before `b.js`
25
+ //
26
+ // E.g.:
27
+ //
28
+ // a.ts
29
+ // b // .ts
30
+ // c // .ts
31
+ // a.js
32
+ // b // .js or .ts
33
+ let jsResolutionOrder = [
19
34
  "",
20
35
  ".js",
21
36
  ".cjs",
@@ -25,7 +40,8 @@ let jsExtensions = [
25
40
  ".mts",
26
41
  ".jsx",
27
42
  ".tsx"
28
- ], tsResolutionOrder = [
43
+ ];
44
+ let tsResolutionOrder = [
29
45
  "",
30
46
  ".ts",
31
47
  ".cts",
@@ -36,26 +52,48 @@ let jsExtensions = [
36
52
  ".mjs",
37
53
  ".jsx"
38
54
  ];
55
+ function resolveWithExtension(file, extensions) {
56
+ // Try to find `./a.ts`, `./a.ts`, ... from `./a`
57
+ for (let ext of extensions){
58
+ let full = `${file}${ext}`;
59
+ if (_fs.default.existsSync(full) && _fs.default.statSync(full).isFile()) {
60
+ return full;
61
+ }
62
+ }
63
+ // Try to find `./a/index.js` from `./a`
64
+ for (let ext of extensions){
65
+ let full = `${file}/index${ext}`;
66
+ if (_fs.default.existsSync(full)) {
67
+ return full;
68
+ }
69
+ }
70
+ return null;
71
+ }
72
+ function* _getModuleDependencies(filename, base, seen, ext = _path.default.extname(filename)) {
73
+ // Try to find the file
74
+ let absoluteFile = resolveWithExtension(_path.default.resolve(base, filename), jsExtensions.includes(ext) ? jsResolutionOrder : tsResolutionOrder);
75
+ if (absoluteFile === null) return; // File doesn't exist
76
+ // Prevent infinite loops when there are circular dependencies
77
+ if (seen.has(absoluteFile)) return; // Already seen
78
+ seen.add(absoluteFile);
79
+ // Mark the file as a dependency
80
+ yield absoluteFile;
81
+ // Resolve new base for new imports/requires
82
+ base = _path.default.dirname(absoluteFile);
83
+ ext = _path.default.extname(absoluteFile);
84
+ let contents = _fs.default.readFileSync(absoluteFile, "utf-8");
85
+ // Find imports/requires
86
+ for (let match of [
87
+ ...contents.matchAll(/import[\s\S]*?['"](.{3,}?)['"]/gi),
88
+ ...contents.matchAll(/import[\s\S]*from[\s\S]*?['"](.{3,}?)['"]/gi),
89
+ ...contents.matchAll(/require\(['"`](.+)['"`]\)/gi)
90
+ ]){
91
+ // Bail out if it's not a relative file
92
+ if (!match[1].startsWith(".")) continue;
93
+ yield* _getModuleDependencies(match[1], base, seen, ext);
94
+ }
95
+ }
39
96
  function getModuleDependencies(absoluteFilePath) {
40
- return null === absoluteFilePath ? new Set() : new Set(function* _getModuleDependencies(filename, base, seen, ext = _path.default.extname(filename)) {
41
- let absoluteFile = function(file, extensions) {
42
- for (let ext of extensions){
43
- let full = `${file}${ext}`;
44
- if (_fs.default.existsSync(full) && _fs.default.statSync(full).isFile()) return full;
45
- }
46
- for (let ext of extensions){
47
- let full = `${file}/index${ext}`;
48
- if (_fs.default.existsSync(full)) return full;
49
- }
50
- return null;
51
- }(_path.default.resolve(base, filename), jsExtensions.includes(ext) ? jsResolutionOrder : tsResolutionOrder);
52
- if (null === absoluteFile || seen.has(absoluteFile)) return;
53
- seen.add(absoluteFile), yield absoluteFile, base = _path.default.dirname(absoluteFile), ext = _path.default.extname(absoluteFile);
54
- let contents = _fs.default.readFileSync(absoluteFile, "utf-8");
55
- for (let match of [
56
- ...contents.matchAll(/import[\s\S]*?['"](.{3,}?)['"]/gi),
57
- ...contents.matchAll(/import[\s\S]*from[\s\S]*?['"](.{3,}?)['"]/gi),
58
- ...contents.matchAll(/require\(['"`](.+)['"`]\)/gi)
59
- ])match[1].startsWith(".") && (yield* _getModuleDependencies(match[1], base, seen, ext));
60
- }(absoluteFilePath, _path.default.dirname(absoluteFilePath), new Set()));
97
+ if (absoluteFilePath === null) return new Set();
98
+ return new Set(_getModuleDependencies(absoluteFilePath, _path.default.dirname(absoluteFilePath), new Set()));
61
99
  }
@@ -1,32 +1,42 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", {
3
- value: !0
4
- }), Object.defineProperty(exports, "loadConfig", {
5
- enumerable: !0,
6
- get: ()=>loadConfig
3
+ value: true
7
4
  });
8
- const _jiti = function(obj) {
5
+ Object.defineProperty(exports, "loadConfig", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return loadConfig;
9
+ }
10
+ });
11
+ const _jiti = /*#__PURE__*/ _interop_require_default(require("jiti"));
12
+ const _sucrase = require("sucrase");
13
+ function _interop_require_default(obj) {
9
14
  return obj && obj.__esModule ? obj : {
10
15
  default: obj
11
16
  };
12
- }(require("jiti")), _sucrase = require("sucrase");
17
+ }
13
18
  let jiti = null;
19
+ function lazyJiti() {
20
+ return jiti !== null && jiti !== void 0 ? jiti : jiti = (0, _jiti.default)(__filename, {
21
+ interopDefault: true,
22
+ transform: (opts)=>{
23
+ return (0, _sucrase.transform)(opts.source, {
24
+ transforms: [
25
+ "typescript",
26
+ "imports"
27
+ ]
28
+ });
29
+ }
30
+ });
31
+ }
14
32
  function loadConfig(path) {
15
- var _config_default;
16
33
  let config = function() {
17
34
  try {
18
35
  return path ? require(path) : {};
19
36
  } catch {
20
- return (null != jiti ? jiti : jiti = (0, _jiti.default)(__filename, {
21
- interopDefault: !0,
22
- transform: (opts)=>(0, _sucrase.transform)(opts.source, {
23
- transforms: [
24
- "typescript",
25
- "imports"
26
- ]
27
- })
28
- }))(path);
37
+ return lazyJiti()(path);
29
38
  }
30
39
  }();
31
- return null !== (_config_default = config.default) && void 0 !== _config_default ? _config_default : config;
40
+ var _config_default;
41
+ return (_config_default = config.default) !== null && _config_default !== void 0 ? _config_default : config;
32
42
  }
@@ -1,40 +1,86 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", {
3
- value: !0
4
- }), Object.defineProperty(exports, "default", {
5
- enumerable: !0,
6
- get: ()=>normalizeTailwindDirectives
3
+ value: true
7
4
  });
8
- const _log = function(obj) {
5
+ Object.defineProperty(exports, "default", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return normalizeTailwindDirectives;
9
+ }
10
+ });
11
+ const _log = /*#__PURE__*/ _interop_require_default(require("../util/log"));
12
+ function _interop_require_default(obj) {
9
13
  return obj && obj.__esModule ? obj : {
10
14
  default: obj
11
15
  };
12
- }(require("../util/log"));
16
+ }
13
17
  function normalizeTailwindDirectives(root) {
14
- let tailwindDirectives = new Set(), layerDirectives = new Set(), applyDirectives = new Set();
15
- if (root.walkAtRules((atRule)=>{
16
- "apply" === atRule.name && applyDirectives.add(atRule), "import" === atRule.name && ('"tailwindcss/base"' === atRule.params || "'tailwindcss/base'" === atRule.params ? (atRule.name = "tailwind", atRule.params = "base") : '"tailwindcss/components"' === atRule.params || "'tailwindcss/components'" === atRule.params ? (atRule.name = "tailwind", atRule.params = "components") : '"tailwindcss/utilities"' === atRule.params || "'tailwindcss/utilities'" === atRule.params ? (atRule.name = "tailwind", atRule.params = "utilities") : ('"tailwindcss/screens"' === atRule.params || "'tailwindcss/screens'" === atRule.params || '"tailwindcss/variants"' === atRule.params || "'tailwindcss/variants'" === atRule.params) && (atRule.name = "tailwind", atRule.params = "variants")), "tailwind" === atRule.name && ("screens" === atRule.params && (atRule.params = "variants"), tailwindDirectives.add(atRule.params)), [
18
+ let tailwindDirectives = new Set();
19
+ let layerDirectives = new Set();
20
+ let applyDirectives = new Set();
21
+ root.walkAtRules((atRule)=>{
22
+ if (atRule.name === "apply") {
23
+ applyDirectives.add(atRule);
24
+ }
25
+ if (atRule.name === "import") {
26
+ if (atRule.params === '"tailwindcss/base"' || atRule.params === "'tailwindcss/base'") {
27
+ atRule.name = "tailwind";
28
+ atRule.params = "base";
29
+ } else if (atRule.params === '"tailwindcss/components"' || atRule.params === "'tailwindcss/components'") {
30
+ atRule.name = "tailwind";
31
+ atRule.params = "components";
32
+ } else if (atRule.params === '"tailwindcss/utilities"' || atRule.params === "'tailwindcss/utilities'") {
33
+ atRule.name = "tailwind";
34
+ atRule.params = "utilities";
35
+ } else if (atRule.params === '"tailwindcss/screens"' || atRule.params === "'tailwindcss/screens'" || atRule.params === '"tailwindcss/variants"' || atRule.params === "'tailwindcss/variants'") {
36
+ atRule.name = "tailwind";
37
+ atRule.params = "variants";
38
+ }
39
+ }
40
+ if (atRule.name === "tailwind") {
41
+ if (atRule.params === "screens") {
42
+ atRule.params = "variants";
43
+ }
44
+ tailwindDirectives.add(atRule.params);
45
+ }
46
+ if ([
17
47
  "layer",
18
48
  "responsive",
19
49
  "variants"
20
- ].includes(atRule.name) && ([
21
- "responsive",
22
- "variants"
23
- ].includes(atRule.name) && _log.default.warn(`${atRule.name}-at-rule-deprecated`, [
24
- `The \`@${atRule.name}\` directive has been deprecated in Tailwind CSS v3.0.`,
25
- "Use `@layer utilities` or `@layer components` instead.",
26
- "https://tailwindcss.com/docs/upgrade-guide#replace-variants-with-layer"
27
- ]), layerDirectives.add(atRule));
28
- }), !tailwindDirectives.has("base") || !tailwindDirectives.has("components") || !tailwindDirectives.has("utilities")) {
29
- for (let rule of layerDirectives)if ("layer" === rule.name && [
30
- "base",
31
- "components",
32
- "utilities"
33
- ].includes(rule.params)) {
34
- if (!tailwindDirectives.has(rule.params)) throw rule.error(`\`@layer ${rule.params}\` is used but no matching \`@tailwind ${rule.params}\` directive is present.`);
35
- } else if ("responsive" === rule.name) {
36
- if (!tailwindDirectives.has("utilities")) throw rule.error("`@responsive` is used but `@tailwind utilities` is missing.");
37
- } else if ("variants" === rule.name && !tailwindDirectives.has("utilities")) throw rule.error("`@variants` is used but `@tailwind utilities` is missing.");
50
+ ].includes(atRule.name)) {
51
+ if ([
52
+ "responsive",
53
+ "variants"
54
+ ].includes(atRule.name)) {
55
+ _log.default.warn(`${atRule.name}-at-rule-deprecated`, [
56
+ `The \`@${atRule.name}\` directive has been deprecated in Tailwind CSS v3.0.`,
57
+ `Use \`@layer utilities\` or \`@layer components\` instead.`,
58
+ "https://tailwindcss.com/docs/upgrade-guide#replace-variants-with-layer"
59
+ ]);
60
+ }
61
+ layerDirectives.add(atRule);
62
+ }
63
+ });
64
+ if (!tailwindDirectives.has("base") || !tailwindDirectives.has("components") || !tailwindDirectives.has("utilities")) {
65
+ for (let rule of layerDirectives){
66
+ if (rule.name === "layer" && [
67
+ "base",
68
+ "components",
69
+ "utilities"
70
+ ].includes(rule.params)) {
71
+ if (!tailwindDirectives.has(rule.params)) {
72
+ throw rule.error(`\`@layer ${rule.params}\` is used but no matching \`@tailwind ${rule.params}\` directive is present.`);
73
+ }
74
+ } else if (rule.name === "responsive") {
75
+ if (!tailwindDirectives.has("utilities")) {
76
+ throw rule.error("`@responsive` is used but `@tailwind utilities` is missing.");
77
+ }
78
+ } else if (rule.name === "variants") {
79
+ if (!tailwindDirectives.has("utilities")) {
80
+ throw rule.error("`@variants` is used but `@tailwind utilities` is missing.");
81
+ }
82
+ }
83
+ }
38
84
  }
39
85
  return {
40
86
  tailwindDirectives,
@@ -1,34 +1,64 @@
1
+ // @ts-check
1
2
  "use strict";
2
3
  Object.defineProperty(exports, "__esModule", {
3
- value: !0
4
- }), Object.defineProperty(exports, "Offsets", {
5
- enumerable: !0,
6
- get: ()=>Offsets
4
+ value: true
7
5
  });
8
- const _bigSign = function(obj) {
6
+ Object.defineProperty(exports, "Offsets", {
7
+ enumerable: true,
8
+ get: function() {
9
+ return Offsets;
10
+ }
11
+ });
12
+ const _bigSign = /*#__PURE__*/ _interop_require_default(require("../util/bigSign"));
13
+ const _remapbitfield = require("./remap-bitfield.js");
14
+ function _interop_require_default(obj) {
9
15
  return obj && obj.__esModule ? obj : {
10
16
  default: obj
11
17
  };
12
- }(require("../util/bigSign")), _remapBitfieldJs = require("./remap-bitfield.js");
18
+ }
13
19
  class Offsets {
14
20
  constructor(){
15
- this.offsets = {
21
+ /**
22
+ * Offsets for the next rule in a given layer
23
+ *
24
+ * @type {Record<Layer, bigint>}
25
+ */ this.offsets = {
16
26
  defaults: 0n,
17
27
  base: 0n,
18
28
  components: 0n,
19
29
  utilities: 0n,
20
30
  variants: 0n,
21
31
  user: 0n
22
- }, this.layerPositions = {
32
+ };
33
+ /**
34
+ * Positions for a given layer
35
+ *
36
+ * @type {Record<Layer, bigint>}
37
+ */ this.layerPositions = {
23
38
  defaults: 0n,
24
39
  base: 1n,
25
40
  components: 2n,
26
41
  utilities: 3n,
42
+ // There isn't technically a "user" layer, but we need to give it a position
43
+ // Because it's used for ordering user-css from @apply
27
44
  user: 4n,
28
45
  variants: 5n
29
- }, this.reservedVariantBits = 0n, this.variantOffsets = new Map();
46
+ };
47
+ /**
48
+ * The total number of functions currently registered across all variants (including arbitrary variants)
49
+ *
50
+ * @type {bigint}
51
+ */ this.reservedVariantBits = 0n;
52
+ /**
53
+ * Positions for a given variant
54
+ *
55
+ * @type {Map<string, bigint>}
56
+ */ this.variantOffsets = new Map();
30
57
  }
31
- create(layer) {
58
+ /**
59
+ * @param {Layer} layer
60
+ * @returns {RuleOffset}
61
+ */ create(layer) {
32
62
  return {
33
63
  layer,
34
64
  parentLayer: layer,
@@ -39,103 +69,238 @@ class Offsets {
39
69
  options: []
40
70
  };
41
71
  }
42
- arbitraryProperty() {
72
+ /**
73
+ * @returns {RuleOffset}
74
+ */ arbitraryProperty() {
43
75
  return {
44
76
  ...this.create("utilities"),
45
77
  arbitrary: 1n
46
78
  };
47
79
  }
48
- forVariant(variant, index = 0) {
80
+ /**
81
+ * Get the offset for a variant
82
+ *
83
+ * @param {string} variant
84
+ * @param {number} index
85
+ * @returns {RuleOffset}
86
+ */ forVariant(variant, index = 0) {
49
87
  let offset = this.variantOffsets.get(variant);
50
- if (void 0 === offset) throw Error(`Cannot find offset for unknown variant ${variant}`);
88
+ if (offset === undefined) {
89
+ throw new Error(`Cannot find offset for unknown variant ${variant}`);
90
+ }
51
91
  return {
52
92
  ...this.create("variants"),
53
93
  variants: offset << BigInt(index)
54
94
  };
55
95
  }
56
- applyVariantOffset(rule, variant, options) {
57
- return options.variant = variant.variants, {
96
+ /**
97
+ * @param {RuleOffset} rule
98
+ * @param {RuleOffset} variant
99
+ * @param {VariantOption} options
100
+ * @returns {RuleOffset}
101
+ */ applyVariantOffset(rule, variant, options) {
102
+ options.variant = variant.variants;
103
+ return {
58
104
  ...rule,
59
105
  layer: "variants",
60
- parentLayer: "variants" === rule.layer ? rule.parentLayer : rule.layer,
106
+ parentLayer: rule.layer === "variants" ? rule.parentLayer : rule.layer,
61
107
  variants: rule.variants | variant.variants,
62
108
  options: options.sort ? [].concat(options, rule.options) : rule.options,
109
+ // TODO: Technically this is wrong. We should be handling parallel index on a per variant basis.
110
+ // We'll take the max of all the parallel indexes for now.
111
+ // @ts-ignore
63
112
  parallelIndex: max([
64
113
  rule.parallelIndex,
65
114
  variant.parallelIndex
66
115
  ])
67
116
  };
68
117
  }
69
- applyParallelOffset(offset, parallelIndex) {
118
+ /**
119
+ * @param {RuleOffset} offset
120
+ * @param {number} parallelIndex
121
+ * @returns {RuleOffset}
122
+ */ applyParallelOffset(offset, parallelIndex) {
70
123
  return {
71
124
  ...offset,
72
125
  parallelIndex: BigInt(parallelIndex)
73
126
  };
74
127
  }
75
- recordVariants(variants, getLength) {
76
- for (let variant of variants)this.recordVariant(variant, getLength(variant));
128
+ /**
129
+ * Each variant gets 1 bit per function / rule registered.
130
+ * This is because multiple variants can be applied to a single rule and we need to know which ones are present and which ones are not.
131
+ * Additionally, every unique group of variants is grouped together in the stylesheet.
132
+ *
133
+ * This grouping is order-independent. For instance, we do not differentiate between `hover:focus` and `focus:hover`.
134
+ *
135
+ * @param {string[]} variants
136
+ * @param {(name: string) => number} getLength
137
+ */ recordVariants(variants, getLength) {
138
+ for (let variant of variants){
139
+ this.recordVariant(variant, getLength(variant));
140
+ }
77
141
  }
78
- recordVariant(variant, fnCount = 1) {
79
- return this.variantOffsets.set(variant, 1n << this.reservedVariantBits), this.reservedVariantBits += BigInt(fnCount), {
142
+ /**
143
+ * The same as `recordVariants` but for a single arbitrary variant at runtime.
144
+ * @param {string} variant
145
+ * @param {number} fnCount
146
+ *
147
+ * @returns {RuleOffset} The highest offset for this variant
148
+ */ recordVariant(variant, fnCount = 1) {
149
+ this.variantOffsets.set(variant, 1n << this.reservedVariantBits);
150
+ // Ensure space is reserved for each "function" in the parallel variant
151
+ // by offsetting the next variant by the number of parallel variants
152
+ // in the one we just added.
153
+ // Single functions that return parallel variants are NOT handled separately here
154
+ // They're offset by 1 (or the number of functions) as usual
155
+ // And each rule returned is tracked separately since the functions are evaluated lazily.
156
+ // @see `RuleOffset.parallelIndex`
157
+ this.reservedVariantBits += BigInt(fnCount);
158
+ return {
80
159
  ...this.create("variants"),
81
160
  variants: this.variantOffsets.get(variant)
82
161
  };
83
162
  }
84
- compare(a, b) {
85
- if (a.layer !== b.layer) return this.layerPositions[a.layer] - this.layerPositions[b.layer];
86
- if (a.parentLayer !== b.parentLayer) return this.layerPositions[a.parentLayer] - this.layerPositions[b.parentLayer];
87
- for (let aOptions of a.options)for (let bOptions of b.options){
88
- var _max;
89
- if (aOptions.id !== bOptions.id || !aOptions.sort || !bOptions.sort) continue;
90
- let maxFnVariant = null !== (_max = max([
91
- aOptions.variant,
92
- bOptions.variant
93
- ])) && void 0 !== _max ? _max : 0n, mask = ~(maxFnVariant | maxFnVariant - 1n);
94
- if ((a.variants & mask) != (b.variants & mask)) continue;
95
- let result = aOptions.sort({
96
- value: aOptions.value,
97
- modifier: aOptions.modifier
98
- }, {
99
- value: bOptions.value,
100
- modifier: bOptions.modifier
101
- });
102
- if (0 !== result) return result;
163
+ /**
164
+ * @param {RuleOffset} a
165
+ * @param {RuleOffset} b
166
+ * @returns {bigint}
167
+ */ compare(a, b) {
168
+ // Sort layers together
169
+ if (a.layer !== b.layer) {
170
+ return this.layerPositions[a.layer] - this.layerPositions[b.layer];
103
171
  }
104
- return a.variants !== b.variants ? a.variants - b.variants : a.parallelIndex !== b.parallelIndex ? a.parallelIndex - b.parallelIndex : a.arbitrary !== b.arbitrary ? a.arbitrary - b.arbitrary : a.index - b.index;
105
- }
106
- recalculateVariantOffsets() {
107
- let variants = Array.from(this.variantOffsets.entries()).filter(([v])=>v.startsWith("[")).sort(([a], [z])=>(function(a, b) {
108
- let aLen = a.length, bLen = b.length, minLen = aLen < bLen ? aLen : bLen;
109
- for(let i = 0; i < minLen; i++){
110
- let cmp = a.charCodeAt(i) - b.charCodeAt(i);
111
- if (0 !== cmp) return cmp;
172
+ // When sorting the `variants` layer, we need to sort based on the parent layer as well within
173
+ // this variants layer.
174
+ if (a.parentLayer !== b.parentLayer) {
175
+ return this.layerPositions[a.parentLayer] - this.layerPositions[b.parentLayer];
176
+ }
177
+ // Sort based on the sorting function
178
+ for (let aOptions of a.options){
179
+ for (let bOptions of b.options){
180
+ if (aOptions.id !== bOptions.id) continue;
181
+ if (!aOptions.sort || !bOptions.sort) continue;
182
+ var _max;
183
+ let maxFnVariant = (_max = max([
184
+ aOptions.variant,
185
+ bOptions.variant
186
+ ])) !== null && _max !== void 0 ? _max : 0n;
187
+ // Create a mask of 0s from bits 1..N where N represents the mask of the Nth bit
188
+ let mask = ~(maxFnVariant | maxFnVariant - 1n);
189
+ let aVariantsAfterFn = a.variants & mask;
190
+ let bVariantsAfterFn = b.variants & mask;
191
+ // If the variants the same, we _can_ sort them
192
+ if (aVariantsAfterFn !== bVariantsAfterFn) {
193
+ continue;
112
194
  }
113
- return aLen - bLen;
114
- })(a, z)), newOffsets = variants.map(([, offset])=>offset).sort((a, z)=>(0, _bigSign.default)(a - z));
115
- return variants.map(([, oldOffset], i)=>[
195
+ let result = aOptions.sort({
196
+ value: aOptions.value,
197
+ modifier: aOptions.modifier
198
+ }, {
199
+ value: bOptions.value,
200
+ modifier: bOptions.modifier
201
+ });
202
+ if (result !== 0) return result;
203
+ }
204
+ }
205
+ // Sort variants in the order they were registered
206
+ if (a.variants !== b.variants) {
207
+ return a.variants - b.variants;
208
+ }
209
+ // Make sure each rule returned by a parallel variant is sorted in ascending order
210
+ if (a.parallelIndex !== b.parallelIndex) {
211
+ return a.parallelIndex - b.parallelIndex;
212
+ }
213
+ // Always sort arbitrary properties after other utilities
214
+ if (a.arbitrary !== b.arbitrary) {
215
+ return a.arbitrary - b.arbitrary;
216
+ }
217
+ // Sort utilities, components, etc… in the order they were registered
218
+ return a.index - b.index;
219
+ }
220
+ /**
221
+ * Arbitrary variants are recorded in the order they're encountered.
222
+ * This means that the order is not stable between environments and sets of content files.
223
+ *
224
+ * In order to make the order stable, we need to remap the arbitrary variant offsets to
225
+ * be in alphabetical order starting from the offset of the first arbitrary variant.
226
+ */ recalculateVariantOffsets() {
227
+ // Sort the variants by their name
228
+ let variants = Array.from(this.variantOffsets.entries()).filter(([v])=>v.startsWith("[")).sort(([a], [z])=>fastCompare(a, z));
229
+ // Sort the list of offsets
230
+ // This is not necessarily a discrete range of numbers which is why
231
+ // we're using sort instead of creating a range from min/max
232
+ let newOffsets = variants.map(([, offset])=>offset).sort((a, z)=>(0, _bigSign.default)(a - z));
233
+ // Create a map from the old offsets to the new offsets in the new sort order
234
+ /** @type {[bigint, bigint][]} */ let mapping = variants.map(([, oldOffset], i)=>[
116
235
  oldOffset,
117
236
  newOffsets[i]
118
- ]).filter(([a, z])=>a !== z);
237
+ ]);
238
+ // Remove any variants that will not move letting us skip
239
+ // remapping if everything happens to be in order
240
+ return mapping.filter(([a, z])=>a !== z);
119
241
  }
120
- remapArbitraryVariantOffsets(list) {
242
+ /**
243
+ * @template T
244
+ * @param {[RuleOffset, T][]} list
245
+ * @returns {[RuleOffset, T][]}
246
+ */ remapArbitraryVariantOffsets(list) {
121
247
  let mapping = this.recalculateVariantOffsets();
122
- return 0 === mapping.length ? list : list.map((item)=>{
248
+ // No arbitrary variants? Nothing to do.
249
+ // Everyhing already in order? Nothing to do.
250
+ if (mapping.length === 0) {
251
+ return list;
252
+ }
253
+ // Remap every variant offset in the list
254
+ return list.map((item)=>{
123
255
  let [offset, rule] = item;
256
+ offset = {
257
+ ...offset,
258
+ variants: (0, _remapbitfield.remapBitfield)(offset.variants, mapping)
259
+ };
124
260
  return [
125
- offset = {
126
- ...offset,
127
- variants: (0, _remapBitfieldJs.remapBitfield)(offset.variants, mapping)
128
- },
261
+ offset,
129
262
  rule
130
263
  ];
131
264
  });
132
265
  }
133
- sort(list) {
134
- return (list = this.remapArbitraryVariantOffsets(list)).sort(([a], [b])=>(0, _bigSign.default)(this.compare(a, b)));
266
+ /**
267
+ * @template T
268
+ * @param {[RuleOffset, T][]} list
269
+ * @returns {[RuleOffset, T][]}
270
+ */ sort(list) {
271
+ list = this.remapArbitraryVariantOffsets(list);
272
+ return list.sort(([a], [b])=>(0, _bigSign.default)(this.compare(a, b)));
135
273
  }
136
274
  }
137
- function max(nums) {
275
+ /**
276
+ *
277
+ * @param {bigint[]} nums
278
+ * @returns {bigint|null}
279
+ */ function max(nums) {
138
280
  let max = null;
139
- for (let num of nums)max = (max = null != max ? max : num) > num ? max : num;
281
+ for (const num of nums){
282
+ max = max !== null && max !== void 0 ? max : num;
283
+ max = max > num ? max : num;
284
+ }
140
285
  return max;
141
286
  }
287
+ /**
288
+ * A fast ASCII order string comparison function.
289
+ *
290
+ * Using `.sort()` without a custom compare function is faster
291
+ * But you can only use that if you're sorting an array of
292
+ * only strings. If you're sorting strings inside objects
293
+ * or arrays, you need must use a custom compare function.
294
+ *
295
+ * @param {string} a
296
+ * @param {string} b
297
+ */ function fastCompare(a, b) {
298
+ let aLen = a.length;
299
+ let bLen = b.length;
300
+ let minLen = aLen < bLen ? aLen : bLen;
301
+ for(let i = 0; i < minLen; i++){
302
+ let cmp = a.charCodeAt(i) - b.charCodeAt(i);
303
+ if (cmp !== 0) return cmp;
304
+ }
305
+ return aLen - bLen;
306
+ }