askrobots / dbbasic-web-native
Public
100+ copy-paste UI components in plain HTML and CSS: no dependencies, no build step.
danq9
Add canonical.html v1.0 - The single source of truth for usability standards
fb6e650
1 year ago
History
build.js · 2.3 KB · 78 lines
| 1 | #!/usr/bin/env node |
| 2 | |
| 3 | /** |
| 4 | * Simple build script for Semantic Components |
| 5 | * Minifies source files for CDN distribution |
| 6 | */ |
| 7 | |
| 8 | const fs = require('fs'); |
| 9 | const path = require('path'); |
| 10 | |
| 11 | const sourceFile = process.argv[2]; |
| 12 | |
| 13 | if (!sourceFile) { |
| 14 | console.error('Usage: node build.js <source-file>'); |
| 15 | process.exit(1); |
| 16 | } |
| 17 | |
| 18 | const sourcePath = path.join(__dirname, sourceFile); |
| 19 | const fileName = path.basename(sourceFile, '.js'); |
| 20 | |
| 21 | // Create dist directory if it doesn't exist |
| 22 | const distDir = path.join(__dirname, 'dist'); |
| 23 | if (!fs.existsSync(distDir)) { |
| 24 | fs.mkdirSync(distDir); |
| 25 | } |
| 26 | |
| 27 | // Read source file |
| 28 | const source = fs.readFileSync(sourcePath, 'utf8'); |
| 29 | |
| 30 | // Add version header |
| 31 | const version = require('./package.json').version; |
| 32 | const header = `/** |
| 33 | * Semantic Components v${version} |
| 34 | * ${fileName}.js |
| 35 | * |
| 36 | * Intent-based, context-aware, modality-agnostic web components |
| 37 | * https://semantic-components.dev |
| 38 | * |
| 39 | * Copyright (c) ${new Date().getFullYear()} Semantic Components Team |
| 40 | * Released under the MIT License |
| 41 | */ |
| 42 | |
| 43 | `; |
| 44 | |
| 45 | // Copy to dist (full version) |
| 46 | const fullPath = path.join(distDir, `${fileName}.js`); |
| 47 | fs.writeFileSync(fullPath, header + source); |
| 48 | console.log(`✓ Built ${fileName}.js`); |
| 49 | |
| 50 | // Create ESM version |
| 51 | const esmSource = source.replace( |
| 52 | /if \(typeof module !== 'undefined' && module\.exports\) \{[\s\S]*?\}/g, |
| 53 | '' |
| 54 | ); |
| 55 | const esmPath = path.join(distDir, `${fileName}.esm.js`); |
| 56 | fs.writeFileSync(esmPath, header + esmSource + '\nexport default window.semanticContext || {};'); |
| 57 | console.log(`✓ Built ${fileName}.esm.js`); |
| 58 | |
| 59 | // Simple minification (remove comments and extra whitespace) |
| 60 | const minified = source |
| 61 | .replace(/\/\*[\s\S]*?\*\//g, '') // Remove multi-line comments |
| 62 | .replace(/\/\/.*/g, '') // Remove single-line comments |
| 63 | .replace(/\s+/g, ' ') // Collapse whitespace |
| 64 | .replace(/\s*([{}();,:])\s*/g, '$1') // Remove spaces around symbols |
| 65 | .trim(); |
| 66 | |
| 67 | const minPath = path.join(distDir, `${fileName}.min.js`); |
| 68 | fs.writeFileSync(minPath, header + minified); |
| 69 | console.log(`✓ Built ${fileName}.min.js`); |
| 70 | |
| 71 | // File sizes |
| 72 | const fullSize = fs.statSync(fullPath).size; |
| 73 | const minSize = fs.statSync(minPath).size; |
| 74 | const savings = ((1 - minSize / fullSize) * 100).toFixed(1); |
| 75 | |
| 76 | console.log(`\nFile sizes:`); |
| 77 | console.log(` Full: ${(fullSize / 1024).toFixed(1)} KB`); |
| 78 | console.log(` Min: ${(minSize / 1024).toFixed(1)} KB (${savings}% smaller)`); |