| 1 | import { readFile, writeFile } from 'fs/promises'; |
| 2 | import { dirname, resolve } from 'path'; |
| 3 | import { fileURLToPath } from 'url'; |
| 4 | import { transform } from 'esbuild'; |
| 5 | import ts from 'typescript'; |
| 6 | import { LICENSE_BANNER } from './banner.js'; |
| 7 | |
| 8 | const __filename = fileURLToPath(import.meta.url); |
| 9 | const __dirname = dirname(__filename); |
| 10 | const root = resolve(__dirname, '..'); |
| 11 | |
| 12 | const inputPath = resolve(root, 'dist/reveal.js'); |
| 13 | const outputPath = resolve(root, 'dist/reveal.es5.js'); |
| 14 | |
| 15 | try { |
| 16 | const source = await readFile(inputPath, 'utf8'); |
| 17 | const result = ts.transpileModule(source, { |
| 18 | compilerOptions: { |
| 19 | // This script intentionally emits a legacy ES5 UMD bundle. |
| 20 | ignoreDeprecations: '6.0', |
| 21 | target: ts.ScriptTarget.ES5, |
| 22 | module: ts.ModuleKind.UMD, |
| 23 | downlevelIteration: true, |
| 24 | removeComments: false, |
| 25 | }, |
| 26 | fileName: 'reveal.js', |
| 27 | reportDiagnostics: true, |
| 28 | }); |
| 29 | |
| 30 | if (result.diagnostics?.length) { |
| 31 | const errors = result.diagnostics |
| 32 | .filter(diagnostic => diagnostic.category === ts.DiagnosticCategory.Error) |
| 33 | .map(diagnostic => ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')); |
| 34 | |
| 35 | if (errors.length) { |
| 36 | throw new Error(`TypeScript transpilation failed:\n${errors.join('\n')}`); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | const minified = await transform(result.outputText, { |
| 41 | loader: 'js', |
| 42 | target: ['es5'], |
| 43 | minify: true, |
| 44 | legalComments: 'none', |
| 45 | }); |
| 46 | |
| 47 | await writeFile(outputPath, `${LICENSE_BANNER}\n${minified.code.trimStart()}`, 'utf8'); |
| 48 | |
| 49 | console.log(`Built ES5 UMD bundle: ${outputPath} (target: es5, minified)`); |
| 50 | } catch (error) { |
| 51 | if (error?.code === 'ENOENT') { |
| 52 | console.error(`Could not find ${inputPath}. Run "npm run build" first.`); |
| 53 | process.exit(1); |
| 54 | } |
| 55 | |
| 56 | throw error; |
| 57 | } |
| 58 |