Skip to content

Commit bd18719

Browse files
authored
Merge pull request #21 from react-hook-form/improve-rollup-config
improve rollup config
2 parents a731798 + 8ddf328 commit bd18719

File tree

8 files changed

+216
-62
lines changed

8 files changed

+216
-62
lines changed

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"description": "React Hook Form error message component",
55
"main": "dist/index.js",
66
"module": "dist/index.esm.js",
7-
"umd:main": "dist/index.umd.min.js",
8-
"unpkg": "dist/index.umd.min.js",
9-
"jsdelivr": "dist/index.umd.min.js",
7+
"umd:main": "dist/index.umd.production.min.js",
8+
"unpkg": "dist/index.umd.production.min.js",
9+
"jsdelivr": "dist/index.umd.production.min.js",
1010
"jsnext:main": "dist/index.esm.js",
11+
"source": "src/index.tsx",
1112
"types": "dist/index.d.ts",
1213
"sideEffects": false,
1314
"files": [
@@ -18,13 +19,13 @@
1819
},
1920
"scripts": {
2021
"clean": "rimraf dist",
21-
"build": "npm run clean && rollup -c",
22-
"lint": "eslint '**/*.{js,ts,tsx}'",
22+
"prebuild": "npm run clean",
23+
"build": "node rollup/writeCjsEntryFile.js && rollup -c",
24+
"lint": "eslint '**/*.{js,ts}'",
2325
"lint:fix": "npm run lint -- --fix",
2426
"lint:types": "tsc --noEmit",
2527
"test": "jest --runInBand",
26-
"test:watch": "jest --watch",
27-
"test:coverage": "jest --coverage",
28+
"test:watch": "npm run test -- --watchAll --coverage",
2829
"postversion": "git push && git push origin v$npm_package_version",
2930
"prepublishOnly": "npm run lint && npm run lint:types && npm test && npm run build"
3031
},
@@ -53,6 +54,7 @@
5354
"@rollup/plugin-commonjs": "^13.0.0",
5455
"@rollup/plugin-json": "^4.1.0",
5556
"@rollup/plugin-node-resolve": "^8.0.1",
57+
"@rollup/plugin-replace": "^2.3.3",
5658
"@testing-library/react": "^10.2.1",
5759
"@testing-library/react-hooks": "^3.2.1",
5860
"@types/jest": "^26.0.0",

rollup.config.js

Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,51 @@
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 = 'ReactHookFormErrorMessage';
6+
const options = [
117
{
12-
input: 'src/index.tsx',
13-
output: {
14-
name: 'ReactHookFormErrorMessage',
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: 'cjs',
18+
env: 'production',
19+
input: pkg.source,
20+
},
21+
{
22+
name,
23+
umdName,
24+
format: 'esm',
25+
input: pkg.source,
26+
},
27+
{
28+
name,
29+
umdName,
30+
format: 'umd',
31+
env: 'development',
32+
input: pkg.source,
33+
},
34+
{
35+
name,
36+
umdName,
37+
format: 'umd',
38+
env: 'production',
39+
input: pkg.source,
40+
},
41+
{
42+
name,
43+
umdName,
44+
format: 'esm',
45+
formatName: 'ie11',
46+
input: pkg.source,
47+
tsconfig: './tsconfig.ie11.json',
6048
},
6149
];
50+
51+
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, callback) {
16+
const name = options.name || safePackageName(pkg.name);
17+
const umdName = options.umdName || 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+
const config = {
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-hook-form': 'ReactHookForm',
47+
},
48+
exports: 'named',
49+
},
50+
plugins: [
51+
external({
52+
includeDependencies: options.format !== 'umd',
53+
}),
54+
json(),
55+
typescript({
56+
tsconfig: options.tsconfig,
57+
clean: true,
58+
}),
59+
resolve(),
60+
options.format === 'umd' &&
61+
commonjs({
62+
include: /\/node_modules\//,
63+
}),
64+
options.env !== undefined &&
65+
replace({
66+
'process.env.NODE_ENV': JSON.stringify(options.env),
67+
}),
68+
sourcemaps(),
69+
shouldMinify &&
70+
terser({
71+
output: { comments: false },
72+
compress: {
73+
drop_console: true,
74+
},
75+
}),
76+
].filter(Boolean),
77+
};
78+
79+
return callback ? callback(config) : config;
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
${baseLine}.${formatName}.production.min.js')
18+
} else {
19+
${baseLine}.${formatName}.development.js')
20+
}
21+
`;
22+
23+
const tsconfigJSON = ts.readConfigFile(tsconfig, ts.sys.readFile).config;
24+
const tsCompilerOptions = ts.parseJsonConfigFileContent(
25+
tsconfigJSON,
26+
ts.sys,
27+
'./',
28+
).options;
29+
30+
const filename =
31+
formatName === 'cjs'
32+
? [name, 'js'].join('.')
33+
: [name, formatName, 'js'].join('.');
34+
35+
return fs.outputFile(path.join(tsCompilerOptions.outDir, filename), contents);
36+
}
37+
38+
writeCjsEntryFile('index');

tsconfig.ie11.json

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

yarn.lock

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,14 @@
505505
is-module "^1.0.0"
506506
resolve "^1.14.2"
507507

508+
"@rollup/plugin-replace@^2.3.3":
509+
version "2.3.3"
510+
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.3.tgz#cd6bae39444de119f5d905322b91ebd4078562e7"
511+
integrity sha512-XPmVXZ7IlaoWaJLkSCDaa0Y6uVo5XQYHhiMFzOd5qSv5rE+t/UJToPIOE56flKIxBFQI27ONsxb7dqHnwSsjKQ==
512+
dependencies:
513+
"@rollup/pluginutils" "^3.0.8"
514+
magic-string "^0.25.5"
515+
508516
"@rollup/pluginutils@^3.0.8", "@rollup/pluginutils@^3.0.9":
509517
version "3.1.0"
510518
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b"
@@ -3212,7 +3220,7 @@ loose-envify@^1.1.0, loose-envify@^1.4.0:
32123220
dependencies:
32133221
js-tokens "^3.0.0 || ^4.0.0"
32143222

3215-
magic-string@^0.25.2:
3223+
magic-string@^0.25.2, magic-string@^0.25.5:
32163224
version "0.25.7"
32173225
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
32183226
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==

0 commit comments

Comments
 (0)