Skip to content

Commit 4be4c8d

Browse files
committed
0.24.1 - debug logging, allowing customizing buildSubdirectories
1 parent 037299c commit 4be4c8d

2 files changed

Lines changed: 46 additions & 35 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@schemavaults/theme",
33
"description": "TailwindCSS theme shared by different SchemaVaults applications",
4-
"version": "0.24.0",
4+
"version": "0.24.1",
55
"private": false,
66
"license": "UNLICENSED",
77
"repository": {

src/TailwindConfigFactory.ts

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import {
88
type ScreenBreakpointID,
99
} from "./ScreenBreakpoints";
1010
import type { TailwindTheme } from "./TailwindTheme";
11-
type OsJoinFn = (
12-
path_segment: string,
13-
...remaining_path_segments: string[]
14-
) => string;
1511
import { existsSync, lstatSync } from "fs";
1612
import { normalize, join, dirname } from "path";
1713

@@ -26,6 +22,10 @@ export interface ISchemaVaultsTailwindConfigFactoryInitOptions {
2622
fileExtensions?: readonly string[];
2723
// Manually specify the project root directory, defaults to process.cwd()
2824
project_root?: string;
25+
// Build subdirectories
26+
// (e.g. ['dist'] here, in combination with @schemavaults/ui in the 'content' array of createConfig(),
27+
// would search node_modules/@schemavaults/ui/dist for classNames)
28+
buildSubdirectories?: readonly string[];
2929
}
3030

3131
export interface ISchemaVaultsTailwindConfigCreationOptions {
@@ -49,8 +49,6 @@ export class SchemaVaultsTailwindConfigFactory
4949
{
5050
protected readonly debug: boolean;
5151

52-
protected readonly join: OsJoinFn;
53-
5452
protected readonly isdir: (path: string) => boolean;
5553
protected readonly isfile: (path: string) => boolean;
5654

@@ -66,14 +64,9 @@ export class SchemaVaultsTailwindConfigFactory
6664
"svelte",
6765
] as const satisfies readonly string[];
6866

69-
protected readonly fileExtensionsToIncludeInTailwindClassNamesSearch: readonly string[];
67+
protected readonly possibleBuildDirectories: readonly string[];
7068

71-
private static defaultJoinImplementation(
72-
path_segment: string,
73-
...remaining_path_segments: string[]
74-
): string {
75-
return join(path_segment, ...remaining_path_segments);
76-
}
69+
protected readonly fileExtensionsToIncludeInTailwindClassNamesSearch: readonly string[];
7770

7871
private static defaultIsDirImplementation(maybeDirPath: string): boolean {
7972
if (!existsSync(maybeDirPath)) {
@@ -89,18 +82,21 @@ export class SchemaVaultsTailwindConfigFactory
8982
return lstatSync(maybeFilePath).isFile();
9083
}
9184

85+
private static prettyPrintItemList(items: readonly string[]): string {
86+
return `${items.map((d) => `"${d}"`).join(", ")}`;
87+
}
88+
9289
public constructor(opts?: ISchemaVaultsTailwindConfigFactoryInitOptions) {
9390
this.debug = opts?.debug ?? false;
9491
if (this.debug) {
9592
console.log("[SchemaVaultsTailwindConfigFactory] constructor()");
9693
}
97-
this.join = SchemaVaultsTailwindConfigFactory.defaultJoinImplementation;
9894
this.isdir = SchemaVaultsTailwindConfigFactory.defaultIsDirImplementation;
9995
this.isfile = SchemaVaultsTailwindConfigFactory.defaultIsFileImplementation;
10096
this.scope = opts?.scope ?? DefaultOrgScope;
10197
if (this.scope.startsWith("@")) {
102-
throw new Error(
103-
"Don't pass the @ in the scope, we'll handle adding passing that!",
98+
throw new TypeError(
99+
"Don't pass the @ in the 'scope', we'll handle adding passing that!",
104100
);
105101
}
106102

@@ -112,6 +108,10 @@ export class SchemaVaultsTailwindConfigFactory
112108

113109
this.project_root = opts?.project_root ?? process.cwd();
114110

111+
this.possibleBuildDirectories = Array.isArray(opts?.buildSubdirectories)
112+
? opts.buildSubdirectories
113+
: SchemaVaultsTailwindConfigFactory.defaultPossibleBuildDirectories;
114+
115115
if (this.debug) {
116116
console.log(
117117
`[SchemaVaultsTailwindConfigFactory] Initialized '@${this.scope}' config factory`,
@@ -214,7 +214,7 @@ export class SchemaVaultsTailwindConfigFactory
214214
if (!this.isdir(current_path)) {
215215
return false;
216216
} else {
217-
if (this.join(current_path, "package.json")) {
217+
if (join(current_path, "package.json")) {
218218
return true;
219219
} else {
220220
return false;
@@ -240,7 +240,7 @@ export class SchemaVaultsTailwindConfigFactory
240240
}
241241

242242
if (this.isdir(current_path)) {
243-
current_path = normalize(this.join(current_path, ".."));
243+
current_path = normalize(join(current_path, ".."));
244244
} else if (this.isfile(current_path)) {
245245
current_path = dirname(current_path);
246246
} else {
@@ -276,7 +276,7 @@ export class SchemaVaultsTailwindConfigFactory
276276
return package_path;
277277
}
278278

279-
private static possibleBuildDirectories: readonly string[] = [
279+
private static defaultPossibleBuildDirectories: readonly string[] = [
280280
"dist",
281281
"src",
282282
"build",
@@ -313,14 +313,20 @@ export class SchemaVaultsTailwindConfigFactory
313313
const package_path: string =
314314
this.resolveOrganizationScopedPackagePath(content_path_input);
315315

316-
SchemaVaultsTailwindConfigFactory.possibleBuildDirectories.forEach(
317-
(possible_build_dir) => {
318-
const buildSubdirPath: string = this.join(
316+
this.possibleBuildDirectories.forEach(
317+
(possible_build_dir: string): void => {
318+
const buildSubdirPath: string = join(
319319
package_path,
320320
possible_build_dir,
321321
);
322-
let hasBuildSubdirectory: boolean;
322+
let hasBuildSubdirectory: boolean = false;
323323
try {
324+
if (this.debug) {
325+
console.log(
326+
`[SchemaVaultsTailwindConfigFactory] Checking if build subdirectory exists at path:`,
327+
buildSubdirPath,
328+
);
329+
}
324330
hasBuildSubdirectory = this.isdir(buildSubdirPath);
325331
} catch (e: unknown) {
326332
console.error(
@@ -337,9 +343,15 @@ export class SchemaVaultsTailwindConfigFactory
337343
}
338344

339345
if (hasBuildSubdirectory) {
340-
content.push(
341-
`${package_path}/${possible_build_dir}/**/*.{${this.fileExtensionsToIncludeInTailwindClassNamesSearch.join(",")}}`,
342-
);
346+
const tailwindContentSearchPath: string = `${buildSubdirPath}/**/*.{${this.fileExtensionsToIncludeInTailwindClassNamesSearch.join(",")}}`;
347+
content.push(tailwindContentSearchPath);
348+
if (this.debug) {
349+
console.log(
350+
`[SchemaVaultsTailwindConfigFactory] ` +
351+
`Found build subdirectory. Added TailwindCSS search blob:`,
352+
tailwindContentSearchPath,
353+
);
354+
}
343355
}
344356
},
345357
);
@@ -357,9 +369,7 @@ export class SchemaVaultsTailwindConfigFactory
357369
content_path_input.startsWith(`@${scope}/`)
358370
) {
359371
console.warn(
360-
`Did not find any known build directories (${SchemaVaultsTailwindConfigFactory.possibleBuildDirectories
361-
.map((d) => `"${d}"`)
362-
.join(", ")})!`,
372+
`Did not find any known build subdirectories (searching subdirectories: ${SchemaVaultsTailwindConfigFactory.prettyPrintItemList(this.possibleBuildDirectories)}) within content path input '${content_path_input}'!`,
363373
);
364374
}
365375
});
@@ -370,7 +380,7 @@ export class SchemaVaultsTailwindConfigFactory
370380
);
371381
}
372382

373-
const plugins = this.plugins;
383+
const plugins: TailwindConfig["plugins"] = this.plugins;
374384
const extend: ThemeExtension = this.themeExtension;
375385

376386
const config = {
@@ -385,10 +395,11 @@ export class SchemaVaultsTailwindConfigFactory
385395
"Failed to load any 'content' search blobs to generate TailwindCSS classes for!",
386396
);
387397

388-
console.assert(
389-
config.content.every((blob) => typeof blob === "string"),
390-
"Received a non-string search blob for the 'content' field of the Tailwind configuration!",
391-
);
398+
if (!config.content.every((blob): boolean => typeof blob === "string")) {
399+
throw new TypeError(
400+
"Received a non-string search blob for the 'content' field of the Tailwind configuration!",
401+
);
402+
}
392403

393404
if (this.debug) {
394405
console.log(

0 commit comments

Comments
 (0)