-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.rollup.config.js
306 lines (276 loc) · 7.4 KB
/
base.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import babel from '@rollup/plugin-babel';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import globals from 'rollup-plugin-node-globals';
import terser from '@rollup/plugin-terser';
import ts from 'rollup-plugin-typescript2';
// Org where the packages are pushed
const NPM_ORG = '@algolia/';
// Output formats
const BROWSER_FORMATS = ['esm-browser', 'umd'];
const NODE_FORMATS = ['esm-node', 'cjs'];
// A mapping that saves the types that have already been checked during the build process
// it avoid checking/generating the same types multiple times
const TYPES_TO_CHECK = {};
// Utils package with default options
const UTILS = {
'client-common': {
external: [],
plugins: [
babel({
babelrc: false,
extensions: ['.ts'],
exclude: 'node_modules/**',
plugins: ['@babel/plugin-transform-class-properties'],
}),
],
},
'requester-browser-xhr': {
external: ['dom'],
plugins: [],
},
'requester-fetch': {
external: ['dom'],
plugins: [],
},
'requester-node-http': {
external: ['https', 'http', 'url'],
plugins: [],
},
};
/**
* Returns the license at the top of the UMD bundled file.
*/
function createLicense(name, version) {
return `/*! ${name}.umd.js | ${version} | © Algolia, inc. | https://github.com/algolia/algoliasearch-client-javascript */`;
}
/**
* Bundlers with their output format and file name for the given client.
*/
function createBundlers({ output, isLiteClient }) {
const commonOptions = {
exports: 'named',
};
const path = isLiteClient ? `./dist/lite` : `./dist`;
return {
'esm-node': {
...commonOptions,
file: `${path}/${output}.esm.node.js`,
format: 'es',
},
'esm-browser': {
...commonOptions,
file: `${path}/${output}.esm.browser.js`,
format: 'es',
},
umd: {
...commonOptions,
file: `${path}/${output}.umd.js`,
format: 'umd',
esModule: false,
},
cjs: {
...commonOptions,
file: `${path}/${output}.cjs`,
format: 'cjs',
},
};
}
/**
* Build configs to iterate on based on the given `package.json`.
*/
function getBaseConfigs(pkg) {
const packageName = pkg.name.replace(NPM_ORG, '');
const isUtils = UTILS[packageName] !== undefined;
const commonConfig = {
dependencies: pkg.dependencies ? Object.keys(pkg.dependencies) : [],
package: packageName,
name: pkg.name,
output: packageName,
plugins: [],
external: [],
formats: NODE_FORMATS,
};
if (isUtils) {
return [
{
...commonConfig,
...UTILS[packageName],
input: 'index.ts',
},
];
}
const isAlgoliasearchClient = packageName === 'algoliasearch';
const configPerEnv = {
browser: {
...commonConfig,
input: 'builds/browser.ts',
formats: BROWSER_FORMATS,
external: ['dom'],
globals: {
[packageName]: packageName,
},
},
node: {
...commonConfig,
input: 'builds/node.ts',
},
};
if (!isAlgoliasearchClient) {
return [configPerEnv.browser, configPerEnv.node];
}
/**
* Algoliasearch is am aggregation of sub clients, plus provide a `lite` version
* that needs its own build.
*/
const litePackageName = `${packageName}/lite`;
return [
// algoliasearch client configs
configPerEnv.browser,
configPerEnv.node,
// lite client configs
{
...commonConfig,
...configPerEnv.browser,
package: litePackageName,
name: litePackageName,
output: 'lite',
input: 'lite/builds/browser.ts',
dependencies: [
`${NPM_ORG}client-common`,
`${NPM_ORG}requester-browser-xhr`,
],
globals: {
[litePackageName]: litePackageName,
},
},
// Node build
{
...commonConfig,
...configPerEnv.node,
package: litePackageName,
name: litePackageName,
output: 'lite',
input: 'lite/builds/node.ts',
dependencies: [
`${NPM_ORG}client-common`,
`${NPM_ORG}requester-node-http`,
],
},
];
}
/**
* Decides whether the currently built client should check for types or not.
*/
function shouldCheckForTypes(name, currentFormat, isLiteClient) {
const defaults = {
node: true,
browser: true,
lite: true,
};
// Initialize with defaults
if (TYPES_TO_CHECK[name] === undefined) {
TYPES_TO_CHECK[name] = defaults;
}
// We then set the value for the key to false when it matches its build format
if (BROWSER_FORMATS.includes(currentFormat) && TYPES_TO_CHECK[name].browser) {
TYPES_TO_CHECK[name].browser = false;
return true;
}
if (NODE_FORMATS.includes(currentFormat) && TYPES_TO_CHECK[name].node) {
TYPES_TO_CHECK[name].node = false;
return true;
}
if (isLiteClient && TYPES_TO_CHECK[name].lite) {
TYPES_TO_CHECK[name].lite = false;
return true;
}
return false;
}
export function buildConfigs(pkg) {
const baseConfigs = getBaseConfigs(pkg);
const rollupConfig = [];
baseConfigs.forEach((baseConfig) => {
const isLiteClient = baseConfig.name === 'algoliasearch/lite';
const bundlers = createBundlers({
output: baseConfig.output,
isLiteClient,
});
baseConfig.formats.forEach((format) => {
const checkForTypes = shouldCheckForTypes(
baseConfig.name,
format,
isLiteClient
);
const isUmdBuild = format === 'umd';
const isEsmBrowserBuild = format === 'esm-browser';
const umdConfig = {
compressorPlugins: [],
transpilerPlugins: [],
};
if (isUmdBuild || isEsmBrowserBuild) {
// eslint-disable-next-line no-param-reassign
baseConfig.dependencies = [];
}
if (isUmdBuild) {
bundlers[format].name = baseConfig.name;
bundlers[format].banner = createLicense(
baseConfig.package,
pkg.version
);
umdConfig.compressorPlugins = [terser()];
umdConfig.transpilerPlugins = [
babel({
babelrc: false,
babelHelpers: 'runtime',
extensions: ['builds/*.ts', 'src/*.ts', 'model/*.ts', 'index.ts'],
exclude: 'node_modules/**',
presets: [
[
'@babel/preset-env',
{
targets: {
browsers: ['> .5%', 'ie >= 11'],
},
},
],
],
plugins: ['@babel/plugin-transform-runtime'],
}),
];
}
rollupConfig.push({
input: baseConfig.input,
external: [...baseConfig.external, ...baseConfig.dependencies],
plugins: [
globals({
global: true,
}),
nodeResolve({
preferBuiltins: true,
}),
ts({
check: checkForTypes,
tsconfig: isLiteClient ? 'lite/tsconfig.json' : 'tsconfig.json',
tsconfigOverride: {
compilerOptions: {
declaration: checkForTypes,
declarationMap: checkForTypes,
noEmit: !checkForTypes,
},
},
}),
...umdConfig.transpilerPlugins,
...umdConfig.compressorPlugins,
...baseConfig.plugins,
],
output: bundlers[format],
onwarn(msg, warn) {
if (!/Circular/.test(msg)) {
warn(msg);
}
},
});
});
});
return rollupConfig;
}