Skip to content

Commit 69ce779

Browse files
committed
replace webpack with rolldown in builder
1 parent 5c6db9d commit 69ce779

File tree

11 files changed

+614
-771
lines changed

11 files changed

+614
-771
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ node_modules/
2424
/packages/core-js/LICENSE
2525
/packages/core-js-babel-plugin/LICENSE
2626
/packages/core-js-builder/LICENSE
27+
/packages/core-js-builder/__tmp__/
2728
/packages/core-js-bundle/LICENSE
2829
/packages/core-js-bundle/index.js
2930
/packages/core-js-bundle/minified.js

package-lock.json

Lines changed: 465 additions & 659 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
2+
__tmp__/
23
*.log
34
.*

packages/core-js-builder/config.js

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const version = '4.0.0-alpha.0';
2+
3+
/* eslint-disable prefer-template -- for better formatting */
4+
export default '/**\n' +
5+
' * core-js ' + version + '\n' +
6+
' * © 2013–2025 Denis Pushkarev (zloirock.ru), 2025–2026 CoreJS Company (core-js.io). All rights reserved.\n' +
7+
' * license: https://github.com/zloirock/core-js/blob/v' + version + '/LICENSE\n' +
8+
' * source: https://github.com/zloirock/core-js\n' +
9+
' */';
10+
/* eslint-enable prefer-template -- for better formatting */

packages/core-js-builder/index.js

Lines changed: 0 additions & 93 deletions
This file was deleted.

