Skip to content

Commit 050e899

Browse files
committed
replace webpack with rolldown in builder
1 parent 5c6db9d commit 050e899

File tree

13 files changed

+625
-779
lines changed

13 files changed

+625
-779
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ jobs:
5050
strategy:
5151
matrix:
5252
node:
53-
- 20.9
54-
- 21
55-
- ^22.5.1
53+
- ^20.19
54+
- ^22.12
5655
- 23
5756
- 24
5857
- 25
@@ -74,9 +73,8 @@ jobs:
7473
- windows-latest
7574
- macos-latest
7675
node:
77-
- 20.9
78-
- 21
79-
- ^22.5.1
76+
- ^20.19
77+
- ^22.12
8078
- 23
8179
- 24
8280
- 25

.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.

packages/core-js-babel-plugin/package.json

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

packages/core-js-builder/package.json

Lines changed: 17 additions & 5 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,14 +26,26 @@
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": {
37-
"node": ">=20.19.0"
49+
"node": "^20.19.0 || >=22.12.0"
3850
}
3951
}

0 commit comments

Comments
 (0)