Skip to content

Commit

Permalink
fix(javascript): clean rollup config (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Jun 16, 2022
1 parent a57da3f commit e93a31c
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 759 deletions.
186 changes: 186 additions & 0 deletions clients/algoliasearch-client-javascript/base.rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import fs from 'fs';

// Org where the packages are pushed
const NPM_ORG = '@experimental-api-clients-automation/';

// Output formats
const BROWSER_FORMATS = ['umd-browser', 'esm-browser', 'cjs-browser'];
const NODE_FORMATS = ['cjs-node', 'esm-node'];

// Utils package with default options
const UTILS = {
'client-common': {
dependencies: [],
},
'requester-browser-xhr': {
external: ['dom'],
dependencies: [`${NPM_ORG}client-common`],
},
'requester-node-http': {
external: ['https', 'http', 'url'],
dependencies: [`${NPM_ORG}client-common`],
},
};

/**
* Returns the `UTILS` packages configuration with their default bundler options.
*/
function getUtilConfigs() {
const commonOptions = {
input: 'index.ts',
formats: NODE_FORMATS,
external: [],
};

return Object.entries(UTILS).map(([key, utilOptions]) => {
return {
...commonOptions,
...utilOptions,
output: key,
package: key,
name: `${NPM_ORG}${key}`,
};
});
}

/**
* Whether to build the given `utilClient` or not.
*/
function shouldBuildUtil(utilClient) {
if (process.env.SKIP_UTILS === 'true') {
return false;
}

if (!process.env.CI) {
return true;
}

// Checking existence of `dist` folder doesn't really guarantee the built files are up-to-date.
// However, on the CI, it's very likely.
return !fs.existsSync(path.resolve('packages', utilClient, 'dist'));
}

/**
* Reads available packages in the monorepo.
*/
function getAvailableClients(client) {
const availableClients = fs
.readdirSync('packages/')
.filter((packageName) => !Object.keys(UTILS).includes(packageName));

return client === 'all'
? availableClients
: availableClients.filter((availableClient) => availableClient === client);
}

/**
* Returns the packages to bundled based on environment variables and run conditions.
*/
export function getPackageConfigs() {
const UTIL_CONFIGS = getUtilConfigs();
const CLIENT = process.env.CLIENT.replace(NPM_ORG, '');

if (CLIENT === 'utils') {
return UTIL_CONFIGS;
}

if (Object.keys(UTILS).includes(CLIENT)) {
return UTIL_CONFIGS.filter((config) => config.package === CLIENT);
}

const availableClients = getAvailableClients(CLIENT);

if (availableClients.length === 0) {
throw new Error(`No clients matches '${CLIENT}'.`);
}

const configs = availableClients.flatMap((packageName) => {
const isAlgoliasearchClient = packageName === 'algoliasearch';
const commonConfig = {
package: packageName,
name: `${NPM_ORG}${packageName}`,
output: packageName,
dependencies: [`${NPM_ORG}client-common`],
external: [],
};

// This non-generated client is an aggregation of client, hence does not follow
// the same build process.
if (isAlgoliasearchClient) {
commonConfig.name = packageName;
commonConfig.dependencies = [
`${NPM_ORG}client-analytics`,
`${NPM_ORG}client-common`,
`${NPM_ORG}client-personalization`,
`${NPM_ORG}client-search`,
];
}

return [
// Browser build
{
...commonConfig,
input: 'builds/browser.ts',
formats: BROWSER_FORMATS,
external: ['dom'],
dependencies: [
...commonConfig.dependencies,
`${NPM_ORG}/requester-browser-xhr`,
],
globals: {
[packageName]: packageName,
},
},
// Node build
{
...commonConfig,
input: 'builds/node.ts',
formats: NODE_FORMATS,
dependencies: [
...commonConfig.dependencies,
`${NPM_ORG}/requester-node-http`,
],
},
];
});

return [
...UTIL_CONFIGS.filter((config) => shouldBuildUtil(config.package)),
...configs,
];
}

/**
* Returns the license at the top of the UMD bundled file.
*/
export 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.
*/
export function createBundlers({ output, clientPath }) {
return {
'esm-node': {
file: `${clientPath}/dist/${output}.esm.node.js`,
format: 'es',
},
'esm-browser': {
file: `${clientPath}/dist/${output}.esm.browser.js`,
format: 'es',
},
'umd-browser': {
file: `${clientPath}/dist/${output}.umd.browser.js`,
format: 'umd',
},
'cjs-node': {
file: `${clientPath}/dist/${output}.cjs.node.js`,
format: 'cjs',
},
'cjs-browser': {
file: `${clientPath}/dist/${output}.cjs.browser.js`,
format: 'cjs',
},
};
}
16 changes: 8 additions & 8 deletions clients/algoliasearch-client-javascript/bundlesize.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
"files": [
{
"path": "packages/algoliasearch/dist/algoliasearch.umd.browser.js",
"maxSize": "7.50KB"
"maxSize": "7.60KB"
},
{
"path": "packages/algoliasearch-lite/dist/algoliasearch-lite.umd.browser.js",
"maxSize": "3.75KB"
"maxSize": "3.85KB"
},
{
"path": "packages/client-abtesting/dist/client-abtesting.umd.browser.js",
"maxSize": "3.90KB"
"maxSize": "3.95KB"
},
{
"path": "packages/client-analytics/dist/client-analytics.umd.browser.js",
"maxSize": "4.50KB"
"maxSize": "4.55KB"
},
{
"path": "packages/client-insights/dist/client-insights.umd.browser.js",
"maxSize": "3.75KB"
"maxSize": "3.80KB"
},
{
"path": "packages/client-personalization/dist/client-personalization.umd.browser.js",
Expand All @@ -30,15 +30,15 @@
},
{
"path": "packages/client-search/dist/client-search.umd.browser.js",
"maxSize": "6.25KB"
"maxSize": "6.30KB"
},
{
"path": "packages/client-sources/dist/client-sources.umd.browser.js",
"maxSize": "3.85KB"
"maxSize": "3.80KB"
},
{
"path": "packages/recommend/dist/recommend.umd.browser.js",
"maxSize": "3.85KB"
"maxSize": "3.80KB"
}
]
}
1 change: 0 additions & 1 deletion clients/algoliasearch-client-javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"execa": "5.1.1",
"lerna": "5.1.1",
"rollup": "2.75.6",
"rollup-plugin-filesize": "9.1.2",
"rollup-plugin-node-globals": "1.4.0",
"rollup-plugin-terser": "7.0.2",
"rollup-plugin-typescript2": "0.32.1",
Expand Down
Loading

0 comments on commit e93a31c

Please sign in to comment.