-
Notifications
You must be signed in to change notification settings - Fork 21
fix(javascript): clean rollup config #700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
186 changes: 186 additions & 0 deletions
186
clients/algoliasearch-client-javascript/base.rollup.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe that value could be read from a
package.json
, should not be complicated if you want to do it