Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ jobs:
strategy:
matrix:
node:
- 20.9
- 21
- ^22.5.1
- ^20.19
- ^22.12
- 23
- 24
- 25
Expand All @@ -74,9 +73,8 @@ jobs:
- windows-latest
- macos-latest
node:
- 20.9
- 21
- ^22.5.1
- ^20.19
- ^22.12
- 23
- 24
- 25
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ node_modules/
/packages/core-js/LICENSE
/packages/core-js-babel-plugin/LICENSE
/packages/core-js-builder/LICENSE
/packages/core-js-builder/__tmp__/
/packages/core-js-bundle/LICENSE
/packages/core-js-bundle/index.js
/packages/core-js-bundle/minified.js
Expand Down
1,134 changes: 470 additions & 664 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/core-js-babel-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"@core-js/compat": "4.0.0-alpha.0"
},
"engines": {
"node": ">=20.19.0"
"node": "^20.19.0 || >=22.12.0"
}
}
1 change: 1 addition & 0 deletions packages/core-js-builder/.npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
__tmp__/
*.log
.*
2 changes: 2 additions & 0 deletions packages/core-js-builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const { script } = await builder({
// output format, 'bundle' by default, can be 'cjs' or 'esm', and in this case
// the result will not be bundled and will contain imports of required modules
format: 'bundle',
// minify the output in the case of format: bundle, `true` by default
minify: true,
// optional target filename, if it's missed a file will not be created
filename: PATH_TO_MY_COREJS_BUNDLE,
});
Expand Down
13 changes: 0 additions & 13 deletions packages/core-js-builder/config.js

This file was deleted.

59 changes: 59 additions & 0 deletions packages/core-js-builder/config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const version = '4.0.0-alpha.0';

/* eslint-disable prefer-template -- for better formatting */
export const banner = '/**\n' +
' * core-js ' + version + '\n' +
' * © 2013–2025 Denis Pushkarev (zloirock.ru), 2025–2026 CoreJS Company (core-js.io). All rights reserved.\n' +
' * license: https://github.com/zloirock/core-js/blob/v' + version + '/LICENSE\n' +
' * source: https://github.com/zloirock/core-js\n' +
' */';
/* eslint-enable prefer-template -- for better formatting */

export function getRolldownOptions(input, output) {
return {
input,
platform: 'neutral',
treeshake: false,
output: {
externalLiveBindings: false,
format: 'iife',
file: output,
keepNames: true,
minifyInternalExports: true,
},
};
}

export const ModernSyntax = [
'arrow-functions',
'shorthand-properties',
];

export const MinifyOptions = {
minify: true,
jsc: {
minify: {
compress: {
arrows: false,
ecma: 5,
hoist_funs: true,
keep_fnames: true,
pure_getters: true,
reduce_funcs: true,
// document.all detection case
typeofs: false,
unsafe_proto: true,
unsafe_undefined: true,
},
mangle: {
keep_fnames: true,
safari10: true,
toplevel: true,
},
format: {
comments: false,
ecma: 5,
},
},
},
};
2 changes: 2 additions & 0 deletions packages/core-js-builder/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Options = Pick<CompatOptions, "exclude" | "modules" | "targets"> & {
format?: Format,
/** optional target filename, if it's missed a file will not be created */
filename?: string,
/** minify the output in the case of format: bundle, `true` by default */
minify?: boolean,
/** shows summary for the bundle, disabled by default */
summary?: Summary,
};
Expand Down
93 changes: 0 additions & 93 deletions packages/core-js-builder/index.js

This file was deleted.

126 changes: 126 additions & 0 deletions packages/core-js-builder/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* eslint-disable no-console -- output */
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { styleText } from 'node:util';
import { build } from 'rolldown';
import { transform } from '@swc/core';
import compat from '@core-js/compat/compat.js';
import { banner, getRolldownOptions, MinifyOptions, ModernSyntax } from './config.mjs';

function normalizeSummary(unit = {}) {
let size, modules;
if (typeof unit != 'object') {
size = modules = !!unit;
} else {
size = !!unit.size;
modules = !!unit.modules;
} return { size, modules };
}

function importIt(it) {
return `import 'core-js/modules/${ it }.js';\n`;
}

