|
| 1 | +import { mkdir, readFile, rm, writeFile } from 'node:fs/promises' |
| 2 | +import { dirname, join } from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | +import { spawn } from 'node:child_process' |
| 5 | +import { compileToTSWithMap, compileWithMap } from '@tu-lang/compiler' |
| 6 | + |
| 7 | +const root = join(dirname(fileURLToPath(import.meta.url)), '..') |
| 8 | +const sourcePath = join(root, 'src', 'index.tu') |
| 9 | +const distDir = join(root, 'dist') |
| 10 | +const buildDir = join(root, '.tu-build') |
| 11 | +const source = await readFile(sourcePath, 'utf8') |
| 12 | +const filename = 'src/index.tu' |
| 13 | + |
| 14 | +await rm(distDir, { recursive: true, force: true }) |
| 15 | +await rm(buildDir, { recursive: true, force: true }) |
| 16 | +await mkdir(distDir, { recursive: true }) |
| 17 | +await mkdir(buildDir, { recursive: true }) |
| 18 | + |
| 19 | +const stripInlineMap = (code) => code.replace(/\n?\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,[A-Za-z0-9+/=]+\n?$/, '') |
| 20 | + |
| 21 | +const js = compileWithMap(source, { filename }) |
| 22 | +await writeFile(join(distDir, 'index.js'), `${stripInlineMap(js.code)}\n//# sourceMappingURL=index.js.map\n`) |
| 23 | +await writeFile(join(distDir, 'index.js.map'), JSON.stringify({ |
| 24 | + ...js.map, |
| 25 | + file: 'index.js', |
| 26 | + sources: ['../src/index.tu'], |
| 27 | +})) |
| 28 | + |
| 29 | +const ts = compileToTSWithMap(source, { filename }) |
| 30 | +await writeFile(join(buildDir, 'index.ts'), stripInlineMap(ts.code)) |
| 31 | + |
| 32 | +await new Promise((resolve, reject) => { |
| 33 | + const child = spawn( |
| 34 | + process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm', |
| 35 | + ['exec', 'tsc', '-p', 'tsconfig.dts.json'], |
| 36 | + { cwd: root, stdio: 'inherit' } |
| 37 | + ) |
| 38 | + child.on('error', reject) |
| 39 | + child.on('exit', (code) => { |
| 40 | + if (code === 0) resolve() |
| 41 | + else reject(new Error(`tsc exited with code ${code}`)) |
| 42 | + }) |
| 43 | +}) |
0 commit comments