swatchkit 0.0.19 → 0.0.21
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/build.js +80 -6
- package/package.json +1 -1
package/build.js
CHANGED
|
@@ -138,15 +138,18 @@ function resolveSettings(cliOptions, fileConfig) {
|
|
|
138
138
|
tokensDir,
|
|
139
139
|
exclude,
|
|
140
140
|
fileConfig, // Expose config to init
|
|
141
|
-
// Internal layout
|
|
141
|
+
// Internal layout templates (relative to this script)
|
|
142
142
|
internalLayout: path.join(__dirname, "src/layout.html"),
|
|
143
|
-
|
|
143
|
+
internalPreviewLayout: path.join(__dirname, "src/preview-layout.html"),
|
|
144
|
+
// Project specific layout overrides
|
|
144
145
|
projectLayout: path.join(swatchkitDir, "_layout.html"),
|
|
146
|
+
projectPreviewLayout: path.join(swatchkitDir, "_preview.html"),
|
|
145
147
|
|
|
146
148
|
// Derived paths
|
|
147
149
|
distCssDir: path.join(outDir, "css"),
|
|
148
150
|
distTokensCssFile: path.join(outDir, "css", "tokens.css"),
|
|
149
151
|
distJsDir: path.join(outDir, "js"),
|
|
152
|
+
distPreviewDir: path.join(outDir, "preview"),
|
|
150
153
|
outputFile: path.join(outDir, "index.html"),
|
|
151
154
|
outputJsFile: path.join(outDir, "js/swatches.js"),
|
|
152
155
|
tokensCssFile: path.join(cssDir, "tokens.css"),
|
|
@@ -220,6 +223,7 @@ function runInit(settings, options) {
|
|
|
220
223
|
copyTemplate("typography.html", "typography.html");
|
|
221
224
|
copyTemplate("spacing.html", "spacing.html");
|
|
222
225
|
copyTemplate("fonts.html", "fonts.html");
|
|
226
|
+
copyTemplate("prose.html", "prose.html");
|
|
223
227
|
|
|
224
228
|
// Create shared script for tokens UI
|
|
225
229
|
const tokensScriptFile = path.join(tokensUiDir, "script.js");
|
|
@@ -282,6 +286,19 @@ function runInit(settings, options) {
|
|
|
282
286
|
);
|
|
283
287
|
process.exit(1);
|
|
284
288
|
}
|
|
289
|
+
|
|
290
|
+
// Copy preview layout template (standalone page for individual swatches)
|
|
291
|
+
const targetPreview = settings.projectPreviewLayout;
|
|
292
|
+
if (!fs.existsSync(targetPreview)) {
|
|
293
|
+
if (fs.existsSync(settings.internalPreviewLayout)) {
|
|
294
|
+
const previewContent = fs.readFileSync(
|
|
295
|
+
settings.internalPreviewLayout,
|
|
296
|
+
"utf-8",
|
|
297
|
+
);
|
|
298
|
+
fs.writeFileSync(targetPreview, previewContent);
|
|
299
|
+
console.log(`Created preview layout at ${targetPreview}`);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
285
302
|
}
|
|
286
303
|
|
|
287
304
|
// --- 5. Build Logic ---
|
|
@@ -428,6 +445,8 @@ function build(settings) {
|
|
|
428
445
|
const sectionName =
|
|
429
446
|
item === "tokens" ? "Design Tokens" : toTitleCase(item);
|
|
430
447
|
const swatches = scanSwatches(itemPath, scripts, exclude);
|
|
448
|
+
// Tag each swatch with its section slug for preview paths
|
|
449
|
+
swatches.forEach((s) => (s.sectionSlug = item));
|
|
431
450
|
if (swatches.length > 0) {
|
|
432
451
|
sections[sectionName] = swatches;
|
|
433
452
|
}
|
|
@@ -447,14 +466,14 @@ function build(settings) {
|
|
|
447
466
|
if (stat.isFile() && item.endsWith(".html")) {
|
|
448
467
|
const name = path.basename(item, ".html");
|
|
449
468
|
const content = fs.readFileSync(itemPath, "utf-8");
|
|
450
|
-
rootSwatches.push({ name, id: name, content });
|
|
469
|
+
rootSwatches.push({ name, id: name, content, sectionSlug: null });
|
|
451
470
|
} else if (stat.isDirectory()) {
|
|
452
471
|
const indexFile = path.join(itemPath, "index.html");
|
|
453
472
|
if (fs.existsSync(indexFile)) {
|
|
454
473
|
// Component folder swatch at root
|
|
455
474
|
const name = item;
|
|
456
475
|
const content = fs.readFileSync(indexFile, "utf-8");
|
|
457
|
-
rootSwatches.push({ name, id: name, content });
|
|
476
|
+
rootSwatches.push({ name, id: name, content, sectionSlug: null });
|
|
458
477
|
|
|
459
478
|
// Collect JS
|
|
460
479
|
const jsFiles = fs
|
|
@@ -512,10 +531,16 @@ function build(settings) {
|
|
|
512
531
|
.replace(/"/g, """)
|
|
513
532
|
.replace(/'/g, "'");
|
|
514
533
|
|
|
534
|
+
// Build preview path: preview/{section}/{name}.html or preview/{name}.html
|
|
535
|
+
const previewPath = p.sectionSlug
|
|
536
|
+
? `preview/${p.sectionSlug}/${p.id}.html`
|
|
537
|
+
: `preview/${p.id}.html`;
|
|
538
|
+
|
|
515
539
|
return `
|
|
516
540
|
<section id="${p.id}">
|
|
517
541
|
<h2>${p.name} <small style="font-weight: normal; opacity: 0.6; font-size: 0.7em">(${section})</small></h2>
|
|
518
542
|
<div class="preview">${p.content}</div>
|
|
543
|
+
<div class="swatchkit-preview-link"><a href="${previewPath}">View full screen</a></div>
|
|
519
544
|
<pre><code>${escapedContent}</code></pre>
|
|
520
545
|
</section>
|
|
521
546
|
`;
|
|
@@ -533,7 +558,56 @@ function build(settings) {
|
|
|
533
558
|
fs.writeFileSync(settings.outputJsFile, "// No swatch scripts found");
|
|
534
559
|
}
|
|
535
560
|
|
|
536
|
-
// 7.
|
|
561
|
+
// 7. Generate preview pages (standalone full-screen view of each swatch)
|
|
562
|
+
let previewLayoutContent;
|
|
563
|
+
if (fs.existsSync(settings.projectPreviewLayout)) {
|
|
564
|
+
previewLayoutContent = fs.readFileSync(
|
|
565
|
+
settings.projectPreviewLayout,
|
|
566
|
+
"utf-8",
|
|
567
|
+
);
|
|
568
|
+
} else if (fs.existsSync(settings.internalPreviewLayout)) {
|
|
569
|
+
previewLayoutContent = fs.readFileSync(
|
|
570
|
+
settings.internalPreviewLayout,
|
|
571
|
+
"utf-8",
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (previewLayoutContent) {
|
|
576
|
+
let previewCount = 0;
|
|
577
|
+
sortedKeys.forEach((section) => {
|
|
578
|
+
const swatches = sections[section];
|
|
579
|
+
swatches.forEach((p) => {
|
|
580
|
+
// Determine output path and relative CSS path
|
|
581
|
+
let previewFile, cssPath;
|
|
582
|
+
if (p.sectionSlug) {
|
|
583
|
+
const sectionDir = path.join(settings.distPreviewDir, p.sectionSlug);
|
|
584
|
+
if (!fs.existsSync(sectionDir))
|
|
585
|
+
fs.mkdirSync(sectionDir, { recursive: true });
|
|
586
|
+
previewFile = path.join(sectionDir, `${p.id}.html`);
|
|
587
|
+
cssPath = "../../"; // preview/section/file.html -> ../../css/
|
|
588
|
+
} else {
|
|
589
|
+
if (!fs.existsSync(settings.distPreviewDir))
|
|
590
|
+
fs.mkdirSync(settings.distPreviewDir, { recursive: true });
|
|
591
|
+
previewFile = path.join(settings.distPreviewDir, `${p.id}.html`);
|
|
592
|
+
cssPath = "../"; // preview/file.html -> ../css/
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
const previewHtml = previewLayoutContent
|
|
596
|
+
.replace("<!-- PREVIEW_TITLE -->", p.name)
|
|
597
|
+
.replace("<!-- PREVIEW_CONTENT -->", p.content)
|
|
598
|
+
.replaceAll("<!-- CSS_PATH -->", cssPath)
|
|
599
|
+
.replace("<!-- HEAD_EXTRAS -->", "");
|
|
600
|
+
|
|
601
|
+
fs.writeFileSync(previewFile, previewHtml);
|
|
602
|
+
previewCount++;
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
console.log(
|
|
606
|
+
`Generated ${previewCount} preview pages in ${settings.distPreviewDir}`,
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// 8. Load Layout
|
|
537
611
|
let layoutContent;
|
|
538
612
|
if (fs.existsSync(settings.projectLayout)) {
|
|
539
613
|
console.log(`Using custom layout: ${settings.projectLayout}`);
|
|
@@ -551,7 +625,7 @@ function build(settings) {
|
|
|
551
625
|
.replace("<!-- SWATCHES -->", swatchBlocks)
|
|
552
626
|
.replace("<!-- HEAD_EXTRAS -->", headExtras);
|
|
553
627
|
|
|
554
|
-
//
|
|
628
|
+
// 9. Write output
|
|
555
629
|
fs.writeFileSync(settings.outputFile, finalHtml);
|
|
556
630
|
|
|
557
631
|
console.log(`Build complete! Generated ${settings.outputFile}`);
|