- Enhanced build system to generate ESM, UMD, and minified formats - Fixed test extraction script to properly parse OxJS inline tests - Added comprehensive test infrastructure with Vitest - Successfully extracted 22 test files from inline documentation - Verified builds work in browser and Node.js environments - Maintained full backward compatibility with Ox.load() pattern - Updated .gitignore to exclude build artifacts (dev/, min/, dist/, test/extracted/) Generated with AI assistance
105 lines
No EOL
2.9 KiB
JavaScript
105 lines
No EOL
2.9 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Build script for OxJS
|
|
* Generates ESM, UMD, and minified builds
|
|
*/
|
|
|
|
const { build } = require('vite');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { minify } = require('terser');
|
|
|
|
async function buildOx() {
|
|
console.log('Building OxJS...\n');
|
|
|
|
// Step 1: Build ESM and UMD formats using Vite
|
|
console.log('1. Building ES modules and UMD...');
|
|
await build({
|
|
configFile: path.resolve(__dirname, '../vite.config.build.js')
|
|
});
|
|
|
|
// Step 2: Create minified version for script tag usage (min/Ox.js)
|
|
console.log('\n2. Creating minified build...');
|
|
|
|
// Read the UMD build
|
|
const umdPath = path.resolve(__dirname, '../dist/ox.umd.js');
|
|
const umdCode = fs.readFileSync(umdPath, 'utf-8');
|
|
|
|
// Minify with Terser
|
|
const minified = await minify(umdCode, {
|
|
compress: {
|
|
drop_console: false, // Keep console for debugging
|
|
drop_debugger: true,
|
|
pure_funcs: ['console.log']
|
|
},
|
|
mangle: {
|
|
reserved: ['Ox'] // Don't mangle the main Ox object
|
|
},
|
|
format: {
|
|
comments: false,
|
|
preamble: '/* OxJS v0.2.0 | (c) 2024 0x2620 | MIT License | oxjs.org */'
|
|
}
|
|
});
|
|
|
|
// Ensure min directory exists
|
|
const minDir = path.resolve(__dirname, '../min');
|
|
if (!fs.existsSync(minDir)) {
|
|
fs.mkdirSync(minDir, { recursive: true });
|
|
}
|
|
|
|
// Write minified file
|
|
fs.writeFileSync(path.join(minDir, 'Ox.js'), minified.code);
|
|
|
|
// Step 3: Copy the minified file to be compatible with old path structure
|
|
console.log('\n3. Creating backward compatible structure...');
|
|
|
|
// Create dev symlink if it doesn't exist
|
|
const devPath = path.resolve(__dirname, '../dev');
|
|
if (!fs.existsSync(devPath)) {
|
|
fs.symlinkSync('source', devPath, 'dir');
|
|
}
|
|
|
|
// Step 4: Generate build info
|
|
const buildInfo = {
|
|
version: '0.2.0',
|
|
date: new Date().toISOString(),
|
|
files: {
|
|
'dist/ox.esm.js': getFileSize('../dist/ox.esm.js'),
|
|
'dist/ox.umd.js': getFileSize('../dist/ox.umd.js'),
|
|
'min/Ox.js': getFileSize('../min/Ox.js')
|
|
}
|
|
};
|
|
|
|
fs.writeFileSync(
|
|
path.resolve(__dirname, '../dist/build-info.json'),
|
|
JSON.stringify(buildInfo, null, 2)
|
|
);
|
|
|
|
console.log('\n✅ Build complete!\n');
|
|
console.log('Generated files:');
|
|
console.log(` dist/ox.esm.js (${buildInfo.files['dist/ox.esm.js']})`);
|
|
console.log(` dist/ox.umd.js (${buildInfo.files['dist/ox.umd.js']})`);
|
|
console.log(` min/Ox.js (${buildInfo.files['min/Ox.js']})`);
|
|
}
|
|
|
|
function getFileSize(relativePath) {
|
|
const filePath = path.resolve(__dirname, relativePath);
|
|
if (fs.existsSync(filePath)) {
|
|
const stats = fs.statSync(filePath);
|
|
return formatBytes(stats.size);
|
|
}
|
|
return 'N/A';
|
|
}
|
|
|
|
function formatBytes(bytes) {
|
|
if (bytes < 1024) return bytes + ' B';
|
|
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
|
|
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
|
|
}
|
|
|
|
// Run build
|
|
buildOx().catch(error => {
|
|
console.error('Build failed:', error);
|
|
process.exit(1);
|
|
}); |