Skip to content

Commit

Permalink
feat: option to bundle declaration files
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyben committed Nov 24, 2019
1 parent 7453be6 commit 35b6fd9
Show file tree
Hide file tree
Showing 22 changed files with 2,514 additions and 19 deletions.
45 changes: 41 additions & 4 deletions __fixtures__/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
import { randomBytes } from 'crypto'
import * as data from './other/data.json'
// tslint:disable: no-implicit-dependencies

const random = randomBytes(32).toString('utf8')
const isFixture = data.jsonFixture
export * from './module/external'

export { ClassType, GenericDecorator, PromiseLike } from './module/interfaces'
export type KV_1 = { [key: string]: any }

// export { PromiseLike as default } from './module/interfaces'

export * from './module/functions-variables'
export { isAsync as isDeferred } from './module/functions-variables'

import * as classes from './module/classes'
export type repo = classes.UserRepository['user']
// export * from './module/classes'
export type UserC = typeof import('./module/classes').default

export { default } from './module/classes'

import { Myspace, log, MyModule } from './module/namespaces'
export { log as aliasedLog, Myspace as AliasedNamespace, MyModule as AliasedModule }

export * from './module/enums'

import classic = require('./module/export-equals')
// export default classic
export { classic }

// import { DEFAULT_EXTENSIONS } from '@babel/core'
// export default DEFAULT_EXTENSIONS

// export default (arg: string) => {
// return arg.toUpperCase()
// }

import { isNumber as yo } from './module/functions-variables'
export { yo }
// export default yo

// export default function aa(arg: string) {
// return arg.toUpperCase()
// }
56 changes: 56 additions & 0 deletions __fixtures__/src/module/classes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { User, OtherInterface } from './interfaces'

/**
* @public
*/
export default class UserController {
/**
* User repository.
*/
repository: UserRepository

/**
* Creates an instance of UserController
*/
constructor(repository: UserRepository) {
this.repository = repository
}

/**
* @returns a user
*/
getOne(): User {
return {
name: this.repository.names[random(0, 3)],
age: random(20, 50),
gender: 'M',
}
}
}

const globalThis = 'user'

/**
* @public
*/
export class UserRepository {
/**
* Model name
*/
static model = globalThis

names = ['Jeremy', 'Sophie', 'Damien', 'Laetitia']
}

// tslint:disable-next-line: no-empty-interface
export interface UserRepository extends OtherInterface {}

export namespace UserRepository {
export function getModel() {
return UserRepository.model
}
}

function random(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
29 changes: 29 additions & 0 deletions __fixtures__/src/module/enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Status codes
*/
export enum Status {
OK = 200,
Error = 400,
}

export namespace Status {
export function getError() {
return Status.Error
}
}

/**
* content-type
*/
export enum ContentType {
Json = 'application/json',
Html = 'text/html',
}

/**
* HTTP verbs
*/
export const enum Verb {
Get = 'GET',
Post = 'POST',
}
7 changes: 7 additions & 0 deletions __fixtures__/src/module/export-equals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @public
*/
const fooBarBaz = { foo: 1, bar: 2, baz: 3, fooBarBaz: 123 } as const
export = fooBarBaz

// export = { foo: 1, bar: 2, baz: 3 }
25 changes: 25 additions & 0 deletions __fixtures__/src/module/external.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ts from 'typescript'
const emitFlags = ts.EmitFlags
export { emitFlags }

import typescript = require('typescript')
export const options: typescript.TokenFlags = 1

export { run } from 'jest'
export { IncomingHttpHeaders } from 'http'

export * from 'tslint'
export * from 'dns'

import * as utils from 'util'
export { utils }

import { StringDecoder } from 'string_decoder'
export { StringDecoder }

import { OutgoingHttpHeaders } from 'http'
export type OutgoingHeaders = OutgoingHttpHeaders

export type Digest = typeof import('ts-jest').digest

export { randomBytesB } from './namespaces'
98 changes: 98 additions & 0 deletions __fixtures__/src/module/functions-variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// tslint:disable: prefer-const no-var-keyword one-variable-per-declaration only-arrow-functions
import { User } from './interfaces'

/**
* @internal
*/
const isFunction = (obj: any) => {
return typeof obj === 'function'
}

/**
* @internal
*/
function isObject(obj: any) {
return typeof obj === 'object'
}

/**
* Checks if given object is Promise-like.
* @deprecated use isAsync
*/
export function isPromise(obj: any): obj is Promise<any> {
return obj != null && isObject(obj) && isFunction(obj.then)
}

export { isPromise as isAsync }

type KV = {
first: 'first'
[key: string]: string
}

/**
* Checks if given object is Observable-like.
* @see https://github.com/ReactiveX/rxjs/blob/master/src/internal/util/isObservable.ts
* @internal
*/
export const isObservable: (obj: KV) => boolean = (obj) => {
return !!obj && (isFunction(obj.lift) && isFunction(obj.subscribe))
}

export const isObs = isObservable

/**
* @internal
*/
const isNumber = function(obj: any) {
return typeof obj === 'number'
}

/**
* @public
*/
function getUser<T extends KV['first']>(firstname: string | User): User {
return {
name: typeof firstname === 'string' ? firstname : firstname.name,
age: 1,
gender: 'F',
}
}

export { isNumber, getUser }

/**
* Best number in the world
* @beta
*/
let three = 3,
four = () => 4,
five = 5

export { three, four as quatro, four as number4 }

/**
* Best name in the world.
* It' true.
* @alpha
*/
export var name = 'Jeremy'
export type name = 'Jeremy'

/**
* @decorator
* @public
*/
export function OverloadedDecorator(...args: Parameters<ParameterDecorator>): void

export function OverloadedDecorator(): ParameterDecorator

export function OverloadedDecorator(): ParameterDecorator {
return (target, methodKey, index) => undefined
}

export { jsonFixture as fixture } from '../other/data.json'
// import { jsonFixture as fixture } from '../other/data.json'
// export { fixture }
import * as jsonFile from '../other/data.json'
export { jsonFile }
60 changes: 60 additions & 0 deletions __fixtures__/src/module/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { KV } from './namespaces'
import { ExternalModuleReference } from 'typescript'

/**
* Defines a JS class type.
* @typeParam T - type of the instance
* @public
*/
export type ClassType<T = any> = new (...args: any[]) => T

/**
* Defines any type of decorator.
* @public
*/
export type GenericDecorator = (
target: object,
propertyKey?: string | symbol,
descriptorOrIndex?: TypedPropertyDescriptor<any> | number
) => any

export interface PromiseLike<T> {
promise: Promise<T>
}

type Promise<T> = { then: (value: any) => T; catch: (reason: any) => void }

/**
* User model.
* @public
*/
export interface User {
/**
* Name of the user.
*/
name: string

/**
* Age of the user.
*/
age: number

/**
* Gender of the user.
* @defaultValue 'M'
*/
gender?: 'M' | 'F'

keyvalue?: KV

extern?: External
}

type External = ExternalModuleReference

// export type KV = { [key: string]: any }

export interface OtherInterface {
other: string
user: User
}
33 changes: 33 additions & 0 deletions __fixtures__/src/module/namespaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { randomBytes } from 'crypto'
export { randomBytes as randomBytesB }

/**
* public
*/
export const log: Myspace.logger = (message) => console.log(message)

export type KV = { [k: number]: any }

/**
* Container namespace
*/
export namespace Myspace {
/**
* @beta
*/
export type logger = (message: any) => void

/**
* @alpha
*/
export const random = randomBytes(32)
}

/**
* My module
* @alpha
*/
// tslint:disable-next-line: no-internal-module
export module MyModule {
export const obj = { yo: 2 }
}
3 changes: 2 additions & 1 deletion __fixtures__/src/other/data.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"jsonFixture": true
"jsonFixture": null,
"default": 1
}
Loading

0 comments on commit 35b6fd9

Please sign in to comment.