-
Notifications
You must be signed in to change notification settings - Fork 3
/
rollup.config.js
187 lines (172 loc) · 4.36 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import babel from "rollup-plugin-babel";
import commonjs from "rollup-plugin-commonjs";
import nodeResolve from "rollup-plugin-node-resolve";
import replace from "rollup-plugin-replace";
import { sizeSnapshot } from "rollup-plugin-size-snapshot";
import sourcemaps from "rollup-plugin-sourcemaps";
import { terser } from "rollup-plugin-terser";
import pkg from "./package.json";
const CJS_DEV = "CJS_DEV";
const CJS_PROD = "CJS_PROD";
const ES = "ES";
const UMD_DEV = "UMD_DEV";
const UMD_PROD = "UMD_PROD";
const input = "./compiled/index.js";
const getGlobals = bundleType => {
const baseGlobals = {
"react-dom": "ReactDOM",
react: "React"
};
switch (bundleType) {
case UMD_DEV:
return { ...baseGlobals, "prop-types": "PropTypes" };
case UMD_PROD:
return baseGlobals;
default:
return {};
}
};
const getExternal = bundleType => {
const peerDependencies = Object.keys(pkg.peerDependencies);
const dependencies = Object.keys(pkg.dependencies);
// Hat-tip: https://github.com/rollup/rollup-plugin-babel/issues/148#issuecomment-399696316.
const makeExternalPredicate = externals => {
if (externals.length === 0) {
return () => false;
}
const pattern = new RegExp(`^(${externals.join("|")})($|/)`);
return id => pattern.test(id);
};
switch (bundleType) {
case CJS_DEV:
case CJS_PROD:
case ES:
return makeExternalPredicate([...peerDependencies, ...dependencies]);
case UMD_DEV:
return makeExternalPredicate([...peerDependencies, "prop-types"]);
default:
return makeExternalPredicate(peerDependencies);
}
};
const isProduction = bundleType =>
bundleType === CJS_PROD || bundleType === UMD_PROD;
const getBabelConfig = bundleType => {
const options = {
babelrc: false,
exclude: "node_modules/**",
presets: [["@babel/env", { loose: true, modules: false }], "@babel/react"],
plugins: ["@babel/transform-runtime"],
runtimeHelpers: true
};
switch (bundleType) {
case ES:
return {
...options,
plugins: [
...options.plugins,
["transform-react-remove-prop-types", { mode: "wrap" }]
]
};
case UMD_PROD:
case CJS_PROD:
return {
...options,
plugins: [
...options.plugins,
["transform-react-remove-prop-types", { removeImport: true }]
]
};
default:
return options;
}
};
const getPlugins = bundleType => [
nodeResolve(),
commonjs({
include: "node_modules/**",
namedExports: {
"node_modules/prop-types/index.js": [
"any",
"array",
"arrayOf",
"bool",
"element",
"exact",
"func",
"instanceOf",
"node",
"number",
"object",
"objectOf",
"oneOf",
"oneOfType",
"shape",
"string",
"symbol"
]
}
}),
babel(getBabelConfig(bundleType)),
replace({
"process.env.NODE_ENV": JSON.stringify(
isProduction(bundleType) ? "production" : "development"
)
}),
sourcemaps(),
sizeSnapshot(),
isProduction(bundleType) &&
terser({
sourcemap: true,
output: { comments: false },
compress: {
keep_infinity: true, // eslint-disable-line @typescript-eslint/camelcase
pure_getters: true // eslint-disable-line @typescript-eslint/camelcase
},
warnings: true,
ecma: 5,
toplevel: false
})
];
const getCjsConfig = bundleType => ({
input,
external: getExternal(bundleType),
output: {
file: `dist/react-fiber-traverse.cjs.${
isProduction(bundleType) ? "production" : "development"
}.js`,
format: "cjs",
sourcemap: true
},
plugins: getPlugins(bundleType)
});
const getEsConfig = () => ({
input,
external: getExternal(ES),
output: {
file: pkg.module,
format: "es",
sourcemap: true
},
plugins: getPlugins(ES)
});
const getUmdConfig = bundleType => ({
input,
external: getExternal(bundleType),
output: {
file: `dist/react-fiber-traverse.umd.${
isProduction(bundleType) ? "production" : "development"
}.js`,
format: "umd",
globals: getGlobals(bundleType),
name: "reactFiberTraverse",
sourcemap: true
},
plugins: getPlugins(bundleType)
});
export default [
getCjsConfig(CJS_DEV),
getCjsConfig(CJS_PROD),
getEsConfig(),
getUmdConfig(UMD_DEV),
getUmdConfig(UMD_PROD)
];