Skip to content

Commit

Permalink
some improvements of core-js-compat/compat tool
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Apr 10, 2022
1 parent e605083 commit 3cad08a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 52 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
- Fixed work of non-standard V8 `Error` features with wrapped `Error` constructors, [#1061](https://github.com/zloirock/core-js/issues/1061)
- `null` and `undefined` allowed as the second argument of `structuredClone`, [#1056](https://github.com/zloirock/core-js/issues/1056)
- Tooling:
- Stabilized proposals are filtered out from the `core-js-compat` -> `core-js-builder` -> `core-js-bundle` output. That mean that if the output contains, for example, `es.object.has-own`, the legacy reference to it, `esnext.object.has-own`, will not be added.
- Stabilized proposals are filtered out from the `core-js-compat` -> `core-js-builder` -> `core-js-bundle` output. That mean that if the output contains, for example, `es.object.has-own`, the legacy reference to it, `esnext.object.has-own`, no longer added.
- `modules` option of `core-js-compat` replaces `filter` for consistency with `core-js-builder`
- Added support of entry points, arrays of them and arrays of regex to modules filter of `core-js-compat`, [see the docs](https://github.com/zloirock/core-js/tree/master/packages/core-js-builder)
- Missed `targets` option in `core-js-compat` means that the `targets` filter just will not be applied, so the result will contain modules required for all possible engines
- Compat data:
- `.stack` property on `DOMException` marked as supported from Deno [1.15](https://github.com/denoland/deno/releases/tag/v1.15.0)
- Added Deno 1.21 compat data mapping
Expand Down
14 changes: 7 additions & 7 deletions packages/core-js-builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ function normalizeSummary(unit = {}) {
}

module.exports = async function ({
blacklist, // TODO: Remove from `core-js@4`
exclude = [],
modules = modulesList.slice(),
targets,
filename,
blacklist = null, // TODO: Obsolete, remove from `core-js@4`
exclude = [],
targets = null,
filename = null,
summary = {},
} = {}) {
summary = { comment: normalizeSummary(summary.comment), console: normalizeSummary(summary.console) };
Expand All @@ -53,13 +53,13 @@ module.exports = async function ({
}

filter('add', modules);
filter('delete', blacklist || exclude);
filter('delete', exclude == null ? blacklist : exclude);

// eslint-disable-next-line sonarjs/no-empty-collection -- false positive
modules = filterOutStabilizedProposals(modulesList.filter(it => set.has(it)));

if (targets) {
const compatResult = compat({ targets, filter: modules });
const compatResult = compat({ targets, modules });
modules = compatResult.list;
modulesWithTargets = compatResult.targets;
}
Expand Down Expand Up @@ -111,7 +111,7 @@ module.exports = async function ({
console.log(JSON.stringify(modulesWithTargets || modules, null, ' '));
}

if (typeof filename != 'undefined') {
if (filename != null) {
await mkdirp(dirname(filename));
await writeFile(filename, script);
}
Expand Down
69 changes: 38 additions & 31 deletions packages/core-js-compat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,48 @@

```js
const {
list, // array of required modules
targets, // object with targets for each module
list, // array of required modules
targets, // object with targets for each module
} = require('core-js-compat')({
targets: '> 2.5%', // browserslist query or object of minimum environment versions to support
filter: /^(es|web)\./, // optional filter - string-prefix, regexp or list of modules
version: '3.21', // used `core-js` version, by default - the latest
targets: '> 1%', // browserslist query or object of minimum environment versions to support, see below
modules: [ // optional modules list / filter - regex, sting or an array of them:
'core-js/actual', // - an entry point
'esnext.array.unique-by', // - a module name (or just a start of a module name)
/^web\./, // - regex that a module name must satisfy
],
version: '3.21', // used `core-js` version, by default - the latest
});

console.log(targets);
/* =>
{
'es.symbol.match-all': { ios: '12.2-12.4' },
'es.array.unscopables.flat': { ios: '12.2-12.4' },
'es.array.unscopables.flat-map': { ios: '12.2-12.4' },
'es.math.hypot': { chrome: '77' },
'es.promise.all-settled': { firefox: '69', ios: '12.2-12.4' },
'es.promise.finally': { ios: '12.2-12.4' },
'es.string.match-all': { chrome: '77', firefox: '69', ios: '12.2-12.4' },
'es.string.replace': { firefox: '69', ios: '12.2-12.4' },
'es.typed-array.float32-array': { ios: '12.2-12.4' },
'es.typed-array.float64-array': { ios: '12.2-12.4' },
'es.typed-array.int8-array': { ios: '12.2-12.4' },
'es.typed-array.int16-array': { ios: '12.2-12.4' },
'es.typed-array.int32-array': { ios: '12.2-12.4' },
'es.typed-array.uint8-array': { ios: '12.2-12.4' },
'es.typed-array.uint8-clamped-array': { ios: '12.2-12.4' },
'es.typed-array.uint16-array': { ios: '12.2-12.4' },
'es.typed-array.uint32-array': { ios: '12.2-12.4' },
'es.typed-array.from': { ios: '12.2-12.4' },
'es.typed-array.of': { ios: '12.2-12.4' },
'web.dom-collections.iterator': { ios: '12.2-12.4' },
'web.immediate': { chrome: '77', firefox: '69', ios: '12.2-12.4' },
'web.url': { ios: '12.2-12.4' },
'web.url.to-json': { ios: '12.2-12.4' },
'web.url-search-params': { ios: '12.2-12.4' }
'es.error.cause': { ios: '14.5-14.8', samsung: '16.0' },
'es.aggregate-error.cause': { ios: '14.5-14.8', samsung: '16.0' },
'es.array.at': { ios: '14.5-14.8' },
'es.object.has-own': { ios: '14.5-14.8', samsung: '16.0' },
'es.string.at-alternative': { ios: '14.5-14.8' },
'es.typed-array.at': { ios: '14.5-14.8' },
'es.typed-array.set': { samsung: '16.0' },
'esnext.array.find-last': { firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.array.find-last-index': { firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.array.group-by': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.array.group-by-to-map': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.array.to-reversed': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.array.to-sorted': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.array.to-spliced': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.array.unique-by': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.array.with': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.symbol.replace-all': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.typed-array.find-last': { firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.typed-array.find-last-index': { firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.typed-array.group-by': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.typed-array.to-reversed': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.typed-array.to-sorted': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.typed-array.to-spliced': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'esnext.typed-array.with': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'web.dom-exception.stack': { chrome: '98', edge: '99', ios: '14.5-14.8', samsung: '16.0' },
'web.immediate': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' },
'web.structured-clone': { chrome: '98', edge: '99', firefox: '98', ios: '14.5-14.8', samsung: '16.0' }
}
*/
```
Expand Down Expand Up @@ -74,9 +81,9 @@ console.log(targets);

```js
// equals of of the method from the example above
require('core-js-compat/compat')({ targets, filter, version }); // => { list: Array<ModuleName>, targets: { [ModuleName]: { [EngineName]: EngineVersion } } }
require('core-js-compat/compat')({ targets, modules, version }); // => { list: Array<ModuleName>, targets: { [ModuleName]: { [EngineName]: EngineVersion } } }
// or
require('core-js-compat').compat({ targets, filter, version }); // => { list: Array<ModuleName>, targets: { [ModuleName]: { [EngineName]: EngineVersion } } }
require('core-js-compat').compat({ targets, modules, version }); // => { list: Array<ModuleName>, targets: { [ModuleName]: { [EngineName]: EngineVersion } } }

// full compat data:
require('core-js-compat/data'); // => { [ModuleName]: { [EngineName]: EngineVersion } }
Expand Down
44 changes: 31 additions & 13 deletions packages/core-js-compat/compat.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
'use strict';
const { compare, filterOutStabilizedProposals, has, intersection } = require('./helpers');
const data = require('./data');
const entries = require('./entries');
const getModulesListForTargetVersion = require('./get-modules-list-for-target-version');
const modules = require('./modules');
const allModules = require('./modules');
const targetsParser = require('./targets-parser');

function getModules(filter) {
if (typeof filter == 'string') {
if (has(entries, filter)) return entries[filter];
return allModules.filter(it => it.startsWith(filter));
} else if (filter instanceof RegExp) return allModules.filter(it => filter.test(it));
throw TypeError('Wrong filter!');
}

function checkModule(name, targets) {
if (!has(data, name)) throw new TypeError(`Incorrect module: ${ name }`);

Expand All @@ -24,30 +33,39 @@ function checkModule(name, targets) {
return result;
}

module.exports = function ({ targets, filter, version }) {
const parsedTargets = targetsParser(targets);
module.exports = function ({
filter = null, // TODO: Obsolete, remove from `core-js@4`
modules = null,
targets = null,
version = null,
} = {}) {
if (modules == null) modules = filter;

const parsedTargets = targets ? targetsParser(targets) : null;

const result = {
list: [],
targets: {},
};

let $modules = Array.isArray(filter) ? filter : modules;

if (filter instanceof RegExp) {
$modules = $modules.filter(it => filter.test(it));
} else if (typeof filter == 'string') {
$modules = $modules.filter(it => it.startsWith(filter));
}

$modules = filterOutStabilizedProposals($modules);
let $modules;
if (modules) {
const list = Array.isArray(modules) ? modules : [modules];
$modules = [...new Set([].concat.apply([], list.map(getModules)))];
} else $modules = allModules;

if (version) {
$modules = intersection($modules, getModulesListForTargetVersion(version));
}

$modules = filterOutStabilizedProposals($modules);

for (const key of $modules) {
const check = checkModule(key, parsedTargets);
const check = parsedTargets ? checkModule(key, parsedTargets) : {
required: true,
targets: {},
};

if (check.required) {
result.list.push(key);
result.targets[key] = check.targets;
Expand Down

0 comments on commit 3cad08a

Please sign in to comment.