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

Commit d066aba

Browse files
improve rollup config (#3)
* improve rollup config * update rollup config
1 parent d2b2a55 commit d066aba

File tree

7 files changed

+201
-61
lines changed

7 files changed

+201
-61
lines changed

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 strictly typed custom hooks.",
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",
10-
"jsnext:main": "index.esm.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",
10+
"jsnext:main": "dist/index.esm.js",
11+
"source": "src/index.tsx",
1112
"types": "dist/index.d.ts",
1213
"sideEffects": false,
1314
"files": [
@@ -17,8 +18,9 @@
1718
"access": "public"
1819
},
1920
"scripts": {
20-
"build": "npm run clean && rollup -c",
2121
"clean": "rimraf dist",
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",
@@ -60,6 +62,7 @@
6062
"@rollup/plugin-commonjs": "^13.0.0",
6163
"@rollup/plugin-json": "^4.1.0",
6264
"@rollup/plugin-node-resolve": "^8.0.1",
65+
"@rollup/plugin-replace": "^2.3.3",
6366
"@testing-library/react": "^10.3.0",
6467
"@testing-library/react-hooks": "^3.3.0",
6568
"@types/jest": "^26.0.0",
@@ -105,7 +108,7 @@
105108
"*.{js,ts,tsx}": [
106109
"npm run lint:fix"
107110
],
108-
"*.{md,json}": [
111+
"*.{md,json,yml}": [
109112
"prettier --write"
110113
]
111114
}

rollup.config.js

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,43 @@
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 = 'ReactHookFormStrictTyped';
6+
const options = [
117
{
12-
input: 'src/index.tsx',
13-
output: {
14-
name: 'ReactHookFormStrictlyTyped',
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,
6040
},
6141
];
42+
43+
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.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+
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: 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');

yarn.lock

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,14 @@
872872
is-module "^1.0.0"
873873
resolve "^1.14.2"
874874

875+
"@rollup/plugin-replace@^2.3.3":
876+
version "2.3.3"
877+
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.3.tgz#cd6bae39444de119f5d905322b91ebd4078562e7"
878+
integrity sha512-XPmVXZ7IlaoWaJLkSCDaa0Y6uVo5XQYHhiMFzOd5qSv5rE+t/UJToPIOE56flKIxBFQI27ONsxb7dqHnwSsjKQ==
879+
dependencies:
880+
"@rollup/pluginutils" "^3.0.8"
881+
magic-string "^0.25.5"
882+
875883
"@rollup/pluginutils@^3.0.8", "@rollup/pluginutils@^3.0.9":
876884
version "3.1.0"
877885
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b"
@@ -5268,7 +5276,7 @@ macos-release@^2.2.0:
52685276
resolved "https://registry.yarnpkg.com/macos-release/-/macos-release-2.3.0.tgz#eb1930b036c0800adebccd5f17bc4c12de8bb71f"
52695277
integrity sha512-OHhSbtcviqMPt7yfw5ef5aghS2jzFVKEFyCJndQt2YpSQ9qRVSEv2axSJI1paVThEu+FFGs584h/1YhxjVqajA==
52705278

5271-
magic-string@^0.25.2:
5279+
magic-string@^0.25.2, magic-string@^0.25.5:
52725280
version "0.25.7"
52735281
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
52745282
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==

0 commit comments

Comments
 (0)