Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate declaration files #4

Merged
merged 15 commits into from
Apr 8, 2021
1 change: 1 addition & 0 deletions src/loaders/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export async function getDeclaration (contents: string, path = '_contents.ts') {

return files[path.replace(/\.(ts|js)$/, '.d.ts')]
} catch {
console.warn(`Could not generate declaration file for ${path}. Do you have \`typescript\` installed?`)
danielroe marked this conversation as resolved.
Show resolved Hide resolved
return ''
}
}
31 changes: 31 additions & 0 deletions test/__snapshots__/index.test.ts.snap
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",
}
`;
57 changes: 56 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolve } from 'upath'
import { mkdist } from '../src/make'
import { createLoader } from '../src/loader'
import { vueLoader } from '../src/loaders'
import { jsLoader, vueLoader } from '../src/loaders'

describe('mkdist', () => {
it('mkdist', async () => {
Expand Down Expand Up @@ -74,4 +74,59 @@ describe('createLoader', () => {
})
expect(results).toBeFalsy()
})
it('vueLoader will generate dts file', async () => {
const { loadFile } = createLoader({
loaders: [vueLoader, jsLoader],
emitTypes: true
})
const results = await loadFile({
extension: '.vue',
getContents: () => '<script lang="ts">export default bob = 42 as const</script>',
path: 'test.vue'
})
expect(results![1]).toMatchSnapshot()
})
it('jsLoader will generate dts file (.js)', async () => {
const { loadFile } = createLoader({
loaders: [jsLoader],
emitTypes: true
})
const results = await loadFile({
extension: '.js',
getContents: () => 'export default bob = 42',
path: 'test.js'
})
expect(results![1]).toMatchSnapshot()
})
it('jsLoader will generate dts file (.ts)', async () => {
const { loadFile } = createLoader({
loaders: [jsLoader],
emitTypes: true
})
const results = await loadFile({
extension: '.ts',
getContents: () => 'export default bob = 42 as const',
path: 'test.ts'
})
expect(results![1]).toMatchSnapshot()
})
it('dts loader handles missing typescript dependency', async () => {
jest.mock('typescript', () => {
throw new Error('does not exist')
})
const spy = jest.spyOn(console, 'warn')
const { loadFile } = createLoader({
loaders: [jsLoader],
emitTypes: true
})
const results = await loadFile({
extension: '.ts',
getContents: () => 'export default bob = 42 as const',
path: 'test.ts',
srcPath: 'test.ts'
})
expect(results![1]).toBeFalsy()
expect(spy).toHaveBeenCalledWith('Could not generate declaration file for test.ts. Do you have `typescript` installed?')
jest.clearAllMocks()
})
})