bigint-mod-arith/build/rollup-plugin-dts.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-04-13 16:30:48 +00:00
import { mkdirSync, writeFileSync } from 'fs'
2022-01-17 10:05:33 +00:00
import ts from 'typescript'
import { join, dirname, extname } from 'path'
2022-01-17 10:05:33 +00:00
import { sync } from 'rimraf'
import * as url from 'url'
const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
2022-01-17 10:05:33 +00:00
const { readJsonConfigFile, sys, parseJsonSourceFileConfigFileContent, createCompilerHost, createProgram } = ts
const rootDir = join(__dirname, '..')
const srcFile = join(rootDir, 'src/ts/index.ts')
const tsConfigPath = join(rootDir, 'tsconfig.json')
const configFile = readJsonConfigFile(tsConfigPath, (file) => {
return sys.readFile(file)
})
const tsConfig = parseJsonSourceFileConfigFileContent(configFile, sys, dirname(tsConfigPath))
2023-04-13 16:30:48 +00:00
export const compile = (outDir) => {
const compilerOptions = {
...tsConfig.options,
declaration: true,
declarationMap: true,
emitDeclarationOnly: true,
outDir
}
2022-01-17 10:05:33 +00:00
2023-04-13 16:30:48 +00:00
const host = createCompilerHost(compilerOptions)
2022-01-17 10:05:33 +00:00
2023-04-13 16:30:48 +00:00
host.writeFile = (fileName, contents) => {
mkdirSync(dirname(fileName), { recursive: true })
writeFileSync(fileName, contents)
2023-04-13 16:30:48 +00:00
// we also write the .d.cts types
let fileName2 = ''
if (extname(fileName) === '.ts') {
fileName2 = fileName.slice(0, -2) + 'cts'
} else { // ext is .d.ts.map
fileName2 = fileName.slice(0, -6) + 'cts.map'
}
writeFileSync(fileName2, contents)
}
2022-01-17 10:05:33 +00:00
// Clear the types dir
sync(outDir)
// Prepare and emit the d.ts files
const program = createProgram([srcFile], compilerOptions, host)
program.emit()
}