Skip to content

Commit

Permalink
feat(compiler): create declaration files for plugin builds
Browse files Browse the repository at this point in the history
Robo plugins may sometimes need to export things to be used from the package directly. Declaration files for plugins built with Typescript are kinda expected, so let's output them!
  • Loading branch information
Pkmmte committed Aug 7, 2023
1 parent fb97b95 commit f6168a0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/lemon-items-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@roboplay/robo.js': minor
---

feat(compiler): create declaration files for plugin builds
2 changes: 1 addition & 1 deletion packages/robo/src/cli/commands/build/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function pluginAction() {

// Use SWC to compile into .robo/build
const { compile } = await import('../../utils/compiler.js')
const compileTime = await compile()
const compileTime = await compile({ plugin: true })
logger.debug(`Compiled in ${compileTime}ms`)

// Generate manifest.json
Expand Down
43 changes: 43 additions & 0 deletions packages/robo/src/cli/utils/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ interface RoboCompileOptions {
files?: string[]
parallel?: number
paths?: Record<string, string[]>
plugin?: boolean
}

export async function compile(options?: RoboCompileOptions) {
Expand Down Expand Up @@ -174,6 +175,14 @@ export async function compile(options?: RoboCompileOptions) {
logger.debug(`Copying additional non-TypeScript files from ${srcDir} to ${distDir}...`)
await copyDir(srcDir, distDir, ['.ts', '.tsx'])

// Generate declaration files for plugins
if (options?.plugin) {
const declarationTime = Date.now()
logger.debug(`Generating declaration files for plugins...`)
compileDeclarationFiles()
logger.debug(`Generated declaration files in ${Date.now() - declarationTime}ms`)
}

logger.debug(`Compiled successfully!`)
return Date.now() - startTime
}
Expand All @@ -198,3 +207,37 @@ async function copyDir(src: string, dest: string, excludeExtensions: string[] =
}
}
}

function compileDeclarationFiles() {
// Define the compiler options specifically for declaration files
const options: CompilerOptions = {
rootDir: 'src',
outDir: '.robo/build',
declaration: true,
emitDeclarationOnly: true,
skipLibCheck: true,
noEmit: false
}

// Emit the declaration files
const fileNames = ts.sys.readDirectory('src', ['.ts', '.tsx'])
const program = ts.createProgram(fileNames, options)
const emitResult = program.emit()

// Collect and display the diagnostics, if any
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics)
allDiagnostics.forEach((diagnostic) => {
if (diagnostic.file) {
const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start!)
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
logger.error(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`)
} else {
logger.error(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'))
}
})

// Exit the process if there were any errors
if (emitResult.emitSkipped) {
process.exit(1)
}
}

0 comments on commit f6168a0

Please sign in to comment.