Skip to content

Commit 96b8068

Browse files
committed
fix(javascript): build
1 parent 7650e52 commit 96b8068

File tree

20 files changed

+271
-346
lines changed

20 files changed

+271
-346
lines changed

.github/workflows/check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ jobs:
162162

163163
- name: Build '${{ matrix.client }}' client
164164
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
165-
run: cd clients/algoliasearch-client-javascript && yarn build ${{ matrix.client }}
165+
run: cd clients/algoliasearch-client-javascript && yarn build:many @algolia/${{ matrix.client }}
166166

167167
- name: Run tests for '${{ matrix.client }}'
168168
if: ${{ steps.cache.outputs.cache-hit != 'true' }}
Lines changed: 140 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import fs from 'fs';
1+
import babel from '@rollup/plugin-babel';
2+
import { nodeResolve } from '@rollup/plugin-node-resolve';
3+
import globals from 'rollup-plugin-node-globals';
4+
import { terser } from 'rollup-plugin-terser';
5+
import ts from 'rollup-plugin-typescript2';
26

37
// Org where the packages are pushed
48
const NPM_ORG = '@algolia/';
@@ -10,111 +14,51 @@ const NODE_FORMATS = ['esm-node', 'cjs'];
1014
// Utils package with default options
1115
const UTILS = {
1216
'client-common': {
13-
dependencies: [],
17+
external: [],
18+
plugins: [
19+
babel({
20+
babelrc: false,
21+
extensions: ['.ts'],
22+
exclude: 'node_modules/**',
23+
plugins: ['@babel/plugin-proposal-class-properties'],
24+
}),
25+
],
1426
},
1527
'requester-browser-xhr': {
1628
external: ['dom'],
17-
dependencies: [`${NPM_ORG}client-common`],
29+
plugins: [],
1830
},
1931
'requester-fetch': {
2032
external: ['dom'],
21-
dependencies: [`${NPM_ORG}client-common`],
33+
plugins: [],
2234
},
2335
'requester-node-http': {
2436
external: ['https', 'http', 'url'],
25-
dependencies: [`${NPM_ORG}client-common`],
37+
plugins: [],
2638
},
2739
};
2840

