Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit fa45037

Browse files
authored
Add code splitting (#32)
* feat: add code splitting (#16)
1 parent 4816e7e commit fa45037

File tree

8 files changed

+195
-18
lines changed

8 files changed

+195
-18
lines changed

jest.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ module.exports = {
1414
statements: 90,
1515
},
1616
},
17-
collectCoverageFrom: ['src/@(components|utils)/**/*.@(ts|tsx)']
17+
collectCoverageFrom: ['src/@(components|utils)/**/*.@(ts|tsx)'],
18+
globals: {
19+
'ts-jest': {
20+
'tsConfigFile': './tsconfig.jest.json',
21+
},
22+
},
1823
};

package-lock.json

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"main": "index.cjs.js",
1717
"module": "index.esm.js",
18-
"types": "types/index.d.ts",
18+
"types": "index.d.ts",
1919
"private": false,
2020
"license": "MIT",
2121
"keywords": [
@@ -61,6 +61,7 @@
6161
"react-styleguidist": "^7.1.3",
6262
"react": "^16.4.1",
6363
"rimraf": "^2.6.2",
64+
"rollup-plugin-commonjs": "^9.1.4",
6465
"rollup-plugin-filesize": "^4.0.1",
6566
"rollup-plugin-node-resolve": "^3.3.0",
6667
"rollup-plugin-terser": "^1.0.1",
@@ -81,12 +82,12 @@
8182
"clean:build": "rimraf ./lib && rimraf ./build",
8283
"contributors:add": "all-contributors add",
8384
"contributors:generate": "all-contributors generate",
84-
"copy:build": "cp package.json lib/ && cp README.md lib/ && cp LICENSE lib/",
85+
"copy:build": "cp package.json lib/ && cp README.md lib/ && cp LICENSE lib/ && cp src/index.d.ts lib/",
8586
"deploy": "npm run build && cd lib && npm publish && cd ..",
8687
"docs:server": "styleguidist server",
8788
"docs": "styleguidist build",
8889
"lint:fix": "npm run lint:write --fix \"src/**/*.{ts,tsx}\"",
89-
"lint:write": "tslint -p tsconfig.build.json -c tslint.json",
90+
"lint:write": "tslint -p tsconfig.json -c tslint.json",
9091
"lint": "npm run lint:write \"src/**/*.{ts,tsx}\"",
9192
"prettier:write": "prettier -c .prettierrc --write",
9293
"prettier": "npm run prettier:write \"src/**/*.{ts,tsx}\"",

rollup.config.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ import { uglify } from 'rollup-plugin-uglify';
33
import { terser } from 'rollup-plugin-terser';
44
import resolve from 'rollup-plugin-node-resolve';
55
import filesize from 'rollup-plugin-filesize';
6+
import commonjs from 'rollup-plugin-commonjs';
67
import pkg from './package.json';
78

8-
const createConfig = ({ output, plugins } = {}) => ({
9+
const createConfig = ({ output, plugins, ...restConfig } = {}) => ({
910
input: 'src/index.ts',
1011
output,
1112
external: ['react', 'prop-types', 'tslib'],
1213
plugins: [
13-
resolve(),
1414
typescript({
15-
useTsconfigDeclarationDir: true,
16-
tsconfig: './tsconfig.build.json',
15+
tsconfig: './tsconfig.json',
1716
typescript: require('typescript'),
1817
}),
18+
commonjs(),
19+
resolve(),
1920
...plugins,
2021
],
22+
...restConfig,
2123
});
2224

2325
export default [
@@ -29,4 +31,22 @@ export default [
2931
output: { file: `lib/${pkg.module}`, format: 'esm' },
3032
plugins: [terser(), filesize()],
3133
}),
34+
createConfig({
35+
experimentalCodeSplitting: true,
36+
optimizeChunks: true,
37+
input: [
38+
'src/components/List/List.tsx',
39+
'src/components/Show/Show.tsx',
40+
'src/components/ShowAsync/ShowAsync.tsx',
41+
'src/components/Switch/Switch.tsx',
42+
'src/components/Switch/SwitchCase.tsx',
43+
'src/components/Switch/SwitchDefault.tsx',
44+
],
45+
output: {
46+
dir: 'lib',
47+
format: 'cjs',
48+
exports: 'named',
49+
},
50+
plugins: [uglify()],
51+
}),
3252
];

src/index.d.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/// <reference types="react" />
2+
3+
interface ICoreProps {
4+
/** Shorthand for primary content. */
5+
render?: () => React.ReactNode;
6+
7+
/** Primary content. */
8+
children?: any;
9+
}
10+
11+
declare module 'react-semantic-render/List' {
12+
interface IListProps {
13+
/** Array to map. */
14+
items: any[];
15+
/** Shorthand for primary content. */
16+
render?: (item?: any, index?: number, array?: any[]) => React.ReactNode;
17+
/** Primary content. */
18+
children?: any;
19+
}
20+
21+
/**
22+
* Renders content from specified callback function from either `render` or `children` on each element of `items`.
23+
*/
24+
export const List: React.SFC<IListProps>;
25+
26+
export default List;
27+
}
28+
29+
declare module 'react-semantic-render/Show' {
30+
interface IShowProps extends ICoreProps {
31+
/** Conditional statement. */
32+
when: boolean;
33+
}
34+
35+
/**
36+
* Renders content if `when` equals true.
37+
*/
38+
export const Show: React.SFC<IShowProps>;
39+
40+
export default Show;
41+
}
42+
43+
declare module 'react-semantic-render/ShowAsync' {
44+
const initialState: {
45+
status: string;
46+
value: string;
47+
};
48+
49+
interface IShowAsyncProps {
50+
/** The promise. */
51+
when: Promise<any>;
52+
/** Render content when promise is pending. */
53+
pending?: () => React.ReactNode;
54+
/** Render content when promise is rejected. */
55+
rejected?: (error?: any) => React.ReactNode;
56+
/** Shorthand for primary content. */
57+
render?: (value?: any) => React.ReactNode;
58+
/** Primary content. Renders content when promise is resolved. */
59+
children?: any;
60+
}
61+
62+
type IShowAsyncState = Readonly<typeof initialState>;
63+
64+
/**
65+
* Renders content when status of specified promise is pending, resolved or rejected.
66+
*/
67+
export class ShowAsync extends React.Component<IShowAsyncProps, IShowAsyncState> {}
68+
export default ShowAsync;
69+
}
70+
71+
declare module 'react-semantic-render/Switch' {
72+
interface ISwitchProps {
73+
/** Conditional statement. */
74+
value: any;
75+
/** Primary content. */
76+
children: React.ReactNode;
77+
}
78+
79+
interface ISwitchCaseProps extends ICoreProps {
80+
/** Conditional statement. */
81+
value: any;
82+
}
83+
84+
/**
85+
* Helper component that is accessed from `Switch` component.
86+
*/
87+
const SwitchCase: React.SFC<ISwitchCaseProps>;
88+
89+
/**
90+
* Helper component that is accessed from `Switch` component.
91+
*/
92+
const SwitchDefault: React.SFC<ICoreProps>;
93+
94+
type SwitchComponent = React.SFC<ISwitchProps> & {
95+
Case?: typeof SwitchCase;
96+
Default?: typeof SwitchDefault;
97+
};
98+
99+
/**
100+
* Renders content from first `Switch.Case` that matches `value`, else `Switch.Default` if it exists.
101+
*/
102+
export const Switch: SwitchComponent;
103+
104+
export default Switch;
105+
}
106+
107+
declare module 'react-semantic-render' {
108+
import List from 'react-semantic-render/List';
109+
import Show from 'react-semantic-render/Show';
110+
import ShowAsync from 'react-semantic-render/ShowAsync';
111+
import Switch from 'react-semantic-render/Switch';
112+
113+
export { List, Show, ShowAsync, Switch };
114+
}

tsconfig.build.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

tsconfig.jest.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "commonjs"
5+
}
6+
}

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"compilerOptions": {
33
"baseUrl": ".",
44
"jsx": "react",
5-
"lib": ["dom", "es2015"],
6-
"module": "commonjs",
5+
"lib": ["dom", "es2015", "es2016", "es2017"],
6+
"module": "es2015",
77
"moduleResolution": "node",
88
"outDir": "lib/",
99
"target": "es5",

0 commit comments

Comments
 (0)