|
| 1 | +import fs from 'fs' |
| 2 | + |
| 3 | +// Get source files |
| 4 | +const constSrc = fs.readFileSync('./src/const.js', 'utf8') |
| 5 | +const parseSrc = fs.readFileSync('./src/parse.js', 'utf8') |
| 6 | +const compileSrc = fs.readFileSync('./src/compile.js', 'utf8') |
| 7 | + |
| 8 | +// Replicate the bundle generation from repl.html |
| 9 | +const stripImports = code => code.replace(/^import\s+.*$/gm, '') |
| 10 | +const stripExports = code => code |
| 11 | + .replace(/^export\s+default\s+/gm, '') |
| 12 | + .replace(/^export\s+(const|let|function)\s+/gm, '$1 ') |
| 13 | + .replace(/^export\s+\{[^}]+\}.*$/gm, '') |
| 14 | + |
| 15 | +let bundle = `// Bundled subscript |
| 16 | +// https://github.com/phtml/subscript |
| 17 | +
|
| 18 | +(function() { |
| 19 | +'use strict'; |
| 20 | +
|
| 21 | +` |
| 22 | + |
| 23 | +// 1. Constants |
| 24 | +bundle += `// === src/const.js ===\n` |
| 25 | +bundle += stripExports(constSrc) + '\n\n' |
| 26 | + |
| 27 | +// 2. Parse |
| 28 | +bundle += `// === src/parse.js ===\n` |
| 29 | +let parseModule = stripImports(parseSrc) |
| 30 | + .replace(/^export\s+let\s+/gm, 'let ') |
| 31 | + .replace(/^export\s+default\s+parse\s*;?\s*$/gm, '') |
| 32 | +bundle += parseModule + '\n\n' |
| 33 | + |
| 34 | +// 3. Compile |
| 35 | +bundle += `// === src/compile.js ===\n` |
| 36 | +let compileModule = stripImports(compileSrc) |
| 37 | + .replace(/^export\s+const\s+/gm, 'const ') |
| 38 | + .replace(/^export\s+default\s+compile\s*;?\s*$/gm, '') |
| 39 | +bundle += compileModule + '\n\n' |
| 40 | + |
| 41 | +// Close IIFE |
| 42 | +bundle += `window.parse = parse; |
| 43 | +window.compile = compile; |
| 44 | +})(); |
| 45 | +` |
| 46 | + |
| 47 | +console.log('Bundle size:', bundle.length, 'bytes') |
| 48 | + |
| 49 | +// Test if it's valid JavaScript |
| 50 | +try { |
| 51 | + new Function(bundle) |
| 52 | + console.log('✓ Bundle is syntactically valid!') |
| 53 | +} catch (e) { |
| 54 | + console.log('❌ Bundle has syntax error:', e.message) |
| 55 | + // Find the line with error |
| 56 | + const lines = bundle.split('\n') |
| 57 | + const match = e.message.match(/position (\d+)/) |
| 58 | + if (match) { |
| 59 | + let pos = parseInt(match[1]) |
| 60 | + let lineNum = 0 |
| 61 | + for (let i = 0; i < lines.length; i++) { |
| 62 | + if (pos <= lines[i].length) { |
| 63 | + console.log(`Error around line ${i}: ${lines[i].slice(0, 100)}`) |
| 64 | + break |
| 65 | + } |
| 66 | + pos -= lines[i].length + 1 |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// Show compile.js section |
| 72 | +console.log('\n=== COMPILE.JS SECTION (first 30 lines) ===') |
| 73 | +const compileStart = bundle.indexOf('// === src/compile.js ===') |
| 74 | +const compileEnd = bundle.indexOf('window.parse') |
| 75 | +const compileSection = bundle.slice(compileStart, compileEnd).split('\n').slice(0, 35) |
| 76 | +compileSection.forEach((line, i) => console.log(`${i}: ${line.slice(0, 120)}`)) |
0 commit comments