-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate declaration files (#4)
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
- Loading branch information
Showing
12 changed files
with
214 additions
and
20 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { CompilerOptions, CompilerHost } from 'typescript' | ||
|
||
const compilerOptions: CompilerOptions = { | ||
allowJs: true, | ||
declaration: true, | ||
incremental: true, | ||
skipLibCheck: true, | ||
emitDeclarationOnly: true | ||
} | ||
|
||
let _ts: typeof import('typescript') | ||
async function getTs () { | ||
if (!_ts) { | ||
try { | ||
_ts = await import('typescript') | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.warn('[mkdist] Could not load `typescript` for generating types. Do you have it installed?') | ||
throw err | ||
} | ||
} | ||
return _ts | ||
} | ||
|
||
const vfs = new Map<string, string>() | ||
let _tsHost: CompilerHost | ||
async function getTsHost () { | ||
if (!_tsHost) { | ||
const ts = await getTs() | ||
_tsHost = ts.createCompilerHost!(compilerOptions) | ||
} | ||
|
||
// Use virtual filesystem | ||
_tsHost.writeFile = (fileName: string, declaration: string) => { | ||
vfs.set(fileName, declaration) | ||
} | ||
const _readFile = _tsHost.readFile | ||
_tsHost.readFile = (filename) => { | ||
if (vfs.has(filename)) { return vfs.get(filename) } | ||
return _readFile(filename) | ||
} | ||
|
||
return _tsHost | ||
} | ||
|
||
export async function getDeclaration (contents: string, filename = '_input.ts') { | ||
const dtsFilename = filename.replace(/\.(ts|js)$/, '.d.ts') | ||
if (vfs.has(dtsFilename)) { | ||
return vfs.get(dtsFilename) | ||
} | ||
try { | ||
const ts = await getTs() | ||
const host = await getTsHost() | ||
if (vfs.has(filename)) { | ||
throw new Error('Race condition for generating ' + filename) | ||
} | ||
vfs.set(filename, contents) | ||
const program = ts.createProgram!([filename], compilerOptions, host) | ||
await program.emit() | ||
const result = vfs.get(dtsFilename) | ||
vfs.delete(filename) | ||
return result | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`Could not generate declaration file for ${filename}:`, err) | ||
return '' | ||
} | ||
} |
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,31 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`createLoader jsLoader will generate dts file (.js) 1`] = ` | ||
Object { | ||
"contents": "declare const _default: 42; | ||
export default _default; | ||
", | ||
"extension": ".d.ts", | ||
"path": "test.js", | ||
} | ||
`; | ||
|
||
exports[`createLoader jsLoader will generate dts file (.ts) 1`] = ` | ||
Object { | ||
"contents": "declare const _default: 42; | ||
export default _default; | ||
", | ||
"extension": ".d.ts", | ||
"path": "test.ts", | ||
} | ||
`; | ||
|
||
exports[`createLoader vueLoader will generate dts file 1`] = ` | ||
Object { | ||
"contents": "declare const _default: 42; | ||
export default _default; | ||
", | ||
"extension": ".d.ts", | ||
"path": "test.vue.ts", | ||
} | ||
`; |
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