-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eval): support esbuild & typescript loader
- Loading branch information
Showing
11 changed files
with
105 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import esbuild from 'esbuild' | ||
|
||
export async function transform(expr: string) { | ||
try { | ||
const { code } = await esbuild.transform(expr, { | ||
sourcemap: 'inline', | ||
loader: 'ts', | ||
charset: 'utf8', | ||
target: 'es2020', | ||
}) | ||
return code | ||
} catch (e) { | ||
const [{ location, text }] = e.errors as esbuild.Message[] | ||
throw new Error(`${text}\n at stdin:${location.line}:${location.column}`) | ||
} | ||
} | ||
|
||
export function extract(expr: string) { | ||
try { | ||
esbuild.transformSync(expr, { | ||
loader: 'ts', | ||
charset: 'utf8', | ||
target: 'es2020', | ||
}) | ||
} catch (e) { | ||
const [{ location, text }] = e.errors as esbuild.Message[] | ||
if (text === 'Unexpected "}"') { | ||
const sLines = expr.split('\n') | ||
return [...sLines.slice(0, location.line - 1), location.lineText.slice(0, location.column)].join('\n') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import ts from 'typescript' | ||
import json5 from 'json5' | ||
import { promises as fs } from 'fs' | ||
import { WorkerData } from '../worker' | ||
import { resolve } from 'path' | ||
import { Logger } from 'koishi-utils' | ||
|
||
export { extract } from './default' | ||
|
||
const compilerOptions: ts.CompilerOptions = { | ||
inlineSourceMap: true, | ||
module: ts.ModuleKind.ES2020, | ||
target: ts.ScriptTarget.ES2020, | ||
} | ||
|
||
export async function prepare(config: WorkerData) { | ||
const logger = new Logger('eval:loader') | ||
const tsconfigPath = resolve(config.root, 'tsconfig.json') | ||
return fs.readFile(tsconfigPath, 'utf8').then((tsconfig) => { | ||
Object.assign(compilerOptions, json5.parse(tsconfig)) | ||
}, () => { | ||
logger.info('auto generating tsconfig.json...') | ||
return fs.writeFile(tsconfigPath, json5.stringify({ compilerOptions }, null, 2)) | ||
}) | ||
} | ||
|
||
export async function transform(expr: string) { | ||
return ts.transpile(expr, compilerOptions) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters