add typings for @loadable/server

This commit is contained in:
Martynas Kadiša 2019-01-04 19:54:16 +02:00
parent 6a7deb65b8
commit 742f59c19a
4 changed files with 190 additions and 0 deletions

86
types/loadable__server/index.d.ts vendored Normal file
View File

@ -0,0 +1,86 @@
// Type definitions for @loadable/server 5.2
// Project: https://github.com/smooth-code/loadable-components
// Definitions by: Martynas Kadiša <https://github.com/martynaskadisa>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
import { ComponentType, ReactElement, Component } from 'react';
export type ChunkExtractorOptions = {
/**
* Webpack entrypoints to load (default to `["main"]`)
*/
entrypoints?: string | string[];
/**
* Optional output path (only for `requireEntrypoint`)
*/
outputPath?: string;
} & ({
/**
* Stats file path generated using `@loadable/webpack-plugin`
*/
statsFile: string;
} | {
/**
* Stats generated using `@loadable/webpack-plugin`.
*/
stats: object;
});
/**
* Used to collect chunks server-side and get them as script tags or script elements
*/
export class ChunkExtractor {
constructor(options: ChunkExtractorOptions);
/**
* Wrap your application in a `ChunkExtractorManager`
*/
collectChunks(
/**
* JSX element that will be wrapped in `ChunkExtractorManager`
*/
element: JSX.Element
): JSX.Element;
/**
* Require the entrypoint of your application as a commonjs module.
*/
requireEntrypoint(name?: string): { default: ComponentType };
/**
* Get scripts as a string of `<script>` tags
*/
getScriptTags(): string[];
/**
* Get scripts as an array of React `<script>` elements.
*/
getScriptElements(): Array<ReactElement<{}>>;
/**
* Get "prefetch" and "preload" links as a string of `<link>` tags
*/
getLinkTags(): string[];
/**
* Get "prefetch" and "preload" links as an array of React `<link>` elements
*/
getLinkElements(): Array<ReactElement<{}>>;
/**
* Get style links as a string of `<link>` tags
*/
getStyleTags(): string[];
/**
* Get style links as an array of React `<link>` elements
*/
getStyleElements(): Array<ReactElement<{}>>;
}
export interface ChunkExtractorManagerProps {
extractor: ChunkExtractor;
}
export class ChunkExtractorManager extends Component<ChunkExtractorManagerProps> {}

View File

@ -0,0 +1,74 @@
import * as React from 'react';
import { ChunkExtractor } from '@loadable/server';
// Should be satisfied by `stats` or `statsFile`
new ChunkExtractor({ stats: {} });
new ChunkExtractor({ statsFile: './path/to/stats' });
const {
collectChunks,
getLinkElements,
getLinkTags,
getScriptElements,
getScriptTags,
getStyleElements,
getStyleTags,
requireEntrypoint
} = new ChunkExtractor({ stats: {} });
// collectChunks
{
// Should accept jsx as first argument
collectChunks(<div>Test</div>);
// Should return jsx
const jsx: JSX.Element = collectChunks(<div>Test</div>);
}
// getLinkElements
{
// Should return an array of React elements
const elements: Array<React.ReactElement<{}>> = getLinkElements();
}
// getLinkTags
{
// Should return an arary of strings
const tags: string[] = getLinkTags();
}
// getScriptElements
{
// Should return an array of React elements
const elements: Array<React.ReactElement<{}>> = getScriptElements();
}
// getScriptTags
{
// Should return an arary of strings
const tags: string[] = getScriptTags();
}
// getStyleElements
{
// Should return an array of React elements
const elements: Array<React.ReactElement<{}>> = getStyleElements();
}
// getStyleTags
{
// Should return an arary of strings
const tags: string[] = getStyleTags();
}
// requireEntrypoint
{
// Should work without params
requireEntrypoint();
// Should accept string
requireEntrypoint('main');
// Should return component on default property
const entry: { default: React.ComponentType } = requireEntrypoint();
}

View File

@ -0,0 +1,29 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"jsx": "react",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"paths": {
"@loadable/server": [
"loadable__server"
]
},
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"loadable__server-tests.tsx"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }