@pandacss/config 0.0.2 → 0.3.1

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.
package/LICENSE.md CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Segun Adebayo
3
+ Copyright (c) 2023 Segun Adebayo
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.d.ts CHANGED
@@ -6,6 +6,29 @@ declare function findConfigFile({ cwd, file }: {
6
6
  file?: string;
7
7
  }): string | void;
8
8
 
9
+ type PathMapping = {
10
+ pattern: RegExp;
11
+ paths: string[];
12
+ };
13
+
14
+ type GetDepsOptions = {
15
+ filename: string;
16
+ ext: string;
17
+ cwd: string;
18
+ seen: Set<string>;
19
+ baseUrl: string | undefined;
20
+ pathMappings: PathMapping[];
21
+ foundModuleAliases: Map<string, string>;
22
+ };
23
+ type GetConfigDependenciesTsOptions = {
24
+ baseUrl?: string | undefined;
25
+ pathMappings: PathMapping[];
26
+ };
27
+ declare function getConfigDependencies(filePath: string, tsOptions?: GetConfigDependenciesTsOptions): {
28
+ deps: Set<string>;
29
+ aliases: Map<string, string>;
30
+ };
31
+
9
32
  type ConfigFileOptions = {
10
33
  cwd: string;
11
34
  file?: string;
@@ -31,4 +54,4 @@ declare function mergeConfigs(configs: ExtendableConfig[]): any;
31
54
  */
32
55
  declare function getResolvedConfig(config: ExtendableConfig, cwd: string): Promise<Config>;
33
56
 
34
- export { bundleConfigFile, findConfigFile, getResolvedConfig, loadConfigFile, mergeConfigs, resolveConfigFile };
57
+ export { GetConfigDependenciesTsOptions, GetDepsOptions, bundleConfigFile, findConfigFile, getConfigDependencies, getResolvedConfig, loadConfigFile, mergeConfigs, resolveConfigFile };
package/dist/index.js CHANGED
@@ -32,6 +32,7 @@ var src_exports = {};
32
32
  __export(src_exports, {
33
33
  bundleConfigFile: () => bundleConfigFile,
34
34
  findConfigFile: () => findConfigFile,
35
+ getConfigDependencies: () => getConfigDependencies,
35
36
  getResolvedConfig: () => getResolvedConfig,
36
37
  loadConfigFile: () => loadConfigFile,
37
38
  mergeConfigs: () => mergeConfigs,
@@ -53,20 +54,129 @@ function findConfigFile({ cwd, file }) {
53
54
  });
54
55
  }
55
56
 
57
+ // src/get-mod-deps.ts
58
+ var import_fs = __toESM(require("fs"));
59
+ var import_path2 = __toESM(require("path"));
60
+
61
+ // src/ts-config-paths-mappings.ts
62
+ var resolveTsPathPattern = (pathMappings, moduleSpecifier) => {
63
+ for (const mapping of pathMappings) {
64
+ const match = moduleSpecifier.match(mapping.pattern);
65
+ if (!match) {
66
+ continue;
67
+ }
68
+ for (const pathTemplate of mapping.paths) {
69
+ let starCount = 0;
70
+ const mappedId = pathTemplate.replace(/\*/g, () => {
71
+ const matchIndex = Math.min(++starCount, match.length - 1);
72
+ return match[matchIndex];
73
+ });
74
+ return mappedId;
75
+ }
76
+ }
77
+ };
78
+
79
+ // src/get-mod-deps.ts
80
+ var jsExtensions = [".js", ".cjs", ".mjs"];
81
+ var jsResolutionOrder = ["", ".js", ".cjs", ".mjs", ".ts", ".cts", ".mts", ".jsx", ".tsx"];
82
+ var tsResolutionOrder = ["", ".ts", ".cts", ".mts", ".tsx", ".js", ".cjs", ".mjs", ".jsx"];
83
+ function resolveWithExtension(file, extensions) {
84
+ for (const ext of extensions) {
85
+ const full = `${file}${ext}`;
86
+ if (import_fs.default.existsSync(full) && import_fs.default.statSync(full).isFile()) {
87
+ return full;
88
+ }
89
+ }
90
+ for (const ext of extensions) {
91
+ const full = `${file}/index${ext}`;
92
+ if (import_fs.default.existsSync(full)) {
93
+ return full;
94
+ }
95
+ }
96
+ return null;
97
+ }
98
+ var importRegex = /import[\s\S]*?['"](.{3,}?)['"]/gi;
99
+ var importFromRegex = /import[\s\S]*from[\s\S]*?['"](.{3,}?)['"]/gi;
100
+ var requireRegex = /require\(['"`](.+)['"`]\)/gi;
101
+ var exportRegex = /export[\s\S]*from[\s\S]*?['"](.{3,}?)['"]/gi;
102
+ function getDeps(opts, fromAlias) {
103
+ const { filename, seen } = opts;
104
+ const absoluteFile = resolveWithExtension(
105
+ import_path2.default.resolve(opts.cwd, filename),
106
+ jsExtensions.includes(opts.ext) ? jsResolutionOrder : tsResolutionOrder
107
+ );
108
+ if (absoluteFile === null)
109
+ return;
110
+ if (fromAlias) {
111
+ opts.foundModuleAliases.set(fromAlias, absoluteFile);
112
+ }
113
+ if (seen.has(absoluteFile))
114
+ return;
115
+ seen.add(absoluteFile);
116
+ const contents = import_fs.default.readFileSync(absoluteFile, "utf-8");
117
+ const fileDeps = [
118
+ ...contents.matchAll(importRegex),
119
+ ...contents.matchAll(importFromRegex),
120
+ ...contents.matchAll(requireRegex),
121
+ ...contents.matchAll(exportRegex)
122
+ ];
123
+ if (!fileDeps.length)
124
+ return;
125
+ const nextOpts = {
126
+ // Resolve new base for new imports/requires
127
+ cwd: import_path2.default.dirname(absoluteFile),
128
+ ext: import_path2.default.extname(absoluteFile),
129
+ seen,
130
+ baseUrl: opts.baseUrl,
131
+ pathMappings: opts.pathMappings,
132
+ foundModuleAliases: opts.foundModuleAliases
133
+ };
134
+ fileDeps.forEach((match) => {
135
+ const mod = match[1];
136
+ if (mod[0] === ".") {
137
+ getDeps(Object.assign({}, nextOpts, { filename: mod }));
138
+ return;
139
+ }
140
+ if (!opts.pathMappings)
141
+ return;
142
+ const filename2 = resolveTsPathPattern(opts.pathMappings, mod);
143
+ if (!filename2)
144
+ return;
145
+ getDeps(Object.assign({}, nextOpts, { filename: filename2 }), mod);
146
+ });
147
+ }
148
+ function getConfigDependencies(filePath, tsOptions = { pathMappings: [] }) {
149
+ if (filePath === null)
150
+ return { deps: /* @__PURE__ */ new Set(), aliases: /* @__PURE__ */ new Map() };
151
+ const foundModuleAliases = /* @__PURE__ */ new Map();
152
+ const deps = /* @__PURE__ */ new Set();
153
+ deps.add(filePath);
154
+ getDeps({
155
+ filename: filePath,
156
+ ext: import_path2.default.extname(filePath),
157
+ cwd: import_path2.default.dirname(filePath),
158
+ seen: deps,
159
+ baseUrl: tsOptions.baseUrl,
160
+ pathMappings: tsOptions.pathMappings ?? [],
161
+ foundModuleAliases
162
+ });
163
+ return { deps, aliases: foundModuleAliases };
164
+ }
165
+
56
166
  // src/load-config.ts
57
167
  var import_error = require("@pandacss/error");
58
168
  var import_logger = require("@pandacss/logger");
59
169
 
60
- // src/merge-config.ts
61
- var import_merge_anything = require("merge-anything");
62
-
63
170
  // src/bundle.ts
64
171
  var import_bundle_n_require = require("bundle-n-require");
65
- async function bundle(filePath, cwd) {
66
- const { mod: config, dependencies } = await (0, import_bundle_n_require.bundleNRequire)(filePath, { cwd });
67
- return { config, dependencies };
172
+ async function bundle(filepath, cwd) {
173
+ const { mod: config, dependencies } = await (0, import_bundle_n_require.bundleNRequire)(filepath, { cwd, interopDefault: true });
174
+ return { config: config?.default ?? config, dependencies };
68
175
  }
69
176
 
177
+ // src/merge-config.ts
178
+ var import_merge_anything = require("merge-anything");
179
+
70
180
  // src/utils.ts
71
181
  var isObject = (value) => {
72
182
  return Object.prototype.toString.call(value) === "[object Object]";
@@ -103,7 +213,12 @@ function assign(target, ...sources) {
103
213
  // src/merge-config.ts
104
214
  function getExtends(items) {
105
215
  return items.reduce((merged, { extend }) => {
216
+ if (!extend)
217
+ return merged;
106
218
  return mergeWith(merged, extend, (originalValue, newValue) => {
219
+ if (newValue === void 0) {
220
+ return originalValue ?? [];
221
+ }
107
222
  if (originalValue === void 0) {
108
223
  return [newValue];
109
224
  }
@@ -126,8 +241,17 @@ function mergeExtensions(records) {
126
241
  return (0, import_merge_anything.mergeAndConcat)({}, obj, ...extensions);
127
242
  });
128
243
  }
244
+ var isEmptyObject = (obj) => typeof obj === "object" && Object.keys(obj).length === 0;
245
+ var compact = (obj) => {
246
+ return Object.keys(obj).reduce((acc, key) => {
247
+ if (obj[key] !== void 0 && !isEmptyObject(obj[key])) {
248
+ acc[key] = obj[key];
249
+ }
250
+ return acc;
251
+ }, {});
252
+ };
129
253
  function mergeConfigs(configs2) {
130
- return assign(
254
+ const mergedResult = assign(
131
255
  {
132
256
  conditions: mergeExtensions(configs2.map((config) => config.conditions ?? {})),
133
257
  theme: mergeExtensions(configs2.map((config) => config.theme ?? {})),
@@ -137,6 +261,7 @@ function mergeConfigs(configs2) {
137
261
  },
138
262
  ...configs2
139
263
  );
264
+ return compact(mergedResult);
140
265
  }
141
266
  async function getResolvedConfig(config, cwd) {
142
267
  const presets = config.presets ?? [];
@@ -156,12 +281,34 @@ async function getResolvedConfig(config, cwd) {
156
281
  }
157
282
 
158
283
  // src/load-config.ts
284
+ var import_preset_base = require("@pandacss/preset-base");
285
+ var import_preset_panda = require("@pandacss/preset-panda");
286
+ var bundledPresets = {
287
+ "@pandacss/preset-base": import_preset_base.preset,
288
+ "@pandacss/preset-panda": import_preset_panda.preset,
289
+ "@pandacss/dev/presets": import_preset_panda.preset
290
+ };
291
+ var bundledPresetsNames = Object.keys(bundledPresets);
292
+ var isBundledPreset = (preset) => bundledPresetsNames.includes(preset);
159
293
  async function loadConfigFile(options) {
160
294
  const result = await bundleConfigFile(options);
161
295
  return resolveConfigFile(result, options.cwd);
162
296
  }
163
297
  async function resolveConfigFile(result, cwd) {
164
- result.config.presets ||= ["@pandacss/dev/presets"];
298
+ const presets = /* @__PURE__ */ new Set();
299
+ presets.add(import_preset_base.preset);
300
+ if (!result.config.presets) {
301
+ presets.add(import_preset_panda.preset);
302
+ } else {
303
+ result.config.presets.forEach((preset) => {
304
+ if (typeof preset === "string" && isBundledPreset(preset)) {
305
+ presets.add(bundledPresets[preset]);
306
+ } else {
307
+ presets.add(preset);
308
+ }
309
+ });
310
+ }
311
+ result.config.presets = Array.from(presets);
165
312
  const mergedConfig = await getResolvedConfig(result.config, cwd);
166
313
  return { ...result, config: mergedConfig };
167
314
  }
@@ -186,6 +333,7 @@ async function bundleConfigFile(options) {
186
333
  0 && (module.exports = {
187
334
  bundleConfigFile,
188
335
  findConfigFile,
336
+ getConfigDependencies,
189
337
  getResolvedConfig,
190
338
  loadConfigFile,
191
339
  mergeConfigs,
package/dist/index.mjs CHANGED
@@ -12,20 +12,129 @@ function findConfigFile({ cwd, file }) {
12
12
  });
13
13
  }
14
14
 
15
+ // src/get-mod-deps.ts
16
+ import fs from "fs";
17
+ import path from "path";
18
+
19
+ // src/ts-config-paths-mappings.ts
20
+ var resolveTsPathPattern = (pathMappings, moduleSpecifier) => {
21
+ for (const mapping of pathMappings) {
22
+ const match = moduleSpecifier.match(mapping.pattern);
23
+ if (!match) {
24
+ continue;
25
+ }
26
+ for (const pathTemplate of mapping.paths) {
27
+ let starCount = 0;
28
+ const mappedId = pathTemplate.replace(/\*/g, () => {
29
+ const matchIndex = Math.min(++starCount, match.length - 1);
30
+ return match[matchIndex];
31
+ });
32
+ return mappedId;
33
+ }
34
+ }
35
+ };
36
+
37
+ // src/get-mod-deps.ts
38
+ var jsExtensions = [".js", ".cjs", ".mjs"];
39
+ var jsResolutionOrder = ["", ".js", ".cjs", ".mjs", ".ts", ".cts", ".mts", ".jsx", ".tsx"];
40
+ var tsResolutionOrder = ["", ".ts", ".cts", ".mts", ".tsx", ".js", ".cjs", ".mjs", ".jsx"];
41
+ function resolveWithExtension(file, extensions) {
42
+ for (const ext of extensions) {
43
+ const full = `${file}${ext}`;
44
+ if (fs.existsSync(full) && fs.statSync(full).isFile()) {
45
+ return full;
46
+ }
47
+ }
48
+ for (const ext of extensions) {
49
+ const full = `${file}/index${ext}`;
50
+ if (fs.existsSync(full)) {
51
+ return full;
52
+ }
53
+ }
54
+ return null;
55
+ }
56
+ var importRegex = /import[\s\S]*?['"](.{3,}?)['"]/gi;
57
+ var importFromRegex = /import[\s\S]*from[\s\S]*?['"](.{3,}?)['"]/gi;
58
+ var requireRegex = /require\(['"`](.+)['"`]\)/gi;
59
+ var exportRegex = /export[\s\S]*from[\s\S]*?['"](.{3,}?)['"]/gi;
60
+ function getDeps(opts, fromAlias) {
61
+ const { filename, seen } = opts;
62
+ const absoluteFile = resolveWithExtension(
63
+ path.resolve(opts.cwd, filename),
64
+ jsExtensions.includes(opts.ext) ? jsResolutionOrder : tsResolutionOrder
65
+ );
66
+ if (absoluteFile === null)
67
+ return;
68
+ if (fromAlias) {
69
+ opts.foundModuleAliases.set(fromAlias, absoluteFile);
70
+ }
71
+ if (seen.has(absoluteFile))
72
+ return;
73
+ seen.add(absoluteFile);
74
+ const contents = fs.readFileSync(absoluteFile, "utf-8");
75
+ const fileDeps = [
76
+ ...contents.matchAll(importRegex),
77
+ ...contents.matchAll(importFromRegex),
78
+ ...contents.matchAll(requireRegex),
79
+ ...contents.matchAll(exportRegex)
80
+ ];
81
+ if (!fileDeps.length)
82
+ return;
83
+ const nextOpts = {
84
+ // Resolve new base for new imports/requires
85
+ cwd: path.dirname(absoluteFile),
86
+ ext: path.extname(absoluteFile),
87
+ seen,
88
+ baseUrl: opts.baseUrl,
89
+ pathMappings: opts.pathMappings,
90
+ foundModuleAliases: opts.foundModuleAliases
91
+ };
92
+ fileDeps.forEach((match) => {
93
+ const mod = match[1];
94
+ if (mod[0] === ".") {
95
+ getDeps(Object.assign({}, nextOpts, { filename: mod }));
96
+ return;
97
+ }
98
+ if (!opts.pathMappings)
99
+ return;
100
+ const filename2 = resolveTsPathPattern(opts.pathMappings, mod);
101
+ if (!filename2)
102
+ return;
103
+ getDeps(Object.assign({}, nextOpts, { filename: filename2 }), mod);
104
+ });
105
+ }
106
+ function getConfigDependencies(filePath, tsOptions = { pathMappings: [] }) {
107
+ if (filePath === null)
108
+ return { deps: /* @__PURE__ */ new Set(), aliases: /* @__PURE__ */ new Map() };
109
+ const foundModuleAliases = /* @__PURE__ */ new Map();
110
+ const deps = /* @__PURE__ */ new Set();
111
+ deps.add(filePath);
112
+ getDeps({
113
+ filename: filePath,
114
+ ext: path.extname(filePath),
115
+ cwd: path.dirname(filePath),
116
+ seen: deps,
117
+ baseUrl: tsOptions.baseUrl,
118
+ pathMappings: tsOptions.pathMappings ?? [],
119
+ foundModuleAliases
120
+ });
121
+ return { deps, aliases: foundModuleAliases };
122
+ }
123
+
15
124
  // src/load-config.ts
16
125
  import { ConfigError, ConfigNotFoundError } from "@pandacss/error";
17
126
  import { logger } from "@pandacss/logger";
18
127
 
19
- // src/merge-config.ts
20
- import { mergeAndConcat } from "merge-anything";
21
-
22
128
  // src/bundle.ts
23
129
  import { bundleNRequire } from "bundle-n-require";
24
- async function bundle(filePath, cwd) {
25
- const { mod: config, dependencies } = await bundleNRequire(filePath, { cwd });
26
- return { config, dependencies };
130
+ async function bundle(filepath, cwd) {
131
+ const { mod: config, dependencies } = await bundleNRequire(filepath, { cwd, interopDefault: true });
132
+ return { config: config?.default ?? config, dependencies };
27
133
  }
28
134
 
135
+ // src/merge-config.ts
136
+ import { mergeAndConcat } from "merge-anything";
137
+
29
138
  // src/utils.ts
30
139
  var isObject = (value) => {
31
140
  return Object.prototype.toString.call(value) === "[object Object]";
@@ -62,7 +171,12 @@ function assign(target, ...sources) {
62
171
  // src/merge-config.ts
63
172
  function getExtends(items) {
64
173
  return items.reduce((merged, { extend }) => {
174
+ if (!extend)
175
+ return merged;
65
176
  return mergeWith(merged, extend, (originalValue, newValue) => {
177
+ if (newValue === void 0) {
178
+ return originalValue ?? [];
179
+ }
66
180
  if (originalValue === void 0) {
67
181
  return [newValue];
68
182
  }
@@ -85,8 +199,17 @@ function mergeExtensions(records) {
85
199
  return mergeAndConcat({}, obj, ...extensions);
86
200
  });
87
201
  }
202
+ var isEmptyObject = (obj) => typeof obj === "object" && Object.keys(obj).length === 0;
203
+ var compact = (obj) => {
204
+ return Object.keys(obj).reduce((acc, key) => {
205
+ if (obj[key] !== void 0 && !isEmptyObject(obj[key])) {
206
+ acc[key] = obj[key];
207
+ }
208
+ return acc;
209
+ }, {});
210
+ };
88
211
  function mergeConfigs(configs2) {
89
- return assign(
212
+ const mergedResult = assign(
90
213
  {
91
214
  conditions: mergeExtensions(configs2.map((config) => config.conditions ?? {})),
92
215
  theme: mergeExtensions(configs2.map((config) => config.theme ?? {})),
@@ -96,6 +219,7 @@ function mergeConfigs(configs2) {
96
219
  },
97
220
  ...configs2
98
221
  );
222
+ return compact(mergedResult);
99
223
  }
100
224
  async function getResolvedConfig(config, cwd) {
101
225
  const presets = config.presets ?? [];
@@ -115,12 +239,34 @@ async function getResolvedConfig(config, cwd) {
115
239
  }
116
240
 
117
241
  // src/load-config.ts
242
+ import { preset as presetBase } from "@pandacss/preset-base";
243
+ import { preset as presetPanda } from "@pandacss/preset-panda";
244
+ var bundledPresets = {
245
+ "@pandacss/preset-base": presetBase,
246
+ "@pandacss/preset-panda": presetPanda,
247
+ "@pandacss/dev/presets": presetPanda
248
+ };
249
+ var bundledPresetsNames = Object.keys(bundledPresets);
250
+ var isBundledPreset = (preset) => bundledPresetsNames.includes(preset);
118
251
  async function loadConfigFile(options) {
119
252
  const result = await bundleConfigFile(options);
120
253
  return resolveConfigFile(result, options.cwd);
121
254
  }
122
255
  async function resolveConfigFile(result, cwd) {
123
- result.config.presets ||= ["@pandacss/dev/presets"];
256
+ const presets = /* @__PURE__ */ new Set();
257
+ presets.add(presetBase);
258
+ if (!result.config.presets) {
259
+ presets.add(presetPanda);
260
+ } else {
261
+ result.config.presets.forEach((preset) => {
262
+ if (typeof preset === "string" && isBundledPreset(preset)) {
263
+ presets.add(bundledPresets[preset]);
264
+ } else {
265
+ presets.add(preset);
266
+ }
267
+ });
268
+ }
269
+ result.config.presets = Array.from(presets);
124
270
  const mergedConfig = await getResolvedConfig(result.config, cwd);
125
271
  return { ...result, config: mergedConfig };
126
272
  }
@@ -144,6 +290,7 @@ async function bundleConfigFile(options) {
144
290
  export {
145
291
  bundleConfigFile,
146
292
  findConfigFile,
293
+ getConfigDependencies,
147
294
  getResolvedConfig,
148
295
  loadConfigFile,
149
296
  mergeConfigs,