tailwindcss-config 1.1.6-next.0 → 2.0.0-next.2
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/dist/index.cjs +339 -2
- package/dist/index.js +379 -0
- package/package.json +10 -8
- package/dist/index.mjs +0 -37
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
6
6
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
8
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
|
|
9
10
|
var __copyProps = (to, from, except, desc) => {
|
|
10
11
|
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
12
|
key = keys[i];
|
|
@@ -26,16 +27,352 @@ node_path = __toESM(node_path, 1);
|
|
|
26
27
|
let node_process = require("node:process");
|
|
27
28
|
node_process = __toESM(node_process, 1);
|
|
28
29
|
let jiti = require("jiti");
|
|
29
|
-
let lilconfig = require("lilconfig");
|
|
30
30
|
let _weapp_tailwindcss_shared = require("@weapp-tailwindcss/shared");
|
|
31
|
+
//#endregion
|
|
31
32
|
//#region src/index.ts
|
|
33
|
+
var import_src = (/* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
34
|
+
const path$1 = require("path");
|
|
35
|
+
const fs = require("fs");
|
|
36
|
+
const os = require("os");
|
|
37
|
+
const url = require("url");
|
|
38
|
+
const fsReadFileAsync = fs.promises.readFile;
|
|
39
|
+
/** @type {(name: string, sync: boolean) => string[]} */
|
|
40
|
+
function getDefaultSearchPlaces(name, sync) {
|
|
41
|
+
return [
|
|
42
|
+
"package.json",
|
|
43
|
+
`.${name}rc.json`,
|
|
44
|
+
`.${name}rc.js`,
|
|
45
|
+
`.${name}rc.cjs`,
|
|
46
|
+
...sync ? [] : [`.${name}rc.mjs`],
|
|
47
|
+
`.config/${name}rc`,
|
|
48
|
+
`.config/${name}rc.json`,
|
|
49
|
+
`.config/${name}rc.js`,
|
|
50
|
+
`.config/${name}rc.cjs`,
|
|
51
|
+
...sync ? [] : [`.config/${name}rc.mjs`],
|
|
52
|
+
`${name}.config.js`,
|
|
53
|
+
`${name}.config.cjs`,
|
|
54
|
+
...sync ? [] : [`${name}.config.mjs`]
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @type {(p: string) => string}
|
|
59
|
+
*
|
|
60
|
+
* see #17
|
|
61
|
+
* On *nix, if cwd is not under homedir,
|
|
62
|
+
* the last path will be '', ('/build' -> '')
|
|
63
|
+
* but it should be '/' actually.
|
|
64
|
+
* And on Windows, this will never happen. ('C:\build' -> 'C:')
|
|
65
|
+
*/
|
|
66
|
+
function parentDir(p) {
|
|
67
|
+
return path$1.dirname(p) || path$1.sep;
|
|
68
|
+
}
|
|
69
|
+
/** @type {import('./index').LoaderSync} */
|
|
70
|
+
const jsonLoader = (_, content) => JSON.parse(content);
|
|
71
|
+
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
|
|
72
|
+
/** @type {import('./index').LoadersSync} */
|
|
73
|
+
const defaultLoadersSync = Object.freeze({
|
|
74
|
+
".js": requireFunc,
|
|
75
|
+
".json": requireFunc,
|
|
76
|
+
".cjs": requireFunc,
|
|
77
|
+
noExt: jsonLoader
|
|
78
|
+
});
|
|
79
|
+
module.exports.defaultLoadersSync = defaultLoadersSync;
|
|
80
|
+
/** @type {import('./index').Loader} */
|
|
81
|
+
const dynamicImport = async (id) => {
|
|
82
|
+
try {
|
|
83
|
+
return (await import(
|
|
84
|
+
/* webpackIgnore: true */
|
|
85
|
+
url.pathToFileURL(id).href
|
|
86
|
+
)).default;
|
|
87
|
+
} catch (e) {
|
|
88
|
+
try {
|
|
89
|
+
return requireFunc(id);
|
|
90
|
+
} catch (requireE) {
|
|
91
|
+
if (requireE.code === "ERR_REQUIRE_ESM" || requireE instanceof SyntaxError && requireE.toString().includes("Cannot use import statement outside a module")) throw e;
|
|
92
|
+
throw requireE;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
/** @type {import('./index').Loaders} */
|
|
97
|
+
const defaultLoaders = Object.freeze({
|
|
98
|
+
".js": dynamicImport,
|
|
99
|
+
".mjs": dynamicImport,
|
|
100
|
+
".cjs": dynamicImport,
|
|
101
|
+
".json": jsonLoader,
|
|
102
|
+
noExt: jsonLoader
|
|
103
|
+
});
|
|
104
|
+
module.exports.defaultLoaders = defaultLoaders;
|
|
105
|
+
/**
|
|
106
|
+
* @param {string} name
|
|
107
|
+
* @param {import('./index').Options | import('./index').OptionsSync} options
|
|
108
|
+
* @param {boolean} sync
|
|
109
|
+
* @returns {Required<import('./index').Options | import('./index').OptionsSync>}
|
|
110
|
+
*/
|
|
111
|
+
function getOptions(name, options, sync) {
|
|
112
|
+
/** @type {Required<import('./index').Options>} */
|
|
113
|
+
const conf = {
|
|
114
|
+
stopDir: os.homedir(),
|
|
115
|
+
searchPlaces: getDefaultSearchPlaces(name, sync),
|
|
116
|
+
ignoreEmptySearchPlaces: true,
|
|
117
|
+
cache: true,
|
|
118
|
+
transform: (x) => x,
|
|
119
|
+
packageProp: [name],
|
|
120
|
+
...options,
|
|
121
|
+
loaders: {
|
|
122
|
+
...sync ? defaultLoadersSync : defaultLoaders,
|
|
123
|
+
...options.loaders
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
conf.searchPlaces.forEach((place) => {
|
|
127
|
+
const key = path$1.extname(place) || "noExt";
|
|
128
|
+
const loader = conf.loaders[key];
|
|
129
|
+
if (!loader) throw new Error(`Missing loader for extension "${place}"`);
|
|
130
|
+
if (typeof loader !== "function") throw new Error(`Loader for extension "${place}" is not a function: Received ${typeof loader}.`);
|
|
131
|
+
});
|
|
132
|
+
return conf;
|
|
133
|
+
}
|
|
134
|
+
/** @type {(props: string | string[], obj: Record<string, any>) => unknown} */
|
|
135
|
+
function getPackageProp(props, obj) {
|
|
136
|
+
if (typeof props === "string" && props in obj) return obj[props];
|
|
137
|
+
return (Array.isArray(props) ? props : props.split(".")).reduce((acc, prop) => acc === void 0 ? acc : acc[prop], obj) || null;
|
|
138
|
+
}
|
|
139
|
+
/** @param {string} filepath */
|
|
140
|
+
function validateFilePath(filepath) {
|
|
141
|
+
if (!filepath) throw new Error("load must pass a non-empty string");
|
|
142
|
+
}
|
|
143
|
+
/** @type {(loader: import('./index').Loader, ext: string) => void} */
|
|
144
|
+
function validateLoader(loader, ext) {
|
|
145
|
+
if (!loader) throw new Error(`No loader specified for extension "${ext}"`);
|
|
146
|
+
if (typeof loader !== "function") throw new Error("loader is not a function");
|
|
147
|
+
}
|
|
148
|
+
/** @type {(enableCache: boolean) => <T>(c: Map<string, T>, filepath: string, res: T) => T} */
|
|
149
|
+
const makeEmplace = (enableCache) => (c, filepath, res) => {
|
|
150
|
+
if (enableCache) c.set(filepath, res);
|
|
151
|
+
return res;
|
|
152
|
+
};
|
|
153
|
+
/** @type {import('./index').lilconfig} */
|
|
154
|
+
module.exports.lilconfig = function lilconfig(name, options) {
|
|
155
|
+
const { ignoreEmptySearchPlaces, loaders, packageProp, searchPlaces, stopDir, transform, cache } = getOptions(name, options ?? {}, false);
|
|
156
|
+
const searchCache = /* @__PURE__ */ new Map();
|
|
157
|
+
const loadCache = /* @__PURE__ */ new Map();
|
|
158
|
+
const emplace = makeEmplace(cache);
|
|
159
|
+
return {
|
|
160
|
+
async search(searchFrom = process.cwd()) {
|
|
161
|
+
/** @type {import('./index').LilconfigResult} */
|
|
162
|
+
const result = {
|
|
163
|
+
config: null,
|
|
164
|
+
filepath: ""
|
|
165
|
+
};
|
|
166
|
+
/** @type {Set<string>} */
|
|
167
|
+
const visited = /* @__PURE__ */ new Set();
|
|
168
|
+
let dir = searchFrom;
|
|
169
|
+
dirLoop: while (true) {
|
|
170
|
+
if (cache) {
|
|
171
|
+
const r = searchCache.get(dir);
|
|
172
|
+
if (r !== void 0) {
|
|
173
|
+
for (const p of visited) searchCache.set(p, r);
|
|
174
|
+
return r;
|
|
175
|
+
}
|
|
176
|
+
visited.add(dir);
|
|
177
|
+
}
|
|
178
|
+
for (const searchPlace of searchPlaces) {
|
|
179
|
+
const filepath = path$1.join(dir, searchPlace);
|
|
180
|
+
try {
|
|
181
|
+
await fs.promises.access(filepath);
|
|
182
|
+
} catch {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
const content = String(await fsReadFileAsync(filepath));
|
|
186
|
+
const loaderKey = path$1.extname(searchPlace) || "noExt";
|
|
187
|
+
const loader = loaders[loaderKey];
|
|
188
|
+
if (searchPlace === "package.json") {
|
|
189
|
+
const maybeConfig = getPackageProp(packageProp, await loader(filepath, content));
|
|
190
|
+
if (maybeConfig != null) {
|
|
191
|
+
result.config = maybeConfig;
|
|
192
|
+
result.filepath = filepath;
|
|
193
|
+
break dirLoop;
|
|
194
|
+
}
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
const isEmpty = content.trim() === "";
|
|
198
|
+
if (isEmpty && ignoreEmptySearchPlaces) continue;
|
|
199
|
+
if (isEmpty) {
|
|
200
|
+
result.isEmpty = true;
|
|
201
|
+
result.config = void 0;
|
|
202
|
+
} else {
|
|
203
|
+
validateLoader(loader, loaderKey);
|
|
204
|
+
result.config = await loader(filepath, content);
|
|
205
|
+
}
|
|
206
|
+
result.filepath = filepath;
|
|
207
|
+
break dirLoop;
|
|
208
|
+
}
|
|
209
|
+
if (dir === stopDir || dir === parentDir(dir)) break dirLoop;
|
|
210
|
+
dir = parentDir(dir);
|
|
211
|
+
}
|
|
212
|
+
const transformed = result.filepath === "" && result.config === null ? transform(null) : transform(result);
|
|
213
|
+
if (cache) for (const p of visited) searchCache.set(p, transformed);
|
|
214
|
+
return transformed;
|
|
215
|
+
},
|
|
216
|
+
async load(filepath) {
|
|
217
|
+
validateFilePath(filepath);
|
|
218
|
+
const absPath = path$1.resolve(process.cwd(), filepath);
|
|
219
|
+
if (cache && loadCache.has(absPath)) return loadCache.get(absPath);
|
|
220
|
+
const { base, ext } = path$1.parse(absPath);
|
|
221
|
+
const loaderKey = ext || "noExt";
|
|
222
|
+
const loader = loaders[loaderKey];
|
|
223
|
+
validateLoader(loader, loaderKey);
|
|
224
|
+
const content = String(await fsReadFileAsync(absPath));
|
|
225
|
+
if (base === "package.json") return emplace(loadCache, absPath, transform({
|
|
226
|
+
config: getPackageProp(packageProp, await loader(absPath, content)),
|
|
227
|
+
filepath: absPath
|
|
228
|
+
}));
|
|
229
|
+
/** @type {import('./index').LilconfigResult} */
|
|
230
|
+
const result = {
|
|
231
|
+
config: null,
|
|
232
|
+
filepath: absPath
|
|
233
|
+
};
|
|
234
|
+
const isEmpty = content.trim() === "";
|
|
235
|
+
if (isEmpty && ignoreEmptySearchPlaces) return emplace(loadCache, absPath, transform({
|
|
236
|
+
config: void 0,
|
|
237
|
+
filepath: absPath,
|
|
238
|
+
isEmpty: true
|
|
239
|
+
}));
|
|
240
|
+
result.config = isEmpty ? void 0 : await loader(absPath, content);
|
|
241
|
+
return emplace(loadCache, absPath, transform(isEmpty ? {
|
|
242
|
+
...result,
|
|
243
|
+
isEmpty,
|
|
244
|
+
config: void 0
|
|
245
|
+
} : result));
|
|
246
|
+
},
|
|
247
|
+
clearLoadCache() {
|
|
248
|
+
if (cache) loadCache.clear();
|
|
249
|
+
},
|
|
250
|
+
clearSearchCache() {
|
|
251
|
+
if (cache) searchCache.clear();
|
|
252
|
+
},
|
|
253
|
+
clearCaches() {
|
|
254
|
+
if (cache) {
|
|
255
|
+
loadCache.clear();
|
|
256
|
+
searchCache.clear();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
};
|
|
261
|
+
/** @type {import('./index').lilconfigSync} */
|
|
262
|
+
module.exports.lilconfigSync = function lilconfigSync(name, options) {
|
|
263
|
+
const { ignoreEmptySearchPlaces, loaders, packageProp, searchPlaces, stopDir, transform, cache } = getOptions(name, options ?? {}, true);
|
|
264
|
+
const searchCache = /* @__PURE__ */ new Map();
|
|
265
|
+
const loadCache = /* @__PURE__ */ new Map();
|
|
266
|
+
const emplace = makeEmplace(cache);
|
|
267
|
+
return {
|
|
268
|
+
search(searchFrom = process.cwd()) {
|
|
269
|
+
/** @type {import('./index').LilconfigResult} */
|
|
270
|
+
const result = {
|
|
271
|
+
config: null,
|
|
272
|
+
filepath: ""
|
|
273
|
+
};
|
|
274
|
+
/** @type {Set<string>} */
|
|
275
|
+
const visited = /* @__PURE__ */ new Set();
|
|
276
|
+
let dir = searchFrom;
|
|
277
|
+
dirLoop: while (true) {
|
|
278
|
+
if (cache) {
|
|
279
|
+
const r = searchCache.get(dir);
|
|
280
|
+
if (r !== void 0) {
|
|
281
|
+
for (const p of visited) searchCache.set(p, r);
|
|
282
|
+
return r;
|
|
283
|
+
}
|
|
284
|
+
visited.add(dir);
|
|
285
|
+
}
|
|
286
|
+
for (const searchPlace of searchPlaces) {
|
|
287
|
+
const filepath = path$1.join(dir, searchPlace);
|
|
288
|
+
try {
|
|
289
|
+
fs.accessSync(filepath);
|
|
290
|
+
} catch {
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
const loaderKey = path$1.extname(searchPlace) || "noExt";
|
|
294
|
+
const loader = loaders[loaderKey];
|
|
295
|
+
const content = String(fs.readFileSync(filepath));
|
|
296
|
+
if (searchPlace === "package.json") {
|
|
297
|
+
const maybeConfig = getPackageProp(packageProp, loader(filepath, content));
|
|
298
|
+
if (maybeConfig != null) {
|
|
299
|
+
result.config = maybeConfig;
|
|
300
|
+
result.filepath = filepath;
|
|
301
|
+
break dirLoop;
|
|
302
|
+
}
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
const isEmpty = content.trim() === "";
|
|
306
|
+
if (isEmpty && ignoreEmptySearchPlaces) continue;
|
|
307
|
+
if (isEmpty) {
|
|
308
|
+
result.isEmpty = true;
|
|
309
|
+
result.config = void 0;
|
|
310
|
+
} else {
|
|
311
|
+
validateLoader(loader, loaderKey);
|
|
312
|
+
result.config = loader(filepath, content);
|
|
313
|
+
}
|
|
314
|
+
result.filepath = filepath;
|
|
315
|
+
break dirLoop;
|
|
316
|
+
}
|
|
317
|
+
if (dir === stopDir || dir === parentDir(dir)) break dirLoop;
|
|
318
|
+
dir = parentDir(dir);
|
|
319
|
+
}
|
|
320
|
+
const transformed = result.filepath === "" && result.config === null ? transform(null) : transform(result);
|
|
321
|
+
if (cache) for (const p of visited) searchCache.set(p, transformed);
|
|
322
|
+
return transformed;
|
|
323
|
+
},
|
|
324
|
+
load(filepath) {
|
|
325
|
+
validateFilePath(filepath);
|
|
326
|
+
const absPath = path$1.resolve(process.cwd(), filepath);
|
|
327
|
+
if (cache && loadCache.has(absPath)) return loadCache.get(absPath);
|
|
328
|
+
const { base, ext } = path$1.parse(absPath);
|
|
329
|
+
const loaderKey = ext || "noExt";
|
|
330
|
+
const loader = loaders[loaderKey];
|
|
331
|
+
validateLoader(loader, loaderKey);
|
|
332
|
+
const content = String(fs.readFileSync(absPath));
|
|
333
|
+
if (base === "package.json") return transform({
|
|
334
|
+
config: getPackageProp(packageProp, loader(absPath, content)),
|
|
335
|
+
filepath: absPath
|
|
336
|
+
});
|
|
337
|
+
const result = {
|
|
338
|
+
config: null,
|
|
339
|
+
filepath: absPath
|
|
340
|
+
};
|
|
341
|
+
const isEmpty = content.trim() === "";
|
|
342
|
+
if (isEmpty && ignoreEmptySearchPlaces) return emplace(loadCache, absPath, transform({
|
|
343
|
+
filepath: absPath,
|
|
344
|
+
config: void 0,
|
|
345
|
+
isEmpty: true
|
|
346
|
+
}));
|
|
347
|
+
result.config = isEmpty ? void 0 : loader(absPath, content);
|
|
348
|
+
return emplace(loadCache, absPath, transform(isEmpty ? {
|
|
349
|
+
...result,
|
|
350
|
+
isEmpty,
|
|
351
|
+
config: void 0
|
|
352
|
+
} : result));
|
|
353
|
+
},
|
|
354
|
+
clearLoadCache() {
|
|
355
|
+
if (cache) loadCache.clear();
|
|
356
|
+
},
|
|
357
|
+
clearSearchCache() {
|
|
358
|
+
if (cache) searchCache.clear();
|
|
359
|
+
},
|
|
360
|
+
clearCaches() {
|
|
361
|
+
if (cache) {
|
|
362
|
+
loadCache.clear();
|
|
363
|
+
searchCache.clear();
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
};
|
|
368
|
+
})))();
|
|
32
369
|
const jiti$1 = (0, jiti.createJiti)(require("url").pathToFileURL(__filename).href);
|
|
33
370
|
async function loadConfig(options) {
|
|
34
371
|
const { config, cwd, moduleName } = (0, _weapp_tailwindcss_shared.defuOverrideArray)(options, {
|
|
35
372
|
cwd: node_process.default.cwd(),
|
|
36
373
|
moduleName: "tailwind"
|
|
37
374
|
});
|
|
38
|
-
const searcher = (0,
|
|
375
|
+
const searcher = (0, import_src.lilconfig)("tailwindcss", {
|
|
39
376
|
searchPlaces: [
|
|
40
377
|
`${moduleName}.config.js`,
|
|
41
378
|
`${moduleName}.config.cjs`,
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import process$1 from "node:process";
|
|
4
|
+
import { createJiti } from "jiti";
|
|
5
|
+
import { defuOverrideArray } from "@weapp-tailwindcss/shared";
|
|
6
|
+
//#region \0rolldown/runtime.js
|
|
7
|
+
var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
|
|
8
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/utils.ts
|
|
11
|
+
var import_src = (/* @__PURE__ */ __commonJSMin(((exports, module) => {
|
|
12
|
+
const path$1 = __require("path");
|
|
13
|
+
const fs = __require("fs");
|
|
14
|
+
const os = __require("os");
|
|
15
|
+
const url = __require("url");
|
|
16
|
+
const fsReadFileAsync = fs.promises.readFile;
|
|
17
|
+
/** @type {(name: string, sync: boolean) => string[]} */
|
|
18
|
+
function getDefaultSearchPlaces(name, sync) {
|
|
19
|
+
return [
|
|
20
|
+
"package.json",
|
|
21
|
+
`.${name}rc.json`,
|
|
22
|
+
`.${name}rc.js`,
|
|
23
|
+
`.${name}rc.cjs`,
|
|
24
|
+
...sync ? [] : [`.${name}rc.mjs`],
|
|
25
|
+
`.config/${name}rc`,
|
|
26
|
+
`.config/${name}rc.json`,
|
|
27
|
+
`.config/${name}rc.js`,
|
|
28
|
+
`.config/${name}rc.cjs`,
|
|
29
|
+
...sync ? [] : [`.config/${name}rc.mjs`],
|
|
30
|
+
`${name}.config.js`,
|
|
31
|
+
`${name}.config.cjs`,
|
|
32
|
+
...sync ? [] : [`${name}.config.mjs`]
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* @type {(p: string) => string}
|
|
37
|
+
*
|
|
38
|
+
* see #17
|
|
39
|
+
* On *nix, if cwd is not under homedir,
|
|
40
|
+
* the last path will be '', ('/build' -> '')
|
|
41
|
+
* but it should be '/' actually.
|
|
42
|
+
* And on Windows, this will never happen. ('C:\build' -> 'C:')
|
|
43
|
+
*/
|
|
44
|
+
function parentDir(p) {
|
|
45
|
+
return path$1.dirname(p) || path$1.sep;
|
|
46
|
+
}
|
|
47
|
+
/** @type {import('./index').LoaderSync} */
|
|
48
|
+
const jsonLoader = (_, content) => JSON.parse(content);
|
|
49
|
+
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : __require;
|
|
50
|
+
/** @type {import('./index').LoadersSync} */
|
|
51
|
+
const defaultLoadersSync = Object.freeze({
|
|
52
|
+
".js": requireFunc,
|
|
53
|
+
".json": requireFunc,
|
|
54
|
+
".cjs": requireFunc,
|
|
55
|
+
noExt: jsonLoader
|
|
56
|
+
});
|
|
57
|
+
module.exports.defaultLoadersSync = defaultLoadersSync;
|
|
58
|
+
/** @type {import('./index').Loader} */
|
|
59
|
+
const dynamicImport = async (id) => {
|
|
60
|
+
try {
|
|
61
|
+
return (await import(
|
|
62
|
+
/* webpackIgnore: true */
|
|
63
|
+
url.pathToFileURL(id).href
|
|
64
|
+
)).default;
|
|
65
|
+
} catch (e) {
|
|
66
|
+
try {
|
|
67
|
+
return requireFunc(id);
|
|
68
|
+
} catch (requireE) {
|
|
69
|
+
if (requireE.code === "ERR_REQUIRE_ESM" || requireE instanceof SyntaxError && requireE.toString().includes("Cannot use import statement outside a module")) throw e;
|
|
70
|
+
throw requireE;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
/** @type {import('./index').Loaders} */
|
|
75
|
+
const defaultLoaders = Object.freeze({
|
|
76
|
+
".js": dynamicImport,
|
|
77
|
+
".mjs": dynamicImport,
|
|
78
|
+
".cjs": dynamicImport,
|
|
79
|
+
".json": jsonLoader,
|
|
80
|
+
noExt: jsonLoader
|
|
81
|
+
});
|
|
82
|
+
module.exports.defaultLoaders = defaultLoaders;
|
|
83
|
+
/**
|
|
84
|
+
* @param {string} name
|
|
85
|
+
* @param {import('./index').Options | import('./index').OptionsSync} options
|
|
86
|
+
* @param {boolean} sync
|
|
87
|
+
* @returns {Required<import('./index').Options | import('./index').OptionsSync>}
|
|
88
|
+
*/
|
|
89
|
+
function getOptions(name, options, sync) {
|
|
90
|
+
/** @type {Required<import('./index').Options>} */
|
|
91
|
+
const conf = {
|
|
92
|
+
stopDir: os.homedir(),
|
|
93
|
+
searchPlaces: getDefaultSearchPlaces(name, sync),
|
|
94
|
+
ignoreEmptySearchPlaces: true,
|
|
95
|
+
cache: true,
|
|
96
|
+
transform: (x) => x,
|
|
97
|
+
packageProp: [name],
|
|
98
|
+
...options,
|
|
99
|
+
loaders: {
|
|
100
|
+
...sync ? defaultLoadersSync : defaultLoaders,
|
|
101
|
+
...options.loaders
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
conf.searchPlaces.forEach((place) => {
|
|
105
|
+
const key = path$1.extname(place) || "noExt";
|
|
106
|
+
const loader = conf.loaders[key];
|
|
107
|
+
if (!loader) throw new Error(`Missing loader for extension "${place}"`);
|
|
108
|
+
if (typeof loader !== "function") throw new Error(`Loader for extension "${place}" is not a function: Received ${typeof loader}.`);
|
|
109
|
+
});
|
|
110
|
+
return conf;
|
|
111
|
+
}
|
|
112
|
+
/** @type {(props: string | string[], obj: Record<string, any>) => unknown} */
|
|
113
|
+
function getPackageProp(props, obj) {
|
|
114
|
+
if (typeof props === "string" && props in obj) return obj[props];
|
|
115
|
+
return (Array.isArray(props) ? props : props.split(".")).reduce((acc, prop) => acc === void 0 ? acc : acc[prop], obj) || null;
|
|
116
|
+
}
|
|
117
|
+
/** @param {string} filepath */
|
|
118
|
+
function validateFilePath(filepath) {
|
|
119
|
+
if (!filepath) throw new Error("load must pass a non-empty string");
|
|
120
|
+
}
|
|
121
|
+
/** @type {(loader: import('./index').Loader, ext: string) => void} */
|
|
122
|
+
function validateLoader(loader, ext) {
|
|
123
|
+
if (!loader) throw new Error(`No loader specified for extension "${ext}"`);
|
|
124
|
+
if (typeof loader !== "function") throw new Error("loader is not a function");
|
|
125
|
+
}
|
|
126
|
+
/** @type {(enableCache: boolean) => <T>(c: Map<string, T>, filepath: string, res: T) => T} */
|
|
127
|
+
const makeEmplace = (enableCache) => (c, filepath, res) => {
|
|
128
|
+
if (enableCache) c.set(filepath, res);
|
|
129
|
+
return res;
|
|
130
|
+
};
|
|
131
|
+
/** @type {import('./index').lilconfig} */
|
|
132
|
+
module.exports.lilconfig = function lilconfig(name, options) {
|
|
133
|
+
const { ignoreEmptySearchPlaces, loaders, packageProp, searchPlaces, stopDir, transform, cache } = getOptions(name, options ?? {}, false);
|
|
134
|
+
const searchCache = /* @__PURE__ */ new Map();
|
|
135
|
+
const loadCache = /* @__PURE__ */ new Map();
|
|
136
|
+
const emplace = makeEmplace(cache);
|
|
137
|
+
return {
|
|
138
|
+
async search(searchFrom = process.cwd()) {
|
|
139
|
+
/** @type {import('./index').LilconfigResult} */
|
|
140
|
+
const result = {
|
|
141
|
+
config: null,
|
|
142
|
+
filepath: ""
|
|
143
|
+
};
|
|
144
|
+
/** @type {Set<string>} */
|
|
145
|
+
const visited = /* @__PURE__ */ new Set();
|
|
146
|
+
let dir = searchFrom;
|
|
147
|
+
dirLoop: while (true) {
|
|
148
|
+
if (cache) {
|
|
149
|
+
const r = searchCache.get(dir);
|
|
150
|
+
if (r !== void 0) {
|
|
151
|
+
for (const p of visited) searchCache.set(p, r);
|
|
152
|
+
return r;
|
|
153
|
+
}
|
|
154
|
+
visited.add(dir);
|
|
155
|
+
}
|
|
156
|
+
for (const searchPlace of searchPlaces) {
|
|
157
|
+
const filepath = path$1.join(dir, searchPlace);
|
|
158
|
+
try {
|
|
159
|
+
await fs.promises.access(filepath);
|
|
160
|
+
} catch {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const content = String(await fsReadFileAsync(filepath));
|
|
164
|
+
const loaderKey = path$1.extname(searchPlace) || "noExt";
|
|
165
|
+
const loader = loaders[loaderKey];
|
|
166
|
+
if (searchPlace === "package.json") {
|
|
167
|
+
const maybeConfig = getPackageProp(packageProp, await loader(filepath, content));
|
|
168
|
+
if (maybeConfig != null) {
|
|
169
|
+
result.config = maybeConfig;
|
|
170
|
+
result.filepath = filepath;
|
|
171
|
+
break dirLoop;
|
|
172
|
+
}
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const isEmpty = content.trim() === "";
|
|
176
|
+
if (isEmpty && ignoreEmptySearchPlaces) continue;
|
|
177
|
+
if (isEmpty) {
|
|
178
|
+
result.isEmpty = true;
|
|
179
|
+
result.config = void 0;
|
|
180
|
+
} else {
|
|
181
|
+
validateLoader(loader, loaderKey);
|
|
182
|
+
result.config = await loader(filepath, content);
|
|
183
|
+
}
|
|
184
|
+
result.filepath = filepath;
|
|
185
|
+
break dirLoop;
|
|
186
|
+
}
|
|
187
|
+
if (dir === stopDir || dir === parentDir(dir)) break dirLoop;
|
|
188
|
+
dir = parentDir(dir);
|
|
189
|
+
}
|
|
190
|
+
const transformed = result.filepath === "" && result.config === null ? transform(null) : transform(result);
|
|
191
|
+
if (cache) for (const p of visited) searchCache.set(p, transformed);
|
|
192
|
+
return transformed;
|
|
193
|
+
},
|
|
194
|
+
async load(filepath) {
|
|
195
|
+
validateFilePath(filepath);
|
|
196
|
+
const absPath = path$1.resolve(process.cwd(), filepath);
|
|
197
|
+
if (cache && loadCache.has(absPath)) return loadCache.get(absPath);
|
|
198
|
+
const { base, ext } = path$1.parse(absPath);
|
|
199
|
+
const loaderKey = ext || "noExt";
|
|
200
|
+
const loader = loaders[loaderKey];
|
|
201
|
+
validateLoader(loader, loaderKey);
|
|
202
|
+
const content = String(await fsReadFileAsync(absPath));
|
|
203
|
+
if (base === "package.json") return emplace(loadCache, absPath, transform({
|
|
204
|
+
config: getPackageProp(packageProp, await loader(absPath, content)),
|
|
205
|
+
filepath: absPath
|
|
206
|
+
}));
|
|
207
|
+
/** @type {import('./index').LilconfigResult} */
|
|
208
|
+
const result = {
|
|
209
|
+
config: null,
|
|
210
|
+
filepath: absPath
|
|
211
|
+
};
|
|
212
|
+
const isEmpty = content.trim() === "";
|
|
213
|
+
if (isEmpty && ignoreEmptySearchPlaces) return emplace(loadCache, absPath, transform({
|
|
214
|
+
config: void 0,
|
|
215
|
+
filepath: absPath,
|
|
216
|
+
isEmpty: true
|
|
217
|
+
}));
|
|
218
|
+
result.config = isEmpty ? void 0 : await loader(absPath, content);
|
|
219
|
+
return emplace(loadCache, absPath, transform(isEmpty ? {
|
|
220
|
+
...result,
|
|
221
|
+
isEmpty,
|
|
222
|
+
config: void 0
|
|
223
|
+
} : result));
|
|
224
|
+
},
|
|
225
|
+
clearLoadCache() {
|
|
226
|
+
if (cache) loadCache.clear();
|
|
227
|
+
},
|
|
228
|
+
clearSearchCache() {
|
|
229
|
+
if (cache) searchCache.clear();
|
|
230
|
+
},
|
|
231
|
+
clearCaches() {
|
|
232
|
+
if (cache) {
|
|
233
|
+
loadCache.clear();
|
|
234
|
+
searchCache.clear();
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
/** @type {import('./index').lilconfigSync} */
|
|
240
|
+
module.exports.lilconfigSync = function lilconfigSync(name, options) {
|
|
241
|
+
const { ignoreEmptySearchPlaces, loaders, packageProp, searchPlaces, stopDir, transform, cache } = getOptions(name, options ?? {}, true);
|
|
242
|
+
const searchCache = /* @__PURE__ */ new Map();
|
|
243
|
+
const loadCache = /* @__PURE__ */ new Map();
|
|
244
|
+
const emplace = makeEmplace(cache);
|
|
245
|
+
return {
|
|
246
|
+
search(searchFrom = process.cwd()) {
|
|
247
|
+
/** @type {import('./index').LilconfigResult} */
|
|
248
|
+
const result = {
|
|
249
|
+
config: null,
|
|
250
|
+
filepath: ""
|
|
251
|
+
};
|
|
252
|
+
/** @type {Set<string>} */
|
|
253
|
+
const visited = /* @__PURE__ */ new Set();
|
|
254
|
+
let dir = searchFrom;
|
|
255
|
+
dirLoop: while (true) {
|
|
256
|
+
if (cache) {
|
|
257
|
+
const r = searchCache.get(dir);
|
|
258
|
+
if (r !== void 0) {
|
|
259
|
+
for (const p of visited) searchCache.set(p, r);
|
|
260
|
+
return r;
|
|
261
|
+
}
|
|
262
|
+
visited.add(dir);
|
|
263
|
+
}
|
|
264
|
+
for (const searchPlace of searchPlaces) {
|
|
265
|
+
const filepath = path$1.join(dir, searchPlace);
|
|
266
|
+
try {
|
|
267
|
+
fs.accessSync(filepath);
|
|
268
|
+
} catch {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
const loaderKey = path$1.extname(searchPlace) || "noExt";
|
|
272
|
+
const loader = loaders[loaderKey];
|
|
273
|
+
const content = String(fs.readFileSync(filepath));
|
|
274
|
+
if (searchPlace === "package.json") {
|
|
275
|
+
const maybeConfig = getPackageProp(packageProp, loader(filepath, content));
|
|
276
|
+
if (maybeConfig != null) {
|
|
277
|
+
result.config = maybeConfig;
|
|
278
|
+
result.filepath = filepath;
|
|
279
|
+
break dirLoop;
|
|
280
|
+
}
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const isEmpty = content.trim() === "";
|
|
284
|
+
if (isEmpty && ignoreEmptySearchPlaces) continue;
|
|
285
|
+
if (isEmpty) {
|
|
286
|
+
result.isEmpty = true;
|
|
287
|
+
result.config = void 0;
|
|
288
|
+
} else {
|
|
289
|
+
validateLoader(loader, loaderKey);
|
|
290
|
+
result.config = loader(filepath, content);
|
|
291
|
+
}
|
|
292
|
+
result.filepath = filepath;
|
|
293
|
+
break dirLoop;
|
|
294
|
+
}
|
|
295
|
+
if (dir === stopDir || dir === parentDir(dir)) break dirLoop;
|
|
296
|
+
dir = parentDir(dir);
|
|
297
|
+
}
|
|
298
|
+
const transformed = result.filepath === "" && result.config === null ? transform(null) : transform(result);
|
|
299
|
+
if (cache) for (const p of visited) searchCache.set(p, transformed);
|
|
300
|
+
return transformed;
|
|
301
|
+
},
|
|
302
|
+
load(filepath) {
|
|
303
|
+
validateFilePath(filepath);
|
|
304
|
+
const absPath = path$1.resolve(process.cwd(), filepath);
|
|
305
|
+
if (cache && loadCache.has(absPath)) return loadCache.get(absPath);
|
|
306
|
+
const { base, ext } = path$1.parse(absPath);
|
|
307
|
+
const loaderKey = ext || "noExt";
|
|
308
|
+
const loader = loaders[loaderKey];
|
|
309
|
+
validateLoader(loader, loaderKey);
|
|
310
|
+
const content = String(fs.readFileSync(absPath));
|
|
311
|
+
if (base === "package.json") return transform({
|
|
312
|
+
config: getPackageProp(packageProp, loader(absPath, content)),
|
|
313
|
+
filepath: absPath
|
|
314
|
+
});
|
|
315
|
+
const result = {
|
|
316
|
+
config: null,
|
|
317
|
+
filepath: absPath
|
|
318
|
+
};
|
|
319
|
+
const isEmpty = content.trim() === "";
|
|
320
|
+
if (isEmpty && ignoreEmptySearchPlaces) return emplace(loadCache, absPath, transform({
|
|
321
|
+
filepath: absPath,
|
|
322
|
+
config: void 0,
|
|
323
|
+
isEmpty: true
|
|
324
|
+
}));
|
|
325
|
+
result.config = isEmpty ? void 0 : loader(absPath, content);
|
|
326
|
+
return emplace(loadCache, absPath, transform(isEmpty ? {
|
|
327
|
+
...result,
|
|
328
|
+
isEmpty,
|
|
329
|
+
config: void 0
|
|
330
|
+
} : result));
|
|
331
|
+
},
|
|
332
|
+
clearLoadCache() {
|
|
333
|
+
if (cache) loadCache.clear();
|
|
334
|
+
},
|
|
335
|
+
clearSearchCache() {
|
|
336
|
+
if (cache) searchCache.clear();
|
|
337
|
+
},
|
|
338
|
+
clearCaches() {
|
|
339
|
+
if (cache) {
|
|
340
|
+
loadCache.clear();
|
|
341
|
+
searchCache.clear();
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
};
|
|
346
|
+
})))();
|
|
347
|
+
//#endregion
|
|
348
|
+
//#region src/index.ts
|
|
349
|
+
const jiti = createJiti(import.meta.url);
|
|
350
|
+
async function loadConfig(options) {
|
|
351
|
+
const { config, cwd, moduleName } = defuOverrideArray(options, {
|
|
352
|
+
cwd: process$1.cwd(),
|
|
353
|
+
moduleName: "tailwind"
|
|
354
|
+
});
|
|
355
|
+
const searcher = (0, import_src.lilconfig)("tailwindcss", {
|
|
356
|
+
searchPlaces: [
|
|
357
|
+
`${moduleName}.config.js`,
|
|
358
|
+
`${moduleName}.config.cjs`,
|
|
359
|
+
`${moduleName}.config.mjs`,
|
|
360
|
+
`${moduleName}.config.ts`,
|
|
361
|
+
`${moduleName}.config.cts`,
|
|
362
|
+
`${moduleName}.config.mts`
|
|
363
|
+
],
|
|
364
|
+
loaders: {
|
|
365
|
+
".js": jiti,
|
|
366
|
+
".cjs": jiti,
|
|
367
|
+
".mjs": jiti,
|
|
368
|
+
".ts": jiti,
|
|
369
|
+
".cts": jiti,
|
|
370
|
+
".mts": jiti
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
if (typeof config === "string" && config) {
|
|
374
|
+
const configPath = path.isAbsolute(config) ? config : path.resolve(cwd, config);
|
|
375
|
+
return await searcher.load(configPath);
|
|
376
|
+
} else return await searcher.search(cwd);
|
|
377
|
+
}
|
|
378
|
+
//#endregion
|
|
379
|
+
export { loadConfig };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tailwindcss-config",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0-next.2",
|
|
5
5
|
"description": "load tailwindcss config function",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -26,6 +26,9 @@
|
|
|
26
26
|
"require": "./dist/index.cjs"
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"module": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
29
32
|
"files": [
|
|
30
33
|
"dist"
|
|
31
34
|
],
|
|
@@ -37,9 +40,11 @@
|
|
|
37
40
|
"registry": "https://registry.npmjs.org"
|
|
38
41
|
},
|
|
39
42
|
"dependencies": {
|
|
40
|
-
"jiti": "
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
+
"jiti": "2.7.0",
|
|
44
|
+
"@weapp-tailwindcss/shared": "2.0.0-next.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"lilconfig": "^3.1.3"
|
|
43
48
|
},
|
|
44
49
|
"scripts": {
|
|
45
50
|
"dev": "tsdown --watch --sourcemap",
|
|
@@ -50,8 +55,5 @@
|
|
|
50
55
|
"release": "pnpm publish",
|
|
51
56
|
"lint": "eslint .",
|
|
52
57
|
"lint:fix": "eslint . --fix"
|
|
53
|
-
}
|
|
54
|
-
"main": "./dist/index.cjs",
|
|
55
|
-
"module": "./dist/index.js",
|
|
56
|
-
"types": "./dist/index.d.ts"
|
|
58
|
+
}
|
|
57
59
|
}
|
package/dist/index.mjs
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import process from "node:process";
|
|
3
|
-
import { createJiti } from "jiti";
|
|
4
|
-
import { lilconfig } from "lilconfig";
|
|
5
|
-
import { defuOverrideArray } from "@weapp-tailwindcss/shared";
|
|
6
|
-
//#region src/index.ts
|
|
7
|
-
const jiti = createJiti(import.meta.url);
|
|
8
|
-
async function loadConfig(options) {
|
|
9
|
-
const { config, cwd, moduleName } = defuOverrideArray(options, {
|
|
10
|
-
cwd: process.cwd(),
|
|
11
|
-
moduleName: "tailwind"
|
|
12
|
-
});
|
|
13
|
-
const searcher = lilconfig("tailwindcss", {
|
|
14
|
-
searchPlaces: [
|
|
15
|
-
`${moduleName}.config.js`,
|
|
16
|
-
`${moduleName}.config.cjs`,
|
|
17
|
-
`${moduleName}.config.mjs`,
|
|
18
|
-
`${moduleName}.config.ts`,
|
|
19
|
-
`${moduleName}.config.cts`,
|
|
20
|
-
`${moduleName}.config.mts`
|
|
21
|
-
],
|
|
22
|
-
loaders: {
|
|
23
|
-
".js": jiti,
|
|
24
|
-
".cjs": jiti,
|
|
25
|
-
".mjs": jiti,
|
|
26
|
-
".ts": jiti,
|
|
27
|
-
".cts": jiti,
|
|
28
|
-
".mts": jiti
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
if (typeof config === "string" && config) {
|
|
32
|
-
const configPath = path.isAbsolute(config) ? config : path.resolve(cwd, config);
|
|
33
|
-
return await searcher.load(configPath);
|
|
34
|
-
} else return await searcher.search(cwd);
|
|
35
|
-
}
|
|
36
|
-
//#endregion
|
|
37
|
-
export { loadConfig };
|