|
| 1 | +#!/usr/bin/env node |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +// Simple dist builder: resolves local @import url(...) statements |
| 6 | +// and concatenates referenced files in order. Does not fetch remote URLs. |
| 7 | + |
| 8 | +const repoRoot = path.resolve(__dirname, '..'); |
| 9 | +const cssRoot = path.join(repoRoot, 'css'); |
| 10 | +const entry = path.join(cssRoot, 'final-fantasy.css'); |
| 11 | +const outDir = path.join(repoRoot, 'dist'); |
| 12 | +const outFile = path.join(outDir, 'final-fantasy.css'); |
| 13 | + |
| 14 | +function readFileSafe(p){ |
| 15 | + if(!fs.existsSync(p)) return null; |
| 16 | + return fs.readFileSync(p,'utf8'); |
| 17 | +} |
| 18 | + |
| 19 | +function resolveImportPath(baseDir, importPath){ |
| 20 | + // strip surrounding url(...) or quotes |
| 21 | + importPath = importPath.trim(); |
| 22 | + importPath = importPath.replace(/^url\((.*)\)$/,'$1').replace(/^['"]|['"]$/g,''); |
| 23 | + // ignore absolute URLs (http/https) |
| 24 | + if(/^https?:\/\//.test(importPath)) return null; |
| 25 | + // if path is relative, resolve |
| 26 | + return path.resolve(baseDir, importPath); |
| 27 | +} |
| 28 | + |
| 29 | +function processCss(filePath, seen){ |
| 30 | + seen = seen || new Set(); |
| 31 | + if(!filePath || seen.has(filePath)) return ''; |
| 32 | + seen.add(filePath); |
| 33 | + const content = readFileSafe(filePath); |
| 34 | + if(content === null) { |
| 35 | + console.warn('Missing file:', filePath); |
| 36 | + return ''; |
| 37 | + } |
| 38 | + |
| 39 | + const dir = path.dirname(filePath); |
| 40 | + let out = `/* ===== ${path.relative(repoRoot, filePath)} ===== */\n`; |
| 41 | + // Process @import url(...) and @import "..." occurrences |
| 42 | + const importRegex = /@import\s+(?:url\()?\s*([^)';]+|"[^"]+"|'[^']+')\s*\)?\s*;?/g; |
| 43 | + let lastIndex = 0; |
| 44 | + let m; |
| 45 | + while((m = importRegex.exec(content)) !== null){ |
| 46 | + const matchStart = m.index; |
| 47 | + // append content before this import |
| 48 | + out += content.slice(lastIndex, matchStart); |
| 49 | + lastIndex = importRegex.lastIndex; |
| 50 | + const rawPath = m[1]; |
| 51 | + const resolved = resolveImportPath(dir, rawPath); |
| 52 | + if(resolved){ |
| 53 | + // If the resolved path is a directory or not a .css, try adding .css |
| 54 | + let candidate = resolved; |
| 55 | + if(!path.extname(candidate)) candidate = candidate + '.css'; |
| 56 | + if(!fs.existsSync(candidate) && fs.existsSync(resolved + '.css')) candidate = resolved + '.css'; |
| 57 | + if(fs.existsSync(candidate)){ |
| 58 | + out += processCss(candidate, seen); |
| 59 | + continue; // skip adding the import line itself |
| 60 | + } else { |
| 61 | + // not found, skip but keep the import as-is |
| 62 | + out += `/* skipped unresolved import: ${rawPath} */\n`; |
| 63 | + } |
| 64 | + } else { |
| 65 | + // remote import (http), keep as-is |
| 66 | + out += m[0] + '\n'; |
| 67 | + } |
| 68 | + } |
| 69 | + // append remaining content |
| 70 | + out += content.slice(lastIndex); |
| 71 | + return out + '\n'; |
| 72 | +} |
| 73 | + |
| 74 | +function ensureDir(d){ if(!fs.existsSync(d)) fs.mkdirSync(d, { recursive: true }); } |
| 75 | + |
| 76 | +function build(){ |
| 77 | + ensureDir(outDir); |
| 78 | + const result = processCss(entry); |
| 79 | + // simple header |
| 80 | + const header = `/* Final Fantasy CSS - built dist (generated by scripts/build-dist.js) */\n`; |
| 81 | + fs.writeFileSync(outFile, header + '\n' + result, 'utf8'); |
| 82 | + console.log('Wrote', outFile); |
| 83 | +} |
| 84 | + |
| 85 | +build(); |
0 commit comments