Skip to content

Commit

Permalink
add format option to core-js-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 6, 2022
1 parent 25f9bd4 commit 894d71e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 32 deletions.
24 changes: 17 additions & 7 deletions packages/core-js-builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ For some cases could be useful to exclude some `core-js` features or generate a
import builder from 'core-js-builder';

const bundle = await builder({
modules: ['core-js/actual', 'esnext.reflect'], // entries / modules / namespaces, by default - all `core-js` modules
exclude: [/^es\.math\./, 'es.number.constructor'], // a blacklist of entries / modules / namespaces, by default - empty list
targets: '> 0.5%, not dead, ie 9-11', // optional browserslist or core-js-compat format query
summary: { // shows summary for the bundle, disabled by default:
console: { size: true, modules: false }, // in the console, you could specify required parts or set `true` for enable all of them
comment: { size: false, modules: true }, // in the comment in the target file, similarly to `summary.console`
// entry / module / namespace / an array of them, by default - all `core-js` modules
modules: ['core-js/actual', /^esnext\.reflect\./],
// a blacklist of entries / modules / namespaces, by default - empty list
exclude: [/^es\.math\./, 'es.number.constructor'],
// optional browserslist or core-js-compat format query
targets: '> 0.5%, not dead, ie 9-11',
// shows summary for the bundle, disabled by default
summary: {
// in the console, you could specify required parts or set `true` for enable all of them
console: { size: true, modules: false },
// in the comment in the target file, similarly to `summary.console`
comment: { size: false, modules: true },
},
filename: PATH_TO_MY_COREJS_BUNDLE, // optional target filename, if it's missed a file will not be created
// 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',
// optional target filename, if it's missed a file will not be created
filename: PATH_TO_MY_COREJS_BUNDLE,
});
```
59 changes: 34 additions & 25 deletions packages/core-js-builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,53 @@ module.exports = async function ({
blacklist = null, // TODO: Obsolete, remove from `core-js@4`
exclude = [],
targets = null,
format = 'bundle',
filename = null,
summary = {},
} = {}) {
if (!['bundle', 'cjs', 'esm'].includes(format)) throw TypeError('Incorrect output type');
summary = { comment: normalizeSummary(summary.comment), console: normalizeSummary(summary.console) };

const TITLE = filename != null ? filename : '`core-js`';
let script = banner;
let code = '';
let code = '\n';

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

if (list.length) {
const tempFileName = `core-js-${ Math.random().toString(36).slice(2) }.js`;
const tempFile = join(tmpdir, tempFileName);
if (format === 'bundle') {
const tempFileName = `core-js-${ Math.random().toString(36).slice(2) }.js`;
const tempFile = join(tmpdir, tempFileName);

await webpack({
mode: 'none',
node: {
global: false,
process: false,
setImmediate: false,
},
entry: list.map(it => require.resolve(`core-js/modules/${ it }`)),
output: {
filename: tempFileName,
hashFunction: 'md5',
path: tmpdir,
},
});
await webpack({
mode: 'none',
node: {
global: false,
process: false,
setImmediate: false,
},
entry: list.map(it => require.resolve(`core-js/modules/${ it }`)),
output: {
filename: tempFileName,
hashFunction: 'md5',
path: tmpdir,
},
});

const file = await readFile(tempFile);
const file = await readFile(tempFile);

await unlink(tempFile);
await unlink(tempFile);

code = `!function (undefined) { 'use strict'; ${
// compress `__webpack_require__` with `keep_fnames` option
String(file).replace(/function __webpack_require__/, 'var __webpack_require__ = function ')
} }();`;
code = `!function (undefined) { 'use strict'; ${
// compress `__webpack_require__` with `keep_fnames` option
String(file).replace(/function __webpack_require__/, 'var __webpack_require__ = function ')
} }();\n`;
} else {
const template = it => format === 'esm'
? `import 'core-js/modules/${ it }.js';\n`
: `require('core-js/modules/${ it }');\n`;
code = list.map(template).join('');
}
}

if (summary.comment.size) script += `/*\n * size: ${ (code.length / 1024).toFixed(2) }KB w/o comments\n */`;
Expand All @@ -81,9 +90,9 @@ module.exports = async function ({

if (summary.console.modules) {
console.log(`\u001B[32mbundling \u001B[36m${ TITLE }\u001B[32m, modules:\u001B[0m`);
for (const it of list) {
if (list.length) for (const it of list) {
console.log(`\u001B[36m${ it + (targets ? ` \u001B[32mfor \u001B[36m${ JSON.stringify(compatTargets[it]) }` : '') }\u001B[0m`);
}
} else console.log('\u001B[36mnothing\u001B[0m');
}

if (filename != null) {
Expand Down

0 comments on commit 894d71e

Please sign in to comment.