@bamboocss/config 1.11.3 → 1.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.
@@ -1,270 +0,0 @@
1
- import { logger } from "@bamboocss/logger";
2
- import { BAMBOO_CONFIG_NAME, assign, isObject, isString, mergeAndConcat, mergeWith, walkObject } from "@bamboocss/shared";
3
- //#region src/merge-hooks.ts
4
- const mergeHooks = (plugins) => {
5
- const hooksFns = {};
6
- plugins.forEach(({ name, hooks }) => {
7
- Object.entries(hooks ?? {}).forEach(([key, value]) => {
8
- if (!hooksFns[key]) hooksFns[key] = [];
9
- hooksFns[key].push([name, value]);
10
- });
11
- });
12
- return Object.fromEntries(Object.entries(hooksFns).map(([key, entries]) => {
13
- const fns = entries.map(([name, fn]) => tryCatch(name, fn));
14
- const reducer = key in reducers ? reducers[key] : void 0;
15
- if (reducer) return [key, reducer(fns)];
16
- return [key, syncHooks.includes(key) ? callAll(...fns) : callAllAsync(...fns)];
17
- }));
18
- };
19
- const createReducer = (reducer) => reducer;
20
- const reducers = {
21
- "config:resolved": createReducer((fns) => async (_args) => {
22
- const args = Object.assign({}, _args);
23
- const original = _args.config;
24
- let config = args.config;
25
- for (const hookFn of fns) {
26
- const result = await hookFn(Object.assign(args, {
27
- config,
28
- original
29
- }));
30
- if (result !== void 0) config = result;
31
- }
32
- return config;
33
- }),
34
- "parser:before": createReducer((fns) => (_args) => {
35
- const args = Object.assign({}, _args);
36
- const original = _args.content;
37
- let content = args.content;
38
- for (const hookFn of fns) {
39
- const result = hookFn(Object.assign(args, {
40
- content,
41
- original
42
- }));
43
- if (result !== void 0) content = result;
44
- }
45
- return content;
46
- }),
47
- "parser:preprocess": createReducer((fns) => (_args) => {
48
- const args = Object.assign({}, _args);
49
- const original = _args.data;
50
- let data = args.data;
51
- for (const hookFn of fns) {
52
- const result = hookFn(Object.assign(args, {
53
- data,
54
- original
55
- }));
56
- if (result !== void 0) data = result;
57
- }
58
- return data;
59
- }),
60
- "cssgen:done": createReducer((fns) => (_args) => {
61
- const args = Object.assign({}, _args);
62
- const original = _args.content;
63
- let content = args.content;
64
- for (const hookFn of fns) {
65
- const result = hookFn(Object.assign(args, {
66
- content,
67
- original
68
- }));
69
- if (result !== void 0) content = result;
70
- }
71
- return content;
72
- }),
73
- "codegen:prepare": createReducer((fns) => async (_args) => {
74
- const args = Object.assign({}, _args);
75
- const original = _args.artifacts;
76
- let artifacts = args.artifacts;
77
- for (const hookFn of fns) {
78
- const result = await hookFn(Object.assign(args, {
79
- artifacts,
80
- original
81
- }));
82
- if (result) artifacts = result;
83
- }
84
- return artifacts;
85
- }),
86
- "preset:resolved": createReducer((fns) => async (_args) => {
87
- const args = Object.assign({}, _args);
88
- const original = _args.preset;
89
- let preset = args.preset;
90
- for (const hookFn of fns) {
91
- const result = await hookFn(Object.assign(args, {
92
- preset,
93
- original
94
- }));
95
- if (result !== void 0) preset = result;
96
- }
97
- return preset;
98
- }),
99
- "css:optimize": createReducer((fns) => (_args) => {
100
- const args = Object.assign({}, _args);
101
- const original = _args.css;
102
- let css = args.css;
103
- for (const hookFn of fns) {
104
- const result = hookFn(Object.assign(args, {
105
- css,
106
- original
107
- }));
108
- if (result !== void 0) css = result;
109
- }
110
- return css;
111
- })
112
- };
113
- const syncHooks = [
114
- "context:created",
115
- "parser:before",
116
- "parser:preprocess",
117
- "parser:after",
118
- "cssgen:done",
119
- "css:optimize"
120
- ];
121
- const callAllAsync = (...fns) => async (...a) => {
122
- for (const fn of fns) await fn?.(...a);
123
- };
124
- const callAll = (...fns) => (...a) => {
125
- fns.forEach((fn) => fn?.(...a));
126
- };
127
- const tryCatch = (name, fn) => {
128
- return (...args) => {
129
- try {
130
- return fn(...args);
131
- } catch (e) {
132
- logger.caughtError("hooks", `Error in plugin "${name}"`, e);
133
- }
134
- };
135
- };
136
- //#endregion
137
- //#region src/validation/utils.ts
138
- const REFERENCE_REGEX = /({([^}]*)})/g;
139
- const curlyBracketRegex = /[{}]/g;
140
- const isValidToken = (token) => isObject(token) && Object.hasOwnProperty.call(token, "value");
141
- const isTokenReference = (value) => typeof value === "string" && REFERENCE_REGEX.test(value);
142
- const formatPath = (path) => path;
143
- function getReferences(value) {
144
- if (typeof value !== "string") return [];
145
- const matches = value.match(REFERENCE_REGEX);
146
- if (!matches) return [];
147
- return matches.map((match) => match.replace(curlyBracketRegex, "")).map((value) => {
148
- return value.trim().split("/")[0];
149
- });
150
- }
151
- const serializeTokenValue = (value) => {
152
- if (isString(value)) return value;
153
- if (isObject(value)) return Object.values(value).map((v) => serializeTokenValue(v)).join(" ");
154
- if (Array.isArray(value)) return value.map((v) => serializeTokenValue(v)).join(" ");
155
- return value.toString();
156
- };
157
- //#endregion
158
- //#region src/merge-config.ts
159
- /**
160
- * Collect all `extend` properties into an array (to avoid mutation)
161
- */
162
- function getExtends(items) {
163
- return items.reduce((merged, { extend }) => {
164
- if (!extend) return merged;
165
- return mergeWith(merged, extend, (originalValue, newValue) => {
166
- if (newValue === void 0) return originalValue ?? [];
167
- if (originalValue === void 0) return [newValue];
168
- if (Array.isArray(originalValue)) return [newValue, ...originalValue];
169
- return [newValue, originalValue];
170
- });
171
- }, {});
172
- }
173
- /**
174
- * Separate the `extend` properties from the rest of the object
175
- */
176
- function mergeRecords(records) {
177
- return {
178
- ...records.reduce((acc, record) => assign(acc, record), {}),
179
- extend: getExtends(records)
180
- };
181
- }
182
- /**
183
- * Merge all `extend` properties into the rest of the object
184
- */
185
- function mergeExtensions(records) {
186
- const { extend = [], ...restProps } = mergeRecords(records);
187
- return mergeWith(restProps, extend, (obj, extensions) => {
188
- return mergeAndConcat({}, obj, ...extensions);
189
- });
190
- }
191
- const isEmptyObject = (obj) => typeof obj === "object" && Object.keys(obj).length === 0;
192
- const compact = (obj) => {
193
- return Object.keys(obj).reduce((acc, key) => {
194
- if (obj[key] !== void 0 && !isEmptyObject(obj[key])) acc[key] = obj[key];
195
- return acc;
196
- }, {});
197
- };
198
- const tokenKeys = [
199
- "description",
200
- "extensions",
201
- "type",
202
- "value",
203
- "deprecated"
204
- ];
205
- /**
206
- * Merge all configs into a single config
207
- */
208
- function mergeConfigs(configs) {
209
- const userConfig = configs.at(-1);
210
- const pluginHooks = userConfig.plugins ?? [];
211
- if (userConfig.hooks) pluginHooks.push({
212
- name: BAMBOO_CONFIG_NAME,
213
- hooks: userConfig.hooks
214
- });
215
- const reversed = Array.from(configs).reverse();
216
- const withoutEmpty = compact(assign({
217
- conditions: mergeExtensions(reversed.map((config) => config.conditions ?? {})),
218
- theme: mergeExtensions(reversed.map((config) => config.theme ?? {})),
219
- patterns: mergeExtensions(reversed.map((config) => config.patterns ?? {})),
220
- utilities: mergeExtensions(reversed.map((config) => config.utilities ?? {})),
221
- globalCss: mergeExtensions(reversed.map((config) => config.globalCss ?? {})),
222
- globalVars: mergeExtensions(reversed.map((config) => config.globalVars ?? {})),
223
- globalFontface: mergeExtensions(reversed.map((config) => config.globalFontface ?? {})),
224
- globalPositionTry: mergeExtensions(reversed.map((config) => config.globalPositionTry ?? {})),
225
- staticCss: mergeExtensions(reversed.map((config) => config.staticCss ?? {})),
226
- themes: mergeExtensions(reversed.map((config) => config.themes ?? {})),
227
- hooks: mergeHooks(pluginHooks)
228
- }, ...reversed));
229
- /**
230
- * Properly merge tokens between flat/nested forms by setting the flat form as the default
231
- * preset:
232
- * ```
233
- * tokens: {
234
- * black: {
235
- * value: "black"
236
- * }
237
- * }
238
- * // color: "black"
239
- * ```
240
- *
241
- * config:
242
- * ```
243
- * tokens: {
244
- * black: {
245
- * 0: { value: "black" },
246
- * 10: { value: "black/10" },
247
- * 20: { value: "black/20" },
248
- * // ...
249
- * }
250
- * }
251
- *
252
- * // color: "black.20"
253
- * ```
254
- */
255
- if (withoutEmpty.theme?.tokens) walkObject(withoutEmpty.theme.tokens, (args) => args, { stop(token) {
256
- if (!isValidToken(token)) return false;
257
- if (Object.keys(token).filter((k) => !tokenKeys.includes(k)).length > 0) {
258
- token.DEFAULT ||= {};
259
- tokenKeys.forEach((key) => {
260
- if (token[key] == null) return;
261
- token.DEFAULT[key] ||= token[key];
262
- delete token[key];
263
- });
264
- }
265
- return true;
266
- } });
267
- return withoutEmpty;
268
- }
269
- //#endregion
270
- export { isValidToken as a, isTokenReference as i, formatPath as n, serializeTokenValue as o, getReferences as r, mergeHooks as s, mergeConfigs as t };
@@ -1,16 +0,0 @@
1
- import { BambooPlugin, Config } from "@bamboocss/types";
2
-
3
- //#region src/merge-hooks.d.ts
4
- declare const mergeHooks: (plugins: BambooPlugin[]) => BambooHooks;
5
- //#endregion
6
- //#region src/merge-config.d.ts
7
- type Extendable<T> = T & {
8
- extend?: T;
9
- };
10
- type ExtendableConfig = Extendable<Config>;
11
- /**
12
- * Merge all configs into a single config
13
- */
14
- declare function mergeConfigs(configs: ExtendableConfig[]): any;
15
- //#endregion
16
- export { mergeHooks as n, mergeConfigs as t };
@@ -1,16 +0,0 @@
1
- import { BambooPlugin, Config } from "@bamboocss/types";
2
-
3
- //#region src/merge-hooks.d.ts
4
- declare const mergeHooks: (plugins: BambooPlugin[]) => BambooHooks;
5
- //#endregion
6
- //#region src/merge-config.d.ts
7
- type Extendable<T> = T & {
8
- extend?: T;
9
- };
10
- type ExtendableConfig = Extendable<Config>;
11
- /**
12
- * Merge all configs into a single config
13
- */
14
- declare function mergeConfigs(configs: ExtendableConfig[]): any;
15
- //#endregion
16
- export { mergeHooks as n, mergeConfigs as t };
@@ -1,11 +0,0 @@
1
- //#region src/ts-config-paths.d.ts
2
- interface PathMapping {
3
- pattern: RegExp;
4
- paths: string[];
5
- }
6
- /**
7
- * @see https://github.com/aleclarson/vite-tsconfig-paths/blob/e8f0acf7adfcfbf77edbe937f64b4e5d39557ad0/src/mappings.ts
8
- */
9
- declare function convertTsPathsToRegexes(paths: Record<string, string[]>, baseUrl: string): PathMapping[];
10
- //#endregion
11
- export { convertTsPathsToRegexes as n, PathMapping as t };
@@ -1,11 +0,0 @@
1
- //#region src/ts-config-paths.d.ts
2
- interface PathMapping {
3
- pattern: RegExp;
4
- paths: string[];
5
- }
6
- /**
7
- * @see https://github.com/aleclarson/vite-tsconfig-paths/blob/e8f0acf7adfcfbf77edbe937f64b4e5d39557ad0/src/mappings.ts
8
- */
9
- declare function convertTsPathsToRegexes(paths: Record<string, string[]>, baseUrl: string): PathMapping[];
10
- //#endregion
11
- export { convertTsPathsToRegexes as n, PathMapping as t };