29-
/**
30-
* Returns the `UTILS` packages configuration with their default bundler options.
31-
*/
32-
function getUtilConfigs() {
33-
const commonOptions = {
34-
input: 'index.ts',
35-
formats: NODE_FORMATS,
36-
external: [],
37-
};
38-
39-
return Object.entries(UTILS).map(([key, utilOptions]) => {
40-
return {
41-
...commonOptions,
42-
...utilOptions,
43-
output: key,
44-
package: key,
45-
name: `${NPM_ORG}${key}`,
46-
};
47-
});
48-
}
49-
50-
/**
51-
* Whether to build the given `utilClient` or not.
52-
*/
53-
function shouldBuildUtil(utilClient) {
54-
if (process.env.SKIP_UTILS === 'true') {
55-
return false;
56-
}
57-
58-
if (!process.env.CI) {
59-
return true;
60-
}
61-
62-
// Checking existence of `dist` folder doesn't really guarantee the built files are up-to-date.
63-
// However, on the CI, it's very likely.
64-
return !fs.existsSync(path.resolve('packages', utilClient, 'dist'));
65-
}
66-
67-
/**
68-
* Reads available packages in the monorepo.
69-
*/
70-
function getAvailableClients(client) {
71-
const availableClients = fs
72-
.readdirSync('packages/')
73-
.filter((packageName) => !Object.keys(UTILS).includes(packageName));
74-
75-
return client === 'all'
76-
? availableClients
77-
: availableClients.filter((availableClient) => availableClient === client);
78-
}
79-
80-
/**
81-
* Returns the packages to bundled based on environment variables and run conditions.
82-
*/
83-
export function getPackageConfigs() {
84-
const UTIL_CONFIGS = getUtilConfigs();
85-
const CLIENT = process.env.CLIENT.replace(NPM_ORG, '');
86-
87-
if (CLIENT === 'utils') {
88-
return UTIL_CONFIGS;
89-
}
90-
91-
if (Object.keys(UTILS).includes(CLIENT)) {
92-
return UTIL_CONFIGS.filter((config) => config.package === CLIENT);
93-
}
94-
95-
const availableClients = getAvailableClients(CLIENT);
41+
function getBaseConfigs(pkg) {
42+
const packageName = pkg.name.replace(NPM_ORG, '');
43+
const isUtils = UTILS[packageName] !== undefined;
44+
const baseConfigs = [];
9645

97-
if (availableClients.length === 0) {
98-
throw new Error(`No clients matches '${CLIENT}'.`);
99-
}
100-
101-
const configs = availableClients.flatMap((packageName) => {
46+
if (!isUtils) {
10247
const isAlgoliasearchClient = packageName === 'algoliasearch';
10348
const commonConfig = {
10449
package: packageName,
105-
name: `${NPM_ORG}${packageName}`,
50+
name: pkg.name,
10651
output: packageName,
107-
dependencies: [`${NPM_ORG}client-common`],
52+
dependencies: pkg.dependencies ? Object.keys(pkg.dependencies) : [],
10853
external: [],
54+
plugins: [],
10955
};
110-
let liteBuildConfig = [];
11156

11257
// This non-generated client is an aggregation of client, hence does not follow
11358
// the same build process.
11459
if (isAlgoliasearchClient) {
11560
const litePackageName = `${packageName}/lite`;
116-
// `algoliasearch/lite` related
117-
liteBuildConfig = [
61+
baseConfigs.push(
11862
{
11963
...commonConfig,
12064
package: litePackageName,
@@ -124,7 +68,7 @@ export function getPackageConfigs() {
12468
formats: BROWSER_FORMATS,
12569
external: ['dom'],
12670
dependencies: [
127-
...commonConfig.dependencies,
71+
`${NPM_ORG}client-common`,
12872
`${NPM_ORG}requester-browser-xhr`,
12973
],
13074
globals: {
@@ -140,34 +84,20 @@ export function getPackageConfigs() {
14084
input: 'lite/builds/node.ts',
14185
formats: NODE_FORMATS,
14286
dependencies: [
143-
...commonConfig.dependencies,
87+
`${NPM_ORG}client-common`,
14488
`${NPM_ORG}requester-node-http`,
14589
],
146-
},
147-
];
148-
149-
// `algoliasearch` related
150-
commonConfig.name = packageName;
151-
commonConfig.dependencies = [
152-
`${NPM_ORG}client-analytics`,
153-
`${NPM_ORG}client-common`,
154-
`${NPM_ORG}client-personalization`,
155-
`${NPM_ORG}client-search`,
156-
];
90+
}
91+
);
15792
}
15893

159-
return [
160-
...liteBuildConfig,
94+
baseConfigs.push(
16195
// Browser build
16296
{
16397
...commonConfig,
16498
input: 'builds/browser.ts',
16599
formats: BROWSER_FORMATS,
166100
external: ['dom'],
167-
dependencies: [
168-
...commonConfig.dependencies,
169-
`${NPM_ORG}requester-browser-xhr`,
170-
],
171101
globals: {
172102
[packageName]: packageName,
173103
},
@@ -177,36 +107,41 @@ export function getPackageConfigs() {
177107
...commonConfig,
178108
input: 'builds/node.ts',
179109
formats: NODE_FORMATS,
180-
dependencies: [
181-
...commonConfig.dependencies,
182-
`${NPM_ORG}requester-node-http`,
183-
],
184-
},
185-
];
186-
});
110+
}
111+
);
112+
113+
return baseConfigs;
114+
}
187115

188116
return [
189-
...UTIL_CONFIGS.filter((config) => shouldBuildUtil(config.package)),
190-
...configs,
117+
{
118+
...UTILS[packageName],
119+
formats: NODE_FORMATS,
120+
input: 'index.ts',
121+
dependencies: pkg.dependencies ? Object.keys(pkg.dependencies) : [],
122+
package: packageName,
123+
name: pkg.name,
124+
output: packageName,
125+
},
191126
];
192127
}
193128

194129
/**
195130
* Returns the license at the top of the UMD bundled file.
196131
*/
197-
export function createLicense(name, version) {
132+
function createLicense(name, version) {
198133
return `/*! ${name}.umd.js | ${version} | © Algolia, inc. | https://github.com/algolia/algoliasearch-client-javascript */`;
199134
}
200135

201136
/**
202137
* Bundlers with their output format and file name for the given client.
203138
*/
204-
export function createBundlers({ output, clientPath, isLiteClient }) {
139+
function createBundlers({ output, isLiteClient }) {
205140
const commonOptions = {
206141
exports: 'named',
207142
};
208143

209-
const path = isLiteClient ? `${clientPath}/dist/lite` : `${clientPath}/dist`;
144+
const path = isLiteClient ? `./dist/lite` : `./dist`;
210145

211146
return {
212147
'esm-node': {
@@ -232,3 +167,95 @@ export function createBundlers({ output, clientPath, isLiteClient }) {
232167
},
233168
};
234169
}
170+
171+
export function buildConfigs(pkg) {
172+
const baseConfigs = getBaseConfigs(pkg);
173+
const rollupConfig = [];
174+
let checkForTypes = true;
175+
176+
baseConfigs.forEach((baseConfig) => {
177+
const isLiteClient = baseConfig.name === 'algoliasearch/lite';
178+
const bundlers = createBundlers({
179+
output: baseConfig.output,
180+
isLiteClient,
181+
});
182+
183+
baseConfig.formats.forEach((format) => {
184+
const isUmdBuild = format === 'umd';
185+
const isEsmBrowserBuild = format === 'esm-browser';
186+
const umdConfig = {
187+
compressorPlugins: [],
188+
transpilerPlugins: [],
189+
};
190+
191+
if (isUmdBuild || isEsmBrowserBuild) {
192+
// eslint-disable-next-line no-param-reassign
193+
baseConfig.dependencies = [];
194+
}
195+
196+
if (isUmdBuild) {
197+
bundlers[format].name = baseConfig.name;
198+
bundlers[format].banner = createLicense(
199+
baseConfig.package,
200+
pkg.version
201+
);
202+
203+
umdConfig.compressorPlugins = [terser()];
204+
umdConfig.transpilerPlugins = [
205+
babel({
206+
babelrc: false,
207+
babelHelpers: 'runtime',
208+
extensions: ['.ts'],
209+
exclude: 'node_modules/**',
210+
presets: [
211+
[
212+
'@babel/preset-env',
213+
{
214+
targets: {
215+
browsers: ['> .5%', 'ie >= 11'],
216+
},
217+
},
218+
],
219+
],
220+
plugins: ['@babel/plugin-transform-runtime'],
221+
}),
222+
];
223+
}
224+
225+
rollupConfig.push({
226+
input: baseConfig.input,
227+
external: [...baseConfig.dependencies, ...baseConfig.external],
228+
plugins: [
229+
globals({
230+
global: true,
231+
}),
232+
nodeResolve(),
233+
ts({
234+
check: checkForTypes,
235+
tsconfig: 'tsconfig.json',
236+
tsconfigOverride: {
237+
compilerOptions: {
238+
declaration: checkForTypes,
239+
declarationMap: checkForTypes,
240+
noEmit: !checkForTypes,
241+
},
242+
},
243+
}),
244+
...umdConfig.transpilerPlugins,
245+
...umdConfig.compressorPlugins,
246+
...baseConfig.plugins,
247+
],
248+
output: bundlers[format],
249+
onwarn(msg, warn) {
250+
if (!/Circular/.test(msg)) {
251+
warn(msg);
252+
}
253+
},
254+
});
255+
256+
checkForTypes = false;
257+
});
258+
});
259+
260+
return rollupConfig;
261+
}

clients/algoliasearch-client-javascript/lerna.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
"packages": [
33
"packages/*"
44
],
5-
"version": "independent"
5+
"version": "independent",
6+
"useWorkspaces": true,
7+
"useNx": true
68
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"tasksRunnerOptions": {
3+
"default": {
4+
"runner": "nx/tasks-runners/default",
5+
"options": {
6+
"cacheableOperations": [
7+
"build:utils",
8+
"build:specific",
9+
"build:all",
10+
"build"
11+
]
12+
}
13+
}
14+
}
15+
}

clients/algoliasearch-client-javascript/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
"packages/*"
66
],
77
"scripts": {
8-
"build": "CLIENT=${0:-all} yarn rollup -c rollup.config.js",
9-
"build:all": "./scripts/build_all.sh",
10-
"build:utils": "yarn build utils",
11-
"clean": "rm -rf packages/*/dist || true",
12-
"clean:utils": "yarn workspace @algolia/client-common clean && yarn workspace @algolia/requester-node-http clean && yarn workspace @algolia/requester-browser-xhr clean && yarn workspace @algolia/requester-fetch clean",
8+
"build:all": "lerna run build --parallel --ignore '{@algolia/requester-*,@algolia/client-common}'",
9+
"build:many": "lerna run build --parallel --scope ${0:-'*'}",
10+
"build:utils": "lerna run build --parallel --scope '{@algolia/requester-*,@algolia/client-common}'",
1311
"release:bump": "lerna version ${0:-patch} --no-changelog --no-git-tag-version --no-push --exact --force-publish --yes",
1412
"release:publish": "ts-node --project tsconfig.script.json scripts/publish.ts",
1513
"test:lint": "eslint . --ext .js,.ts",
@@ -31,6 +29,7 @@
3129
"bundlesize2": "0.0.31",
3230
"execa": "5.1.1",
3331
"lerna": "5.2.0",
32+
"nx": "14.4.3",
3433
"rollup": "2.77.0",
3534
"rollup-plugin-node-globals": "1.4.0",
3635
"rollup-plugin-terser": "7.0.2",

0 commit comments

Comments
 (0)