Skip to content

Commit e93a31c

Browse files
authored
fix(javascript): clean rollup config (#700)
1 parent a57da3f commit e93a31c

File tree

7 files changed

+262
-759
lines changed

7 files changed

+262
-759
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import fs from 'fs';
2+
3+
// Org where the packages are pushed
4+
const NPM_ORG = '@experimental-api-clients-automation/';
5+
6+
// Output formats
7+
const BROWSER_FORMATS = ['umd-browser', 'esm-browser', 'cjs-browser'];
8+
const NODE_FORMATS = ['cjs-node', 'esm-node'];
9+
10+
// Utils package with default options
11+
const UTILS = {
12+
'client-common': {
13+
dependencies: [],
14+
},
15+
'requester-browser-xhr': {
16+
external: ['dom'],
17+
dependencies: [`${NPM_ORG}client-common`],
18+
},
19+
'requester-node-http': {
20+
external: ['https', 'http', 'url'],
21+
dependencies: [`${NPM_ORG}client-common`],
22+
},
23+
};
24+
25+
/**
26+
* Returns the `UTILS` packages configuration with their default bundler options.
27+
*/
28+
function getUtilConfigs() {
29+
const commonOptions = {
30+
input: 'index.ts',
31+
formats: NODE_FORMATS,
32+
external: [],
33+
};
34+
35+
return Object.entries(UTILS).map(([key, utilOptions]) => {
36+
return {
37+
...commonOptions,
38+
...utilOptions,
39+
output: key,
40+
package: key,
41+
name: `${NPM_ORG}${key}`,
42+
};
43+
});
44+
}
45+
46+
/**
47+
* Whether to build the given `utilClient` or not.
48+
*/
49+
function shouldBuildUtil(utilClient) {
50+
if (process.env.SKIP_UTILS === 'true') {
51+
return false;
52+
}
53+
54+
if (!process.env.CI) {
55+
return true;
56+
}
57+
58+
// Checking existence of `dist` folder doesn't really guarantee the built files are up-to-date.
59+
// However, on the CI, it's very likely.
60+
return !fs.existsSync(path.resolve('packages', utilClient, 'dist'));
61+
}
62+
63+
/**
64+
* Reads available packages in the monorepo.
65+
*/
66+
function getAvailableClients(client) {
67+
const availableClients = fs
68+
.readdirSync('packages/')
69+
.filter((packageName) => !Object.keys(UTILS).includes(packageName));
70+
71+
return client === 'all'
72+
? availableClients
73+
: availableClients.filter((availableClient) => availableClient === client);
74+
}
75+
76+
/**
77+
* Returns the packages to bundled based on environment variables and run conditions.
78+
*/
79+
export function getPackageConfigs() {
80+
const UTIL_CONFIGS = getUtilConfigs();
81+
const CLIENT = process.env.CLIENT.replace(NPM_ORG, '');
82+
83+
if (CLIENT === 'utils') {
84+
return UTIL_CONFIGS;
85+
}
86+
87+
if (Object.keys(UTILS).includes(CLIENT)) {
88+
return UTIL_CONFIGS.filter((config) => config.package === CLIENT);
89+
}
90+
91+
const availableClients = getAvailableClients(CLIENT);
92+
93+
if (availableClients.length === 0) {
94+
throw new Error(`No clients matches '${CLIENT}'.`);
95+
}
96+
97+
const configs = availableClients.flatMap((packageName) => {
98+
const isAlgoliasearchClient = packageName === 'algoliasearch';
99+
const commonConfig = {
100+
package: packageName,
101+
name: `${NPM_ORG}${packageName}`,
102+
output: packageName,
103+
dependencies: [`${NPM_ORG}client-common`],
104+
external: [],
105+
};
106+
107+
// This non-generated client is an aggregation of client, hence does not follow
108+
// the same build process.
109+
if (isAlgoliasearchClient) {
110+
commonConfig.name = packageName;
111+
commonConfig.dependencies = [
112+
`${NPM_ORG}client-analytics`,
113+
`${NPM_ORG}client-common`,
114+
`${NPM_ORG}client-personalization`,
115+
`${NPM_ORG}client-search`,
116+
];
117+
}
118+
119+
return [
120+
// Browser build
121+
{
122+
...commonConfig,
123+
input: 'builds/browser.ts',
124+
formats: BROWSER_FORMATS,
125+
external: ['dom'],
126+
dependencies: [
127+
...commonConfig.dependencies,
128+
`${NPM_ORG}/requester-browser-xhr`,
129+
],
130+
globals: {
131+
[packageName]: packageName,
132+
},
133+
},
134+
// Node build
135+
{
136+
...commonConfig,
137+
input: 'builds/node.ts',
138+
formats: NODE_FORMATS,
139+
dependencies: [
140+
...commonConfig.dependencies,
141+
`${NPM_ORG}/requester-node-http`,
142+
],
143+
},
144+
];
145+
});
146+
147+
return [
148+
...UTIL_CONFIGS.filter((config) => shouldBuildUtil(config.package)),
149+
...configs,
150+
];
151+
}
152+
153+
/**
154+
* Returns the license at the top of the UMD bundled file.
155+
*/
156+
export function createLicense(name, version) {
157+
return `/*! ${name}.umd.js | ${version} | © Algolia, inc. | https://github.com/algolia/algoliasearch-client-javascript */`;
158+
}
159+
160+
/**
161+
* Bundlers with their output format and file name for the given client.
162+
*/
163+
export function createBundlers({ output, clientPath }) {
164+
return {
165+
'esm-node': {
166+
file: `${clientPath}/dist/${output}.esm.node.js`,
167+
format: 'es',
168+
},
169+
'esm-browser': {
170+
file: `${clientPath}/dist/${output}.esm.browser.js`,
171+
format: 'es',
172+
},
173+
'umd-browser': {
174+
file: `${clientPath}/dist/${output}.umd.browser.js`,
175+
format: 'umd',
176+
},
177+
'cjs-node': {
178+
file: `${clientPath}/dist/${output}.cjs.node.js`,
179+
format: 'cjs',
180+
},
181+
'cjs-browser': {
182+
file: `${clientPath}/dist/${output}.cjs.browser.js`,
183+
format: 'cjs',
184+
},
185+
};
186+
}

clients/algoliasearch-client-javascript/bundlesize.config.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
"files": [
33
{
44
"path": "packages/algoliasearch/dist/algoliasearch.umd.browser.js",
5-
"maxSize": "7.50KB"
5+
"maxSize": "7.60KB"
66
},
77
{
88
"path": "packages/algoliasearch-lite/dist/algoliasearch-lite.umd.browser.js",
9-
"maxSize": "3.75KB"
9+
"maxSize": "3.85KB"
1010
},
1111
{
1212
"path": "packages/client-abtesting/dist/client-abtesting.umd.browser.js",
13-
"maxSize": "3.90KB"
13+
"maxSize": "3.95KB"
1414
},
1515
{
1616
"path": "packages/client-analytics/dist/client-analytics.umd.browser.js",
17-
"maxSize": "4.50KB"
17+
"maxSize": "4.55KB"
1818
},
1919
{
2020
"path": "packages/client-insights/dist/client-insights.umd.browser.js",
21-
"maxSize": "3.75KB"
21+
"maxSize": "3.80KB"
2222
},
2323
{
2424
"path": "packages/client-personalization/dist/client-personalization.umd.browser.js",
@@ -30,15 +30,15 @@
3030
},
3131
{
3232
"path": "packages/client-search/dist/client-search.umd.browser.js",
33-
"maxSize": "6.25KB"
33+
"maxSize": "6.30KB"
3434
},
3535
{
3636
"path": "packages/client-sources/dist/client-sources.umd.browser.js",
37-
"maxSize": "3.85KB"
37+
"maxSize": "3.80KB"
3838
},
3939
{
4040
"path": "packages/recommend/dist/recommend.umd.browser.js",
41-
"maxSize": "3.85KB"
41+
"maxSize": "3.80KB"
4242
}
4343
]
4444
}

clients/algoliasearch-client-javascript/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"execa": "5.1.1",
3232
"lerna": "5.1.1",
3333
"rollup": "2.75.6",
34-
"rollup-plugin-filesize": "9.1.2",
3534
"rollup-plugin-node-globals": "1.4.0",
3635
"rollup-plugin-terser": "7.0.2",
3736
"rollup-plugin-typescript2": "0.32.1",

0 commit comments

Comments
 (0)