Complete core ES module migration with build system and tests
- 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
This commit is contained in:
parent
a8a7dc9445
commit
d51d3f60f1
11 changed files with 597 additions and 21 deletions
105
scripts/build.js
Normal file
105
scripts/build.js
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
#!/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);
|
||||
});
|
||||
|
|
@ -47,10 +47,26 @@ function parseDocComments(source, filename) {
|
|||
*/
|
||||
function parseDocContent(content, filename) {
|
||||
const lines = content.split('\n');
|
||||
const firstLine = lines[0].trim();
|
||||
const itemMatch = firstLine.match(re.item);
|
||||
|
||||
if (!itemMatch) return null;
|
||||
// Find the first non-empty line that matches the item pattern
|
||||
let itemMatch = null;
|
||||
let itemName = 'Unknown';
|
||||
|
||||
for (const line of lines) {
|
||||
const trimmed = line.trim();
|
||||
if (trimmed) {
|
||||
itemMatch = trimmed.match(re.item);
|
||||
if (itemMatch) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!itemMatch) {
|
||||
// If no item match, still try to extract tests with a generic name
|
||||
// This handles cases where tests are in script blocks or without proper headers
|
||||
itemMatch = ['', filename.replace(/.*\//, '').replace('.js', ''), 'tests', ''];
|
||||
}
|
||||
|
||||
const doc = {
|
||||
name: itemMatch[1],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue