diff --git a/jest.config.js b/jest.config.js index c68f02c8..d88e86a4 100644 --- a/jest.config.js +++ b/jest.config.js @@ -14,5 +14,10 @@ module.exports = { statements: 90, }, }, - collectCoverageFrom: ['src/@(components|utils)/**/*.@(ts|tsx)'] + collectCoverageFrom: ['src/@(components|utils)/**/*.@(ts|tsx)'], + globals: { + 'ts-jest': { + 'tsConfigFile': './tsconfig.jest.json', + }, + }, }; diff --git a/package-lock.json b/package-lock.json index 02e65621..4a1b09ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3935,6 +3935,12 @@ "integrity": "sha1-De4/7TH81GlhjOc0IJn8GvoL2xM=", "dev": true }, + "estree-walker": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-0.5.2.tgz", + "integrity": "sha512-XpCnW/AE10ws/kDAs37cngSkvgIR8aN3G0MS85m7dUpuK2EREo9VJ00uvw6Dg/hXEpfsE1I1TvJOJr+Z+TL+ig==", + "dev": true + }, "esutils": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.2.tgz", @@ -10995,6 +11001,29 @@ "@types/node": "*" } }, + "rollup-plugin-commonjs": { + "version": "9.1.4", + "resolved": "https://registry.npmjs.org/rollup-plugin-commonjs/-/rollup-plugin-commonjs-9.1.4.tgz", + "integrity": "sha512-dpPb6QxvEMG35Eat1yFbpVcuYWE33D2LZK8q2BlSWIBpjXiX2uaqCEMf9czqFChFsfewsA2c2eEoROTepEmyng==", + "dev": true, + "requires": { + "estree-walker": "^0.5.1", + "magic-string": "^0.22.4", + "resolve": "^1.5.0", + "rollup-pluginutils": "^2.0.1" + }, + "dependencies": { + "resolve": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.8.1.tgz", + "integrity": "sha512-AicPrAC7Qu1JxPCZ9ZgCZlY35QgFnNqc+0LtbRNxnVw4TXvjQ72wnuL9JQcEBgXkI9JM8MsT9kaQoHcpCRJOYA==", + "dev": true, + "requires": { + "path-parse": "^1.0.5" + } + } + } + }, "rollup-plugin-filesize": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/rollup-plugin-filesize/-/rollup-plugin-filesize-4.0.1.tgz", @@ -11125,6 +11154,16 @@ } } }, + "rollup-pluginutils": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/rollup-pluginutils/-/rollup-pluginutils-2.3.1.tgz", + "integrity": "sha512-JZS8aJMHEHhqmY2QVPMXwKP6lsD1ShkrcGYjhAIvqKKdXQyPHw/9NF0tl3On/xOJ4ACkxfeG7AF+chfCN1NpBg==", + "dev": true, + "requires": { + "estree-walker": "^0.5.2", + "micromatch": "^2.3.11" + } + }, "rst-selector-parser": { "version": "2.2.3", "resolved": "https://registry.npmjs.org/rst-selector-parser/-/rst-selector-parser-2.2.3.tgz", diff --git a/package.json b/package.json index 66941224..d559832f 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ }, "main": "index.cjs.js", "module": "index.esm.js", - "types": "types/index.d.ts", + "types": "index.d.ts", "private": false, "license": "MIT", "keywords": [ @@ -61,6 +61,7 @@ "react-styleguidist": "^7.1.3", "react": "^16.4.1", "rimraf": "^2.6.2", + "rollup-plugin-commonjs": "^9.1.4", "rollup-plugin-filesize": "^4.0.1", "rollup-plugin-node-resolve": "^3.3.0", "rollup-plugin-terser": "^1.0.1", @@ -81,12 +82,12 @@ "clean:build": "rimraf ./lib && rimraf ./build", "contributors:add": "all-contributors add", "contributors:generate": "all-contributors generate", - "copy:build": "cp package.json lib/ && cp README.md lib/ && cp LICENSE lib/", + "copy:build": "cp package.json lib/ && cp README.md lib/ && cp LICENSE lib/ && cp src/index.d.ts lib/", "deploy": "npm run build && cd lib && npm publish && cd ..", "docs:server": "styleguidist server", "docs": "styleguidist build", "lint:fix": "npm run lint:write --fix \"src/**/*.{ts,tsx}\"", - "lint:write": "tslint -p tsconfig.build.json -c tslint.json", + "lint:write": "tslint -p tsconfig.json -c tslint.json", "lint": "npm run lint:write \"src/**/*.{ts,tsx}\"", "prettier:write": "prettier -c .prettierrc --write", "prettier": "npm run prettier:write \"src/**/*.{ts,tsx}\"", diff --git a/rollup.config.js b/rollup.config.js index bbaf45fa..7ebac2e7 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -3,21 +3,23 @@ import { uglify } from 'rollup-plugin-uglify'; import { terser } from 'rollup-plugin-terser'; import resolve from 'rollup-plugin-node-resolve'; import filesize from 'rollup-plugin-filesize'; +import commonjs from 'rollup-plugin-commonjs'; import pkg from './package.json'; -const createConfig = ({ output, plugins } = {}) => ({ +const createConfig = ({ output, plugins, ...restConfig } = {}) => ({ input: 'src/index.ts', output, external: ['react', 'prop-types', 'tslib'], plugins: [ - resolve(), typescript({ - useTsconfigDeclarationDir: true, - tsconfig: './tsconfig.build.json', + tsconfig: './tsconfig.json', typescript: require('typescript'), }), + commonjs(), + resolve(), ...plugins, ], + ...restConfig, }); export default [ @@ -29,4 +31,22 @@ export default [ output: { file: `lib/${pkg.module}`, format: 'esm' }, plugins: [terser(), filesize()], }), + createConfig({ + experimentalCodeSplitting: true, + optimizeChunks: true, + input: [ + 'src/components/List/List.tsx', + 'src/components/Show/Show.tsx', + 'src/components/ShowAsync/ShowAsync.tsx', + 'src/components/Switch/Switch.tsx', + 'src/components/Switch/SwitchCase.tsx', + 'src/components/Switch/SwitchDefault.tsx', + ], + output: { + dir: 'lib', + format: 'cjs', + exports: 'named', + }, + plugins: [uglify()], + }), ]; diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 00000000..6cd5024f --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,114 @@ +/// + +interface ICoreProps { + /** Shorthand for primary content. */ + render?: () => React.ReactNode; + + /** Primary content. */ + children?: any; +} + +declare module 'react-semantic-render/List' { + interface IListProps { + /** Array to map. */ + items: any[]; + /** Shorthand for primary content. */ + render?: (item?: any, index?: number, array?: any[]) => React.ReactNode; + /** Primary content. */ + children?: any; + } + + /** + * Renders content from specified callback function from either `render` or `children` on each element of `items`. + */ + export const List: React.SFC; + + export default List; +} + +declare module 'react-semantic-render/Show' { + interface IShowProps extends ICoreProps { + /** Conditional statement. */ + when: boolean; + } + + /** + * Renders content if `when` equals true. + */ + export const Show: React.SFC; + + export default Show; +} + +declare module 'react-semantic-render/ShowAsync' { + const initialState: { + status: string; + value: string; + }; + + interface IShowAsyncProps { + /** The promise. */ + when: Promise; + /** Render content when promise is pending. */ + pending?: () => React.ReactNode; + /** Render content when promise is rejected. */ + rejected?: (error?: any) => React.ReactNode; + /** Shorthand for primary content. */ + render?: (value?: any) => React.ReactNode; + /** Primary content. Renders content when promise is resolved. */ + children?: any; + } + + type IShowAsyncState = Readonly; + + /** + * Renders content when status of specified promise is pending, resolved or rejected. + */ + export class ShowAsync extends React.Component {} + export default ShowAsync; +} + +declare module 'react-semantic-render/Switch' { + interface ISwitchProps { + /** Conditional statement. */ + value: any; + /** Primary content. */ + children: React.ReactNode; + } + + interface ISwitchCaseProps extends ICoreProps { + /** Conditional statement. */ + value: any; + } + + /** + * Helper component that is accessed from `Switch` component. + */ + const SwitchCase: React.SFC; + + /** + * Helper component that is accessed from `Switch` component. + */ + const SwitchDefault: React.SFC; + + type SwitchComponent = React.SFC & { + Case?: typeof SwitchCase; + Default?: typeof SwitchDefault; + }; + + /** + * Renders content from first `Switch.Case` that matches `value`, else `Switch.Default` if it exists. + */ + export const Switch: SwitchComponent; + + export default Switch; +} + +declare module 'react-semantic-render' { + import List from 'react-semantic-render/List'; + import Show from 'react-semantic-render/Show'; + import ShowAsync from 'react-semantic-render/ShowAsync'; + import Switch from 'react-semantic-render/Switch'; + + export { List, Show, ShowAsync, Switch }; +} diff --git a/tsconfig.build.json b/tsconfig.build.json deleted file mode 100644 index 888d4d2d..00000000 --- a/tsconfig.build.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "removeComments": false, - "declaration": true, - "declarationDir": "lib/types" - } -} diff --git a/tsconfig.jest.json b/tsconfig.jest.json new file mode 100644 index 00000000..65ffdd49 --- /dev/null +++ b/tsconfig.jest.json @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs" + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 62188252..41ce60f1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,8 +2,8 @@ "compilerOptions": { "baseUrl": ".", "jsx": "react", - "lib": ["dom", "es2015"], - "module": "commonjs", + "lib": ["dom", "es2015", "es2016", "es2017"], + "module": "es2015", "moduleResolution": "node", "outDir": "lib/", "target": "es5",