You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
9.1 KiB

2 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "default", {
  6. enumerable: true,
  7. get: ()=>resolveConfig
  8. });
  9. const _negateValue = /*#__PURE__*/ _interopRequireDefault(require("./negateValue"));
  10. const _corePluginList = /*#__PURE__*/ _interopRequireDefault(require("../corePluginList"));
  11. const _configurePlugins = /*#__PURE__*/ _interopRequireDefault(require("./configurePlugins"));
  12. const _defaultConfigStub = /*#__PURE__*/ _interopRequireDefault(require("../../stubs/defaultConfig.stub"));
  13. const _colors = /*#__PURE__*/ _interopRequireDefault(require("../public/colors"));
  14. const _defaults = require("./defaults");
  15. const _toPath = require("./toPath");
  16. const _normalizeConfig = require("./normalizeConfig");
  17. const _isPlainObject = /*#__PURE__*/ _interopRequireDefault(require("./isPlainObject"));
  18. const _cloneDeep = require("./cloneDeep");
  19. const _pluginUtils = require("./pluginUtils");
  20. const _withAlphaVariable = require("./withAlphaVariable");
  21. const _toColorValue = /*#__PURE__*/ _interopRequireDefault(require("./toColorValue"));
  22. function _interopRequireDefault(obj) {
  23. return obj && obj.__esModule ? obj : {
  24. default: obj
  25. };
  26. }
  27. function isFunction(input) {
  28. return typeof input === "function";
  29. }
  30. function isObject(input) {
  31. return typeof input === "object" && input !== null;
  32. }
  33. function mergeWith(target, ...sources) {
  34. let customizer = sources.pop();
  35. for (let source of sources){
  36. for(let k in source){
  37. let merged = customizer(target[k], source[k]);
  38. if (merged === undefined) {
  39. if (isObject(target[k]) && isObject(source[k])) {
  40. target[k] = mergeWith(target[k], source[k], customizer);
  41. } else {
  42. target[k] = source[k];
  43. }
  44. } else {
  45. target[k] = merged;
  46. }
  47. }
  48. }
  49. return target;
  50. }
  51. const configUtils = {
  52. colors: _colors.default,
  53. negative (scale) {
  54. // TODO: Log that this function isn't really needed anymore?
  55. return Object.keys(scale).filter((key)=>scale[key] !== "0").reduce((negativeScale, key)=>{
  56. let negativeValue = (0, _negateValue.default)(scale[key]);
  57. if (negativeValue !== undefined) {
  58. negativeScale[`-${key}`] = negativeValue;
  59. }
  60. return negativeScale;
  61. }, {});
  62. },
  63. breakpoints (screens) {
  64. return Object.keys(screens).filter((key)=>typeof screens[key] === "string").reduce((breakpoints, key)=>({
  65. ...breakpoints,
  66. [`screen-${key}`]: screens[key]
  67. }), {});
  68. }
  69. };
  70. function value(valueToResolve, ...args) {
  71. return isFunction(valueToResolve) ? valueToResolve(...args) : valueToResolve;
  72. }
  73. function collectExtends(items) {
  74. return items.reduce((merged, { extend })=>{
  75. return mergeWith(merged, extend, (mergedValue, extendValue)=>{
  76. if (mergedValue === undefined) {
  77. return [
  78. extendValue
  79. ];
  80. }
  81. if (Array.isArray(mergedValue)) {
  82. return [
  83. extendValue,
  84. ...mergedValue
  85. ];
  86. }
  87. return [
  88. extendValue,
  89. mergedValue
  90. ];
  91. });
  92. }, {});
  93. }
  94. function mergeThemes(themes) {
  95. return {
  96. ...themes.reduce((merged, theme)=>(0, _defaults.defaults)(merged, theme), {}),
  97. // In order to resolve n config objects, we combine all of their `extend` properties
  98. // into arrays instead of objects so they aren't overridden.
  99. extend: collectExtends(themes)
  100. };
  101. }
  102. function mergeExtensionCustomizer(merged, value) {
  103. // When we have an array of objects, we do want to merge it
  104. if (Array.isArray(merged) && isObject(merged[0])) {
  105. return merged.concat(value);
  106. }
  107. // When the incoming value is an array, and the existing config is an object, prepend the existing object
  108. if (Array.isArray(value) && isObject(value[0]) && isObject(merged)) {
  109. return [
  110. merged,
  111. ...value
  112. ];
  113. }
  114. // Override arrays (for example for font-families, box-shadows, ...)
  115. if (Array.isArray(value)) {
  116. return value;
  117. }
  118. // Execute default behaviour
  119. return undefined;
  120. }
  121. function mergeExtensions({ extend , ...theme }) {
  122. return mergeWith(theme, extend, (themeValue, extensions)=>{
  123. // The `extend` property is an array, so we need to check if it contains any functions
  124. if (!isFunction(themeValue) && !extensions.some(isFunction)) {
  125. return mergeWith({}, themeValue, ...extensions, mergeExtensionCustomizer);
  126. }
  127. return (resolveThemePath, utils)=>mergeWith({}, ...[
  128. themeValue,
  129. ...extensions
  130. ].map((e)=>value(e, resolveThemePath, utils)), mergeExtensionCustomizer);
  131. });
  132. }
  133. /**
  134. *
  135. * @param {string} key
  136. * @return {Iterable<string[] & {alpha: string | undefined}>}
  137. */ function* toPaths(key) {
  138. let path = (0, _toPath.toPath)(key);
  139. if (path.length === 0) {
  140. return;
  141. }
  142. yield path;
  143. if (Array.isArray(key)) {
  144. return;
  145. }
  146. let pattern = /^(.*?)\s*\/\s*([^/]+)$/;
  147. let matches = key.match(pattern);
  148. if (matches !== null) {
  149. let [, prefix, alpha] = matches;
  150. let newPath = (0, _toPath.toPath)(prefix);
  151. newPath.alpha = alpha;
  152. yield newPath;
  153. }
  154. }
  155. function resolveFunctionKeys(object) {
  156. // theme('colors.red.500 / 0.5') -> ['colors', 'red', '500 / 0', '5]
  157. const resolvePath = (key, defaultValue)=>{
  158. for (const path of toPaths(key)){
  159. let index = 0;
  160. let val = object;
  161. while(val !== undefined && val !== null && index < path.length){
  162. val = val[path[index++]];
  163. let shouldResolveAsFn = isFunction(val) && (path.alpha === undefined || index <= path.length - 1);
  164. val = shouldResolveAsFn ? val(resolvePath, configUtils) : val;
  165. }
  166. if (val !== undefined) {
  167. if (path.alpha !== undefined) {
  168. let normalized = (0, _pluginUtils.parseColorFormat)(val);
  169. return (0, _withAlphaVariable.withAlphaValue)(normalized, path.alpha, (0, _toColorValue.default)(normalized));
  170. }
  171. if ((0, _isPlainObject.default)(val)) {
  172. return (0, _cloneDeep.cloneDeep)(val);
  173. }
  174. return val;
  175. }
  176. }
  177. return defaultValue;
  178. };
  179. Object.assign(resolvePath, {
  180. theme: resolvePath,
  181. ...configUtils
  182. });
  183. return Object.keys(object).reduce((resolved, key)=>{
  184. resolved[key] = isFunction(object[key]) ? object[key](resolvePath, configUtils) : object[key];
  185. return resolved;
  186. }, {});
  187. }
  188. function extractPluginConfigs(configs) {
  189. let allConfigs = [];
  190. configs.forEach((config)=>{
  191. allConfigs = [
  192. ...allConfigs,
  193. config
  194. ];
  195. var ref;
  196. const plugins = (ref = config === null || config === void 0 ? void 0 : config.plugins) !== null && ref !== void 0 ? ref : [];
  197. if (plugins.length === 0) {
  198. return;
  199. }
  200. plugins.forEach((plugin)=>{
  201. if (plugin.__isOptionsFunction) {
  202. plugin = plugin();
  203. }
  204. var ref;
  205. allConfigs = [
  206. ...allConfigs,
  207. ...extractPluginConfigs([
  208. (ref = plugin === null || plugin === void 0 ? void 0 : plugin.config) !== null && ref !== void 0 ? ref : {}
  209. ])
  210. ];
  211. });
  212. });
  213. return allConfigs;
  214. }
  215. function resolveCorePlugins(corePluginConfigs) {
  216. const result = [
  217. ...corePluginConfigs
  218. ].reduceRight((resolved, corePluginConfig)=>{
  219. if (isFunction(corePluginConfig)) {
  220. return corePluginConfig({
  221. corePlugins: resolved
  222. });
  223. }
  224. return (0, _configurePlugins.default)(corePluginConfig, resolved);
  225. }, _corePluginList.default);
  226. return result;
  227. }
  228. function resolvePluginLists(pluginLists) {
  229. const result = [
  230. ...pluginLists
  231. ].reduceRight((resolved, pluginList)=>{
  232. return [
  233. ...resolved,
  234. ...pluginList
  235. ];
  236. }, []);
  237. return result;
  238. }
  239. function resolveConfig(configs) {
  240. let allConfigs = [
  241. ...extractPluginConfigs(configs),
  242. {
  243. prefix: "",
  244. important: false,
  245. separator: ":",
  246. variantOrder: _defaultConfigStub.default.variantOrder
  247. },
  248. ];
  249. var ref, ref1;
  250. return (0, _normalizeConfig.normalizeConfig)((0, _defaults.defaults)({
  251. theme: resolveFunctionKeys(mergeExtensions(mergeThemes(allConfigs.map((t)=>{
  252. return (ref = t === null || t === void 0 ? void 0 : t.theme) !== null && ref !== void 0 ? ref : {};
  253. })))),
  254. corePlugins: resolveCorePlugins(allConfigs.map((c)=>c.corePlugins)),
  255. plugins: resolvePluginLists(configs.map((c)=>{
  256. return (ref1 = c === null || c === void 0 ? void 0 : c.plugins) !== null && ref1 !== void 0 ? ref1 : [];
  257. }))
  258. }, ...allConfigs));
  259. }