core-js-builder is the programmatic API for producing custom core-js bundles. Instead of shipping the full library, you can generate a single file that contains only the polyfills required by a specific set of target environments or a specific set of features — or both.
Installation
core-js-builder depends on core-js and core-js-compat. Install core-js alongside it:
Basic usage
build.mjs
Promise that resolves to the bundle source as a string.
Options reference
modules
modules
Controls which parts of core-js are eligible for inclusion.Accepted values:
- A string matching an entry point name — e.g.
'core-js/actual','core-js/es' - A string matching a module name — e.g.
'es.array.flat-map' - A regular expression matching one or more module names — e.g.
/^esnext\.reflect\./ - An array of any combination of the above
modules is omitted, all core-js modules are candidates for inclusion (still filtered by targets if provided).exclude
exclude
Removes specific modules from the set selected by
modules. Same format as modules.targets
targets
A browserslist query string. When set, the builder uses compatibility data from Omit
core-js-compat to further reduce the module list to only those not natively supported by the specified environments.targets to build for all environments — the output will contain every module listed in modules.format
format
Controls the output format. Defaults to
In
'bundle'.| Value | Description |
|---|---|
'bundle' | Single self-contained IIFE. Ready for a <script> tag or direct inclusion. |
'cjs' | CommonJS: a file of require('core-js/modules/...') calls. Not bundled. |
'esm' | ES modules: a file of import 'core-js/modules/....js' statements. Not bundled. |
cjs and esm modes the result is not bundled — it is a list of imports of the individual core-js module files.filename
filename
Optional path where the bundle is written to disk. The directory is created automatically if it does not exist.If
filename is omitted, no file is written; the bundle is only returned as a string from the Promise.summary
summary
Controls what summary information is produced. Each sub-key (When
console and comment) accepts true (enable both size and modules) or an object with size and modules booleans.summary.console.size is true, the builder prints a line like:Output
The generated bundle is a self-contained script. When you providetargets, each module in the output is only present if at least one of your target environments does not natively support it. This means the same modules configuration produces a smaller file for modern target sets and a larger file for older ones.
Use summary.console.modules: true to see exactly which modules were selected and which target environments each one covers:
TypeScript
When importingcore-js-builder from TypeScript, set esModuleInterop: true in your tsconfig.json:
tsconfig.json
build.ts
Related
- Entry points — the
modulesandexcludeoptions accept the same entry point names used in direct imports. core-js-compat— the compatibility data package thattargetsqueries are evaluated against.