Files
nostr_login_lite/lite/bundler-clean.js
2025-09-13 09:06:32 -04:00

97 lines
2.8 KiB
JavaScript

/**
* Clean bundler for NOSTR_LOGIN_LITE
* Removes problematic files and recreates bundle
*/
const fs = require('fs');
const path = require('path');
async function createCleanBundle() {
// First, remove the old bundle if it exists
const outputPath = path.join(__dirname, 'nostr-login-lite.bundle.js');
try {
if (fs.existsSync(outputPath)) {
fs.unlinkSync(outputPath);
}
} catch (e) {
console.log('No old bundle to remove');
}
const mainFile = path.join(__dirname, 'nostr-login-lite.js');
const nip46File = path.join(__dirname, 'core/nip46-client.js');
const modalFile = path.join(__dirname, 'ui/modal.js');
// Start with a clean header
let bundle = `/**
* NOSTR_LOGIN_LITE
* Single-file Nostr authentication library
* Generated on: ${new Date().toISOString()}
*/
`;
// Add section markers and combine files
const files = [
{ path: modalFile, name: 'modal.js' },
{ path: nip46File, name: 'nip46-client.js' },
{ path: mainFile, name: 'nostr-login-lite.js' }
];
for (const file of files) {
if (fs.existsSync(file.path)) {
const content = fs.readFileSync(file.path, 'utf8');
bundle += `\n// ======================================\n`;
bundle += `// ${file.name}\n`;
bundle += `// ======================================\n\n`;
// Clean the content by removing initial header comments
let lines = content.split('\n');
let contentStartIndex = 0;
// Skip the first 10 lines if they contain file headers
for (let i = 0; i < Math.min(10, lines.length); i++) {
const line = lines[i].trim();
if (line.startsWith('/**') || line.startsWith('*') || line.startsWith('/*') || line.startsWith('//') ||
line.includes('Copyright') || line.includes('@license') || line.includes('Licensed') || line.includes('©')) {
contentStartIndex = i + 1;
}
}
if (contentStartIndex > 0) {
lines = lines.slice(contentStartIndex);
}
bundle += lines.join('\n');
bundle += '\n\n';
console.log(`Added ${file.name}`);
} else {
console.warn(`File not found: ${file.path}`);
}
}
// Write the bundled file
fs.writeFileSync(outputPath, bundle, 'utf8');
const sizeKB = (bundle.length / 1024).toFixed(2);
console.log(`\n✅ Clean bundle created: ${outputPath}`);
console.log(`📏 Bundle size: ${sizeKB} KB`);
console.log(`📄 Total lines: ${bundle.split('\n').length}`);
// Verify the bundle starts correctly
const firstLines = bundle.split('\n').slice(0, 20).join('\n');
console.log('\n📋 First 20 lines:');
console.log(firstLines);
return bundle;
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = { createCleanBundle };
}
// Run if called directly
if (typeof require !== 'undefined' && require.main === module) {
createCleanBundle().catch(console.error);
}