function requireIt(it) {
return `require('core-js/modules/${ it }');\n`;
}

function importModules(modules, format = 'esm') {
return modules.map(format === 'esm' ? importIt : requireIt).join('');
}

export default async function ({
modules = null,
exclude = [],
targets = null,
format = 'bundle',
minify = true,
filename = null,
summary = {},
} = {}) {
if (!['bundle', 'cjs', 'esm'].includes(format)) throw new TypeError('Incorrect output type');

summary = { comment: normalizeSummary(summary.comment), console: normalizeSummary(summary.console) };

const TITLE = filename ?? '`core-js`';

if (typeof TITLE != 'string') throw new TypeError('Incorrect filename');

let script = banner;
let code = '\n';

const { list, targets: compatTargets } = compat({ targets, modules, exclude });

if (list.length) {
if (format === 'bundle') {
const tempDir = join(dirname(fileURLToPath(import.meta.url)), '__tmp__');
const tempFile = join(tempDir, `core-js-${ Math.random().toString(36).slice(2) }.js`);
const templateFile = `${ tempFile }-template.js`;

try {
await mkdir(tempDir, { recursive: true });
await writeFile(templateFile, importModules(list));

await build(getRolldownOptions(templateFile, tempFile));

code = String(await readFile(tempFile, 'utf8'));
} finally {
await rm(templateFile, { force: true });
await rm(tempFile, { force: true });
}

const swcOptions = {};

// rolldown helpers / wrappers contain es2015 syntax
let syntax = ModernSyntax;

if (targets) {
syntax = compat({ targets, modules: syntax, __external: true }).list;
}

const swcTransforms = syntax.map(it => `transform-${ it }`);

if (swcTransforms.length) Object.assign(swcOptions, {
env: {
include: swcTransforms,
},
});

if (minify) Object.assign(swcOptions, MinifyOptions);

if (swcTransforms.length || minify) {
code = (await transform(code, swcOptions)).code;
}

// swc and rolldown considers output code as a module and drops 'use strict'
code = `!function () { 'use strict'; ${ code } }();\n`;
} else {
code = importModules(list, format);
}
}

if (summary.comment.size) script += `/*\n * size: ${ (code.length / 1024).toFixed(2) }KB */`;
if (summary.comment.modules) script += `/*\n * modules:\n${ list.map(it => ` * ${ it }\n`).join('') } */`;
if (code) script += `\n${ code }`;

if (summary.console.size) {
console.log(styleText('green', `bundling ${ styleText('cyan', TITLE) }, size: ${
styleText('cyan', (script.length / 1024).toFixed(2))
}KB`));
}

if (summary.console.modules) {
console.log(styleText('green', `bundling ${ styleText('cyan', TITLE) }, modules:`));
if (list.length) for (const it of list) {
console.log(styleText('cyan', `${ it + (targets ? styleText('green', ` for ${ styleText('cyan', JSON.stringify(compatTargets[it])) }`) : '') }`));
} else console.log(styleText('green', 'nothing'));
}

if (filename !== null && filename !== undefined) {
await mkdir(dirname(filename), { recursive: true });
await writeFile(filename, script);
}

return { script };
}
22 changes: 17 additions & 5 deletions packages/core-js-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"publishConfig": {
"access": "public"
},
"type": "commonjs",
"type": "module",
"description": "core-js builder",
"repository": {
"type": "git",
Expand All @@ -26,14 +26,26 @@
"url": "http://zloirock.ru"
},
"sideEffects": false,
"main": "index.js",
"main": "index.mjs",
"exports": {
".": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"./index.mjs": {
"types": "./index.d.ts",
"default": "./index.mjs"
},
"./config.mjs": "./config.mjs"
},
"types": "index.d.ts",
"dependencies": {
"core-js": "4.0.0-alpha.0",
"@core-js/compat": "4.0.0-alpha.0",
"webpack": "^5.104.1"
"@swc/core": "^1.15.11",
"core-js": "4.0.0-alpha.0",
"rolldown": "^1.0.0-rc.3"
},
"engines": {
"node": ">=20.19.0"
"node": "^20.19.0 || >=22.12.0"
}
}
Loading