Skip to content

Commit cbcc044

Browse files
committed
update: rollback config to multi export
1 parent ca77d4e commit cbcc044

File tree

7 files changed

+253
-112
lines changed

7 files changed

+253
-112
lines changed

package.json

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
"description": "Functional library to try-catch code that accepts functions and promises",
66
"author": "Casper Engelmann <me@casperengelmann.com>",
77
"type": "module",
8-
"main": "./lib/index.js",
9-
"module": "./lib/index.esm.js",
10-
"jsnext:main": "./lib/index.esm.js",
8+
"main": "./lib/try-catch.js",
9+
"module": "./lib/try-catch.esm.js",
10+
"jsnext:main": "./lib/try-catch.esm.js",
1111
"umd:main": "./lib/try-catch.umd.production.js",
1212
"unpkg": "./lib/try-catch.umd.production.js",
1313
"jsdelivr": "./lib/try-catch.umd.production.js",
1414
"browser": "./lib/try-catch.umd.production.js",
15-
"source": "./src/index.ts",
16-
"types": "./lib/index.d.ts",
15+
"source": "./src/try-catch.ts",
16+
"types": "./lib/try-catch.d.ts",
1717
"files": [
1818
"lib"
1919
],
@@ -31,8 +31,8 @@
3131
"coverage": "jest --coverage --coverageReporters=text-lcov",
3232
"test": "cross-env NODE_ENV=testing jest",
3333
"watch:test": "npm run test --watch",
34-
"prebuild": "tsc -d",
35-
"build": "rollup -c rollup.config.js",
34+
"prebuild": "tsc -d --emitDeclarationOnly",
35+
"build": "rollup -c ./scripts/rollup.config.js",
3636
"prepublishOnly": "npm test"
3737
},
3838
"publishConfig": {
@@ -46,7 +46,7 @@
4646
"bugs": {
4747
"url": "https://github.com/CasperEngl/try-catch/issues"
4848
},
49-
"homepage": "https://refined-github-html-preview.kidonng.workers.dev/CasperEngl/try-catch/raw/master/example/index.html",
49+
"homepage": "https://refined-github-html-preview.kidonng.workers.dev/CasperEngl/try-catch/raw/master/example/try-catch.html",
5050
"keywords": [
5151
"trycatch",
5252
"try catch",
@@ -64,7 +64,11 @@
6464
"@rollup/plugin-replace": "^2.3.4",
6565
"@rollup/plugin-typescript": "^8.1.0",
6666
"@size-limit/preset-small-lib": "^4.9.1",
67+
"@testing-library/react-hooks": "^5.0.3",
6768
"@types/jest": "^26.0.10",
69+
"@types/node": "^14.14.25",
70+
"@types/react": "^17.0.1",
71+
"@types/react-dom": "^17.0.0",
6872
"@typescript-eslint/eslint-plugin": "^4.13.0",
6973
"@typescript-eslint/parser": "^4.13.0",
7074
"axios": "^0.21.1",
@@ -77,6 +81,8 @@
7781
"jest": "^26.4.2",
7882
"lodash": "^4.17.20",
7983
"miragejs": "^0.1.41",
84+
"react": "^17.0.1",
85+
"react-dom": "^17.0.1",
8086
"rollup": "^2.38.5",
8187
"rollup-plugin-cleanup": "^3.2.1",
8288
"rollup-plugin-copy": "^3.3.0",
@@ -86,5 +92,6 @@
8692
"ts-jest": "^26.2.0",
8793
"ts-loader": "^8.0.14",
8894
"typescript": "^4.1.3"
89-
}
95+
},
96+
"peerDependencies": {}
9097
}

rollup.config.js

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

scripts/createRollupConfig.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import typescript from '@rollup/plugin-typescript';
2+
import resolve from '@rollup/plugin-node-resolve';
3+
import cleanup from 'rollup-plugin-cleanup';
4+
import sourcemaps from 'rollup-plugin-sourcemaps';
5+
import copy from 'rollup-plugin-copy';
6+
import { terser } from 'rollup-plugin-terser';
7+
8+
import { omit } from 'lodash';
9+
10+
/**
11+
*
12+
* @param {string} input
13+
* @param {{
14+
* env: string;
15+
* file: string;
16+
* fileDt: string;
17+
* external: string[];
18+
* }} option
19+
*/
20+
export const createRollupConfig = (input, option) => {
21+
const fileDt = option.env
22+
? `${option.file.replace('.js', '')}.${option.env}.d.ts`
23+
: option.file;
24+
25+
return {
26+
...(option.external ? { external: option.external } : null),
27+
input,
28+
output: {
29+
...omit(option, ['env', 'fileDt']),
30+
},
31+
plugins: [
32+
typescript(),
33+
resolve(),
34+
cleanup({
35+
comments: 'none',
36+
}),
37+
sourcemaps(),
38+
option.env === 'production' && terser(),
39+
option.fileDt &&
40+
copy({
41+
targets: [
42+
{
43+
src: option.fileDt,
44+
dest: '.',
45+
rename: fileDt,
46+
},
47+
],
48+
}),
49+
],
50+
};
51+
};

scripts/rollup.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createRollupConfig } from './createRollupConfig';
2+
import { tryCatchOptions } from './tryCatchOptions';
3+
import { useTryCatchOptions } from './useTryCatchOptions';
4+
5+
const tryCatchConfigs = tryCatchOptions.map((option) =>
6+
createRollupConfig('./src/index.ts', option)
7+
);
8+
9+
const useTryCatchConfigs = useTryCatchOptions.map((option) =>
10+
createRollupConfig('./src/react/index.ts', option)
11+
);
12+
13+
export default [...tryCatchConfigs, ...useTryCatchConfigs];

scripts/tryCatchOptions.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pkg from '../package.json';
2+
3+
const name = 'tryCatch';
4+
const fileDt = './lib/try-catch.d.ts';
5+
6+
const option = {
7+
name,
8+
exports: 'named',
9+
fileDt,
10+
sourcemap: true,
11+
};
12+
13+
export const tryCatchOptions = [
14+
{
15+
file: pkg.module,
16+
format: 'es',
17+
env: 'production',
18+
},
19+
{
20+
file: pkg.module,
21+
format: 'es',
22+
env: 'development',
23+
},
24+
{
25+
file: pkg.main,
26+
format: 'cjs',
27+
env: 'production',
28+
},
29+
{
30+
file: pkg.main,
31+
format: 'cjs',
32+
env: 'development',
33+
},
34+
{
35+
file: pkg.browser,
36+
format: 'umd',
37+
},
38+
].map((o) => ({
39+
...option,
40+
...o,
41+
}));

scripts/useTryCatchOptions.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const name = 'useTryCatch';
2+
const fileDt = './lib/react/index.d.ts';
3+
4+
const globals = {
5+
react: 'React',
6+
'react-dom': 'ReactDOM',
7+
};
8+
9+
const option = {
10+
name,
11+
exports: 'named',
12+
file: './lib/react/index.js',
13+
fileDt,
14+
format: 'es',
15+
sourcemap: true,
16+
globals,
17+
external: ['react', 'react-dom', '../try-catch'],
18+
};
19+
20+
export const useTryCatchOptions = [
21+
{
22+
env: 'production',
23+
},
24+
{
25+
env: 'development',
26+
},
27+
].map((o) => ({
28+
...option,
29+
...o,
30+
}));

0 commit comments

Comments
 (0)