Skip to content

Commit

Permalink
Add typescript declarations for core-js-compat
Browse files Browse the repository at this point in the history
Reuse it for the core-js-builder declarations to prevent duplicated code

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Apr 8, 2023
1 parent e4433a8 commit f80bb74
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 44 deletions.
48 changes: 4 additions & 44 deletions packages/core-js-builder/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,4 @@
type StringOrRegExp = string | RegExp;

type Modules = StringOrRegExp | readonly StringOrRegExp[];

type Target =
| 'android'
| 'bun'
| 'chrome'
| 'chrome-android'
| 'deno'
| 'edge'
| 'electron'
| 'firefox'
| 'firefox-android'
| 'hermes'
| 'ie'
| 'ios'
| 'node'
| 'opera'
| 'opera-android'
| 'phantom'
| 'quest'
| 'react-native'
| 'rhino'
| 'safari'
| 'samsung';

type BrowserslistQuery = string | ReadonlyArray<string>;

type Environments = {
[target in Target]?: string | number;
};

type Targets = Environments & {
browsers?: Environments | BrowserslistQuery,
esmodules?: boolean,
};
import type compat from "core-js-compat";

type Format = 'bundle' | 'esm' | 'cjs';

Expand All @@ -50,13 +14,9 @@ type Summary = {
console?: SummaryEntry,
};

type Options = {
/** entry / module / namespace / an array of them, by default - all `core-js` modules */
modules?: Modules,
/** a blacklist, entry / module / namespace / an array of them, by default - empty list */
exclude?: Modules,
/** optional browserslist or core-js-compat format query */
targets?: Targets | BrowserslistQuery,
type CompatOptions = Exclude<Parameters<typeof compat.compat>[0], undefined>;

type Options = Pick<CompatOptions, "exclude" | "modules" | "targets"> & {
/** 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?: Format,
Expand Down
93 changes: 93 additions & 0 deletions packages/core-js-compat/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
type StringOrRegExp = string | RegExp;

type Modules = StringOrRegExp | readonly StringOrRegExp[];

type ModuleName = string;

type Target =
| 'android'
| 'bun'
| 'chrome'
| 'chrome-android'
| 'deno'
| 'edge'
| 'electron'
| 'firefox'
| 'firefox-android'
| 'hermes'
| 'ie'
| 'ios'
| 'node'
| 'opera'
| 'opera-android'
| 'phantom'
| 'quest'
| 'react-native'
| 'rhino'
| 'safari'
| 'samsung';

type BrowserslistQuery = string | ReadonlyArray<string>;

type Environments = {
[target in Target]?: string | number;
};

type Targets = Environments & {
browsers?: Environments | BrowserslistQuery,
esmodules?: boolean,
};

type Options = {
/** entry / module / namespace / an array of them, by default - all `core-js` modules */
modules?: Modules,
/** a blacklist, entry / module / namespace / an array of them, by default - empty list */
exclude?: Modules,
/** optional browserslist or core-js-compat format query */
targets?: Targets | BrowserslistQuery,
/** used `core-js` version, by default the latest */
version?: string,
/** inverse of the result, shows modules that are NOT required for the target environment */
inverse?: boolean,
/**
* @deprecated use `modules` instead
*/
filter?: Modules
};

type TargetVersion = string;

type Output = {
/** array of required modules */
list: ModuleName[],
/** object with targets for each module */
targets: {
[module: ModuleName]: {
[target in Target]?: TargetVersion
}
}
}

type CompatData = {
[module: ModuleName]: {
[target in Target]?: TargetVersion
}
};

declare const ExportedCompatObject: {
compat(options?: Options): Output,

/** The subset of modules which available in the passed `core-js` version */
getModulesListForTargetVersion(version: string): readonly ModuleName[],

/** Full list compatibility data */
data: CompatData,

/** map of modules by `core-js` entry points */
entries: {[entry_point: string]: readonly ModuleName[]},

/** Full list of modules */
modules: readonly ModuleName[]
}

export = ExportedCompatObject
1 change: 1 addition & 0 deletions packages/core-js-compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"sideEffects": false,
"main": "index.js",
"types": "index.d.ts",
"dependencies": {
"browserslist": "^4.21.5"
}
Expand Down

0 comments on commit f80bb74

Please sign in to comment.