packages/core-js-builder/index.mjs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/* eslint-disable no-console -- output */
2+
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
3+
import { dirname, join } from 'node:path';
4+
import { fileURLToPath } from 'node:url';
5+
import { build } from 'rolldown';
6+
import { transform } from '@swc/core';
7+
import compat from '@core-js/compat/compat.js';
8+
import banner from './config.mjs';
9+
10+
function normalizeSummary(unit = {}) {
11+
let size, modules;
12+
if (typeof unit != 'object') {
13+
size = modules = !!unit;
14+
} else {
15+
size = !!unit.size;
16+
modules = !!unit.modules;
17+
} return { size, modules };
18+
}
19+
20+
function importIt(it) {
21+
return `import 'core-js/modules/${ it }.js';\n`;
22+
}
23+
24+
function requireIt(it) {
25+
return `require('core-js/modules/${ it }');\n`;
26+
}
27+
28+
function importModules(modules, format = 'esm') {
29+
return modules.map(format === 'esm' ? importIt : requireIt).join('');
30+
}
31+
32+
export default async function ({
33+
modules = null,
34+
exclude = [],
35+
targets = null,
36+
format = 'bundle',
37+
filename = null,
38+
summary = {},
39+
} = {}) {
40+
if (!['bundle', 'cjs', 'esm'].includes(format)) throw new TypeError('Incorrect output type');
41+
summary = { comment: normalizeSummary(summary.comment), console: normalizeSummary(summary.console) };
42+
43+
const TITLE = filename !== null || filename !== undefined ? filename : '`core-js`';
44+
let script = banner;
45+
let code = '\n';
46+
47+
const { list, targets: compatTargets } = compat({ targets, modules, exclude });
48+
49+
if (list.length) {
50+
if (format === 'bundle') {
51+
const tempDir = join(dirname(fileURLToPath(import.meta.url)), '__tmp__');
52+
const tempFile = join(tempDir, `core-js-${ Math.random().toString(36).slice(2) }.js`);
53+
const templateFile = `${ tempFile }-template.js`;
54+
55+
try {
56+
await mkdir(tempDir, { recursive: true });
57+
await writeFile(templateFile, importModules(list));
58+
console.time(1);
59+
await build({
60+
input: templateFile,
61+
platform: 'neutral',
62+
treeshake: false,
63+
output: {
64+
externalLiveBindings: false,
65+
format: 'iife',
66+
file: tempFile,
67+
keepNames: true,
68+
minifyInternalExports: true,
69+
},
70+
});
71+
console.timeEnd(1);
72+
code = String(await readFile(tempFile, 'utf8'));
73+
} finally {
74+
await rm(templateFile, { force: true });
75+
await rm(tempFile, { force: true });
76+
}
77+
78+
// rolldown helpers / wrappers contain arrows
79+
code = (await transform(code, {
80+
env: {
81+
include: [
82+
'transform-arrow-functions',
83+
'transform-shorthand-properties',
84+
],
85+
},
86+
})).code;
87+
88+
code = `!function (undefined) { 'use strict'; ${ code } }();\n`;
89+
} else {
90+
code = importModules(list, format);
91+
}
92+
}
93+
94+
if (summary.comment.size) script += `/*\n * size: ${ (code.length / 1024).toFixed(2) }KB w/o comments\n */`;
95+
if (summary.comment.modules) script += `/*\n * modules:\n${ list.map(it => ` * ${ it }\n`).join('') } */`;
96+
if (code) script += `\n${ code }`;
97+
98+
if (summary.console.size) {
99+
console.log(`\u001B[32mbundling \u001B[36m${ TITLE }\u001B[32m, size: \u001B[36m${
100+
(script.length / 1024).toFixed(2)
101+
}KB\u001B[0m`);
102+
}
103+
104+
if (summary.console.modules) {
105+
console.log(`\u001B[32mbundling \u001B[36m${ TITLE }\u001B[32m, modules:\u001B[0m`);
106+
if (list.length) for (const it of list) {
107+
console.log(`\u001B[36m${ it + (targets ? ` \u001B[32mfor \u001B[36m${ JSON.stringify(compatTargets[it]) }` : '') }\u001B[0m`);
108+
} else console.log('\u001B[36mnothing\u001B[0m');
109+
}
110+
111+
if (!(filename === null || filename === undefined)) {
112+
await mkdir(dirname(filename), { recursive: true });
113+
await writeFile(filename, script);
114+
}
115+
116+
return { script };
117+
}

packages/core-js-builder/package.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"publishConfig": {
55
"access": "public"
66
},
7-
"type": "commonjs",
7+
"type": "module",
88
"description": "core-js builder",
99
"repository": {
1010
"type": "git",
@@ -26,12 +26,24 @@
2626
"url": "http://zloirock.ru"
2727
},
2828
"sideEffects": false,
29-
"main": "index.js",
29+
"main": "index.mjs",
30+
"exports": {
31+
".": {
32+
"types": "./index.d.ts",
33+
"default": "./index.mjs"
34+
},
35+
"./index.mjs": {
36+
"types": "./index.d.ts",
37+
"default": "./index.mjs"
38+
},
39+
"./config.mjs": "./config.mjs"
40+
},
3041
"types": "index.d.ts",
3142
"dependencies": {
32-
"core-js": "4.0.0-alpha.0",
3343
"@core-js/compat": "4.0.0-alpha.0",
34-
"webpack": "^5.104.1"
44+
"@swc/core": "^1.15.11",
45+
"core-js": "4.0.0-alpha.0",
46+
"rolldown": "^1.0.0-rc.2"
3547
},
3648
"engines": {
3749
"node": ">=20.19.0"

scripts/bundle-package/bundle-package.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { minify } from 'terser';
22
import builder from '@core-js/builder';
3-
import config from '@core-js/builder/config.js';
3+
import config from '@core-js/builder/config.mjs';
44

55
const { cyan, green } = chalk;
66
const ESMODULES = argv._.includes('esmodules');

scripts/update-version.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const shared = await readFile(SHARED, 'utf8');
3838
await writeFile(SHARED, shared.replaceAll(PREV_VERSION, NEW_VERSION).replaceAll(OLD_YEAR, CURRENT_YEAR));
3939

4040
const builderConfig = await readFile(BUILDER_CONFIG, 'utf8');
41-
await writeFile(BUILDER_CONFIG, builderConfig.replaceAll(OLD_YEAR, CURRENT_YEAR));
41+
await writeFile(BUILDER_CONFIG, builderConfig.replaceAll(PREV_VERSION, NEW_VERSION).replaceAll(OLD_YEAR, CURRENT_YEAR));
4242

4343
const usage = await readFile(USAGE, 'utf8');
4444
await writeFile(USAGE, usage.replaceAll(PREV_VERSION, NEW_VERSION).replaceAll(PREV_VERSION_MINOR, NEW_VERSION_MINOR));

0 commit comments

Comments
 (0)