Skip to content

Commit 1064bee

Browse files
improve rollup config (#37)
1 parent 25a7748 commit 1064bee

File tree

9 files changed

+194
-74
lines changed

9 files changed

+194
-74
lines changed

index.js

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

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"description": "React Hook Form dev tool to help debugging forms",
55
"main": "dist/index.js",
66
"module": "dist/index.esm.js",
7-
"jsnext:main": "index.esm.js",
8-
"umd:main": "dist/index.umd.min.js",
9-
"unpkg": "dist/index.umd.min.js",
10-
"jsdelivr": "dist/index.umd.min.js",
7+
"umd:main": "dist/index.umd.development.js",
8+
"unpkg": "dist/index.umd.development.js",
9+
"jsdelivr": "dist/index.umd.development.js",
10+
"jsnext:main": "dist/index.esm.js",
11+
"source": "src/index.tsx",
1112
"types": "dist/index.d.ts",
1213
"sideEffects": false,
1314
"files": [
@@ -18,7 +19,8 @@
1819
},
1920
"scripts": {
2021
"clean": "rimraf dist",
21-
"build": "npm run clean && rollup -c",
22+
"prebuild": "npm run clean",
23+
"build": "node rollup/writeCjsEntryFile.js && rollup -c",
2224
"lint": "eslint '**/*.{js,ts,tsx}'",
2325
"lint:fix": "npm run lint -- --fix",
2426
"lint:types": "tsc --noEmit",
@@ -62,6 +64,7 @@
6264
"@rollup/plugin-commonjs": "^13.0.0",
6365
"@rollup/plugin-json": "^4.1.0",
6466
"@rollup/plugin-node-resolve": "^8.1.0",
67+
"@rollup/plugin-replace": "^2.3.3",
6568
"@testing-library/react": "^10.0.4",
6669
"@types/jest": "^25.2.3",
6770
"@types/react": "^16.9.35",
@@ -78,7 +81,7 @@
7881
"prettier": "^2.0.5",
7982
"react": "^16.13.1",
8083
"react-dom": "^16.13.1",
81-
"react-hook-form": "^5.7.2",
84+
"react-hook-form": "^6.0.0",
8285
"rimraf": "^3.0.2",
8386
"rollup": "^2.10.7",
8487
"rollup-plugin-peer-deps-external": "^2.2.2",

rollup.config.js

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,29 @@
1-
import external from 'rollup-plugin-peer-deps-external';
2-
import json from '@rollup/plugin-json';
3-
import typescript from 'rollup-plugin-typescript2';
4-
import commonjs from '@rollup/plugin-commonjs';
5-
import resolve from '@rollup/plugin-node-resolve';
6-
import sourcemaps from 'rollup-plugin-sourcemaps';
7-
import { terser } from 'rollup-plugin-terser';
1+
import { createRollupConfig } from './rollup/createRollupConfig';
82
import pkg from './package.json';
93

10-
export default [
4+
const name = 'index';
5+
const umdName = 'ReactHookFormDevTools';
6+
const options = [
117
{
12-
input: 'src/index.tsx',
13-
output: {
14-
name: 'ReactHookFormDevTools',
15-
file: pkg.unpkg,
16-
format: 'umd',
17-
sourcemap: true,
18-
globals: {
19-
react: 'React',
20-
'react-dom': 'ReactDOM',
21-
'react-hook-form': 'ReactHookForm',
22-
},
23-
},
24-
plugins: [
25-
external(),
26-
json(),
27-
typescript({
28-
clean: true,
29-
}),
30-
commonjs(),
31-
resolve(),
32-
sourcemaps(),
33-
terser(),
34-
],
8+
name,
9+
umdName,
10+
format: 'cjs',
11+
env: 'development',
12+
input: pkg.source,
3513
},
3614
{
37-
input: 'src/index.tsx',
38-
output: [
39-
{
40-
file: pkg.main,
41-
format: 'cjs',
42-
sourcemap: true,
43-
},
44-
{
45-
file: pkg.module,
46-
format: 'es',
47-
sourcemap: true,
48-
},
49-
],
50-
plugins: [
51-
external(),
52-
json(),
53-
typescript({
54-
clean: true,
55-
}),
56-
commonjs(),
57-
resolve(),
58-
sourcemaps(),
59-
],
15+
name,
16+
umdName,
17+
format: 'esm',
18+
input: pkg.source,
19+
},
20+
{
21+
name,
22+
umdName,
23+
format: 'umd',
24+
env: 'development',
25+
input: pkg.source,
6026
},
6127
];
28+
29+
export default options.map((option) => createRollupConfig(option));

rollup/createRollupConfig.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import ts from 'typescript';
2+
import path from 'path';
3+
import external from 'rollup-plugin-peer-deps-external';
4+
import json from '@rollup/plugin-json';
5+
import replace from '@rollup/plugin-replace';
6+
import typescript from 'rollup-plugin-typescript2';
7+
import resolve from '@rollup/plugin-node-resolve';
8+
import commonjs from '@rollup/plugin-commonjs';
9+
import sourcemaps from 'rollup-plugin-sourcemaps';
10+
import { terser } from 'rollup-plugin-terser';
11+
import { safePackageName } from './safePackageName';
12+
import { pascalcase } from './pascalcase';
13+
import pkg from '../package.json';
14+
15+
export function createRollupConfig(options) {
16+
const name = options.name || safePackageName(pkg.name);
17+
const umdName = options.undName || pascalcase(safePackageName(pkg.name));
18+
const shouldMinify = options.minify || options.env === 'production';
19+
const tsconfigPath = options.tsconfig || 'tsconfig.json';
20+
const tsconfigJSON = ts.readConfigFile(tsconfigPath, ts.sys.readFile).config;
21+
const tsCompilerOptions = ts.parseJsonConfigFileContent(
22+
tsconfigJSON,
23+
ts.sys,
24+
'./',
25+
).options;
26+
27+
const outputName = [
28+
path.join(tsCompilerOptions.outDir, name),
29+
options.formatName || options.format,
30+
options.env,
31+
shouldMinify ? 'min' : '',
32+
'js',
33+
]
34+
.filter(Boolean)
35+
.join('.');
36+
37+
return {
38+
input: options.input,
39+
output: {
40+
file: outputName,
41+
format: options.format,
42+
name: umdName,
43+
sourcemap: true,
44+
globals: {
45+
react: 'React',
46+
'react-dom': 'ReactDOM',
47+
'react-hook-form': 'ReactHookForm',
48+
},
49+
exports: 'named',
50+
},
51+
plugins: [
52+
external({
53+
includeDependencies: options.format !== 'umd',
54+
}),
55+
json(),
56+
typescript({
57+
tsconfig: options.tsconfig,
58+
clean: true,
59+
}),
60+
resolve(),
61+
options.format === 'umd' &&
62+
commonjs({
63+
include: /\/node_modules\//,
64+
}),
65+
options.env !== undefined &&
66+
replace({
67+
'process.env.NODE_ENV': JSON.stringify(options.env),
68+
}),
69+
sourcemaps(),
70+
shouldMinify &&
71+
terser({
72+
output: { comments: false },
73+
compress: {
74+
drop_console: true,
75+
},
76+
}),
77+
options.plugins,
78+
],
79+
};
80+
}

rollup/pascalcase.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const titlecase = (input) => input[0].toLocaleUpperCase() + input.slice(1);
2+
3+
export const pascalcase = (value) => {
4+
if (value === null || value === void 0) {
5+
return '';
6+
}
7+
if (typeof value.toString !== 'function') {
8+
return '';
9+
}
10+
11+
let input = value.toString().trim();
12+
if (input === '') {
13+
return '';
14+
}
15+
if (input.length === 1) {
16+
return input.toLocaleUpperCase();
17+
}
18+
19+
let match = input.match(/[a-zA-Z0-9]+/g);
20+
if (match) {
21+
return match.map((m) => titlecase(m)).join('');
22+
}
23+
24+
return input;
25+
};

rollup/safePackageName.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const safePackageName = (name) =>
2+
name
3+
.toLowerCase()
4+
.replace(/(^@.*\/)|((^[^a-zA-Z]+)|[^\w.-])|([^a-zA-Z0-9]+$)/g, '');

rollup/writeCjsEntryFile.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
const ts = require('typescript');
3+
const fs = require('fs-extra');
4+
const path = require('path');
5+
const pkg = require('../package.json');
6+
7+
function writeCjsEntryFile(
8+
name = pkg.name,
9+
formatName = 'cjs',
10+
tsconfig = 'tsconfig.json',
11+
) {
12+
const baseLine = `module.exports = require('./${name}`;
13+
const contents = `
14+
'use strict'
15+
16+
if (process.env.NODE_ENV === 'production') {
17+
module.exports = {
18+
DevTool: () => null,
19+
};
20+
} else {
21+
${baseLine}.${formatName}.development.js')
22+
}
23+
`;
24+
25+
const tsconfigJSON = ts.readConfigFile(tsconfig, ts.sys.readFile).config;
26+
const tsCompilerOptions = ts.parseJsonConfigFileContent(
27+
tsconfigJSON,
28+
ts.sys,
29+
'./',
30+
).options;
31+
32+
const filename =
33+
formatName === 'cjs'
34+
? [name, 'js'].join('.')
35+
: [name, formatName, 'js'].join('.');
36+
37+
return fs.outputFile(path.join(tsCompilerOptions.outDir, filename), contents);
38+
}
39+
40+
writeCjsEntryFile('index');

src/formStateTable.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import colors from './colors';
22
import * as React from 'react';
3-
import { FormStateProxy } from 'react-hook-form';
43
import { Button, paraGraphDefaultStyle } from './styled';
54

65
type Props = {
7-
formState: FormStateProxy<Record<string, any>>;
6+
formState: any;
87
showFormState: boolean;
98
setShowFormState: (payload: boolean) => void;
109
};

yarn.lock

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,14 @@
621621
is-module "^1.0.0"
622622
resolve "^1.14.2"
623623

624+
"@rollup/plugin-replace@^2.3.3":
625+
version "2.3.3"
626+
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.3.tgz#cd6bae39444de119f5d905322b91ebd4078562e7"
627+
integrity sha512-XPmVXZ7IlaoWaJLkSCDaa0Y6uVo5XQYHhiMFzOd5qSv5rE+t/UJToPIOE56flKIxBFQI27ONsxb7dqHnwSsjKQ==
628+
dependencies:
629+
"@rollup/pluginutils" "^3.0.8"
630+
magic-string "^0.25.5"
631+
624632
"@rollup/pluginutils@^3.0.8":
625633
version "3.0.10"
626634
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.0.10.tgz#a659b9025920378494cd8f8c59fbf9b3a50d5f12"
@@ -3416,7 +3424,7 @@ loose-envify@^1.1.0, loose-envify@^1.4.0:
34163424
dependencies:
34173425
js-tokens "^3.0.0 || ^4.0.0"
34183426

3419-
magic-string@^0.25.2:
3427+
magic-string@^0.25.2, magic-string@^0.25.5:
34203428
version "0.25.7"
34213429
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
34223430
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==
@@ -4041,10 +4049,10 @@ react-dom@^16.13.1:
40414049
prop-types "^15.6.2"
40424050
scheduler "^0.19.1"
40434051

4044-
react-hook-form@^5.7.2:
4045-
version "5.7.2"
4046-
resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-5.7.2.tgz#a84e259e5d37dd30949af4f79c4dac31101b79ac"
4047-
integrity sha512-bJvY348vayIvEUmSK7Fvea/NgqbT2racA2IbnJz/aPlQ3GBtaTeDITH6rtCa6y++obZzG6E3Q8VuoXPir7QYUg==
4052+
react-hook-form@^6.0.0:
4053+
version "6.0.0"
4054+
resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-6.0.0.tgz#b51faa638aadfa3d4d9758dc1f8d0f67c2b1aa97"
4055+
integrity sha512-qFUMMnAMnZ84mcMFu/UZ6zkuEUQg/dAKdb9/wbjmOJ/KOgrobTx15aMGPhZyemzk6yLHwG7NmJCEOusRTV9UMg==
40484056

40494057
react-is@^16.12.0, react-is@^16.8.1, react-is@^16.8.4:
40504058
version "16.12.0"

0 commit comments

Comments
 (0)