From 558eec9e905d68c03d68ff27a303002bd4ea1685 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 7 Aug 2018 20:06:24 +0200 Subject: [PATCH 1/4] chore: add index.d.ts --- package-lock.json | 39 ++++++++++++++ package.json | 5 +- rollup.config.js | 4 +- src/index.d.ts | 123 ++++++++++++++++++++++++++++++++++++++++++++ tsconfig.build.json | 2 +- tsconfig.json | 2 +- 6 files changed, 170 insertions(+), 5 deletions(-) create mode 100644 src/index.d.ts diff --git a/package-lock.json b/package-lock.json index 43354eb1..fc578641 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 24de2697..f57c765a 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,7 +82,7 @@ "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", diff --git a/rollup.config.js b/rollup.config.js index bbaf45fa..fdadfe83 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -3,6 +3,7 @@ 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 } = {}) => ({ @@ -10,12 +11,13 @@ const createConfig = ({ output, plugins } = {}) => ({ output, external: ['react', 'prop-types', 'tslib'], plugins: [ - resolve(), typescript({ useTsconfigDeclarationDir: true, tsconfig: './tsconfig.build.json', typescript: require('typescript'), }), + commonjs(), + resolve(), ...plugins, ], }); diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 00000000..f94cc632 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,123 @@ +/// + +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/SwitchCase' { + export interface ISwitchCaseProps extends ICoreProps { + /** Conditional statement. */ + value: any; + } + + /** + * Helper component that is accessed from `Switch` component. + */ + export const SwitchCase: React.SFC; + export default SwitchCase; +} + +declare module 'react-semantic-render/SwitchDefault' { + /** + * Helper component that is accessed from `Switch` component. + */ + const SwitchDefault: React.SFC; + export default SwitchDefault; +} + +declare module 'react-semantic-render/Switch' { + import SwitchCase from 'react-semantic-render/SwitchCase'; + import SwitchDefault from 'react-semantic-render/SwitchDefault'; + + interface ISwitchProps { + /** Conditional statement. */ + value: any; + /** Primary content. */ + children: React.ReactNode; + } + + 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 index 888d4d2d..2f8b08b3 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -2,7 +2,7 @@ "extends": "./tsconfig.json", "compilerOptions": { "removeComments": false, - "declaration": true, + "declaration": false, "declarationDir": "lib/types" } } diff --git a/tsconfig.json b/tsconfig.json index 62188252..d9501196 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "baseUrl": ".", "jsx": "react", - "lib": ["dom", "es2015"], + "lib": ["dom", "es2015", "es2016", "es2017"], "module": "commonjs", "moduleResolution": "node", "outDir": "lib/", From 0d4b51de31d5008e38faae8a7a72a99214bf704a Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 7 Aug 2018 21:38:00 +0200 Subject: [PATCH 2/4] feat: add code splitting (#16) --- jest.config.js | 7 ++++++- package.json | 2 +- rollup.config.js | 22 +++++++++++++++++++--- src/index.d.ts | 4 ++-- tsconfig.build.json | 8 -------- tsconfig.jest.json | 6 ++++++ tsconfig.json | 2 +- 7 files changed, 35 insertions(+), 16 deletions(-) delete mode 100644 tsconfig.build.json create mode 100644 tsconfig.jest.json 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.json b/package.json index 8aa02306..d559832f 100644 --- a/package.json +++ b/package.json @@ -87,7 +87,7 @@ "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 fdadfe83..28202267 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -6,20 +6,20 @@ 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: [ typescript({ - useTsconfigDeclarationDir: true, - tsconfig: './tsconfig.build.json', + tsconfig: './tsconfig.json', typescript: require('typescript'), }), commonjs(), resolve(), ...plugins, ], + ...restConfig, }); export default [ @@ -31,4 +31,20 @@ 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', + ], + output: { + dir: 'lib', + format: 'cjs', + exports: 'named', + }, + plugins: [uglify()], + }), ]; diff --git a/src/index.d.ts b/src/index.d.ts index f94cc632..ee9485cb 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -85,7 +85,7 @@ declare module 'react-semantic-render/SwitchDefault' { /** * Helper component that is accessed from `Switch` component. */ - const SwitchDefault: React.SFC; + export const SwitchDefault: React.SFC; export default SwitchDefault; } @@ -119,5 +119,5 @@ declare module 'react-semantic-render' { import ShowAsync from 'react-semantic-render/ShowAsync'; import Switch from 'react-semantic-render/Switch'; - export { List, Show, ShowAsync, Switch }; + export default { List, Show, ShowAsync, Switch }; } diff --git a/tsconfig.build.json b/tsconfig.build.json deleted file mode 100644 index 2f8b08b3..00000000 --- a/tsconfig.build.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "removeComments": false, - "declaration": false, - "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 d9501196..41ce60f1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "baseUrl": ".", "jsx": "react", "lib": ["dom", "es2015", "es2016", "es2017"], - "module": "commonjs", + "module": "es2015", "moduleResolution": "node", "outDir": "lib/", "target": "es5", From 679634cdd056ff2982ed193a4aa852869c0126a2 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 7 Aug 2018 22:08:44 +0200 Subject: [PATCH 3/4] fix: remove default export in index.d.ts --- rollup.config.js | 2 ++ src/index.d.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/rollup.config.js b/rollup.config.js index 28202267..7ebac2e7 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -39,6 +39,8 @@ export default [ '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', diff --git a/src/index.d.ts b/src/index.d.ts index ee9485cb..f96d9f36 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -119,5 +119,5 @@ declare module 'react-semantic-render' { import ShowAsync from 'react-semantic-render/ShowAsync'; import Switch from 'react-semantic-render/Switch'; - export default { List, Show, ShowAsync, Switch }; + export { List, Show, ShowAsync, Switch }; } From 8f8fb057f73cacdb87993eec4804af3b4aeea943 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 7 Aug 2018 22:25:47 +0200 Subject: [PATCH 4/4] fix(types): fix types for Switch component --- src/index.d.ts | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index f96d9f36..6cd5024f 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -68,8 +68,15 @@ declare module 'react-semantic-render/ShowAsync' { export default ShowAsync; } -declare module 'react-semantic-render/SwitchCase' { - export interface ISwitchCaseProps extends ICoreProps { +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; } @@ -77,28 +84,12 @@ declare module 'react-semantic-render/SwitchCase' { /** * Helper component that is accessed from `Switch` component. */ - export const SwitchCase: React.SFC; - export default SwitchCase; -} + const SwitchCase: React.SFC; -declare module 'react-semantic-render/SwitchDefault' { /** * Helper component that is accessed from `Switch` component. */ - export const SwitchDefault: React.SFC; - export default SwitchDefault; -} - -declare module 'react-semantic-render/Switch' { - import SwitchCase from 'react-semantic-render/SwitchCase'; - import SwitchDefault from 'react-semantic-render/SwitchDefault'; - - interface ISwitchProps { - /** Conditional statement. */ - value: any; - /** Primary content. */ - children: React.ReactNode; - } + const SwitchDefault: React.SFC; type SwitchComponent = React.SFC & { Case?: typeof SwitchCase;