|
| 1 | +/* |
| 2 | + * Compile source for NPM |
| 3 | + */ |
| 4 | + |
| 5 | +import swc from '@swc/core' |
| 6 | +import { mkdirSync, rmSync, writeFileSync } from 'node:fs' |
| 7 | +import { glob } from 'node:fs/promises' |
| 8 | +import path, { dirname } from 'node:path' |
| 9 | +import { fileURLToPath } from 'node:url' |
| 10 | +import tsconfig from './tsconfig.npm.json' with { type: 'json' } |
| 11 | +import { updateImports } from './updateImports.ts' |
| 12 | + |
| 13 | +const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 14 | + |
| 15 | +const outDir = path.join(__dirname, '..', 'npm') |
| 16 | + |
| 17 | +try { |
| 18 | + rmSync(outDir, { recursive: true, force: true }) |
| 19 | +} catch { |
| 20 | + // pass |
| 21 | +} |
| 22 | + |
| 23 | +for await (const file of glob( |
| 24 | + `./.npm/{${tsconfig.include.join(',')}}/**/*.ts`, |
| 25 | +)) { |
| 26 | + if (file.endsWith('.spec.ts')) continue |
| 27 | + let compiled = ( |
| 28 | + await swc.transformFile(file, { |
| 29 | + jsc: { |
| 30 | + parser: { |
| 31 | + syntax: 'typescript', |
| 32 | + }, |
| 33 | + target: 'es2024', |
| 34 | + }, |
| 35 | + module: { |
| 36 | + type: 'es6', |
| 37 | + }, |
| 38 | + }) |
| 39 | + ).code |
| 40 | + |
| 41 | + compiled = updateImports(compiled) |
| 42 | + |
| 43 | + const targetFile = path.join(outDir, file.replace(/\.ts$/, '.js')) |
| 44 | + |
| 45 | + mkdirSync(dirname(targetFile), { recursive: true }) |
| 46 | + |
| 47 | + writeFileSync(targetFile, compiled, 'utf8') |
| 48 | + |
| 49 | + console.log(file) |
| 50 | +} |
0 commit comments