Skip to content

Commit

Permalink
BREAKING: Synchronise package with module template to fix ESM build (M…
Browse files Browse the repository at this point in the history
…etaMask#124)

* Synchronise package with module template to fix ESM build

* Dedupe dependencies
  • Loading branch information
Mrtenz authored Jul 24, 2023
1 parent 942bcd8 commit d94d13c
Show file tree
Hide file tree
Showing 19 changed files with 537 additions and 290 deletions.
5 changes: 5 additions & 0 deletions constraints.pro
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ gen_enforced_field(WorkspaceCwd, 'license').

% The type definitions entrypoint the package must be `./dist/types/index.d.ts`.
gen_enforced_field(WorkspaceCwd, 'types', './dist/types/index.d.ts').
gen_enforced_field(WorkspaceCwd, 'exports["."].types', './dist/types/index.d.ts').

% The entrypoint for the package must be `./dist/cjs/index.js`.
gen_enforced_field(WorkspaceCwd, 'main', './dist/cjs/index.js').
gen_enforced_field(WorkspaceCwd, 'exports["."].require', './dist/cjs/index.js').

% The module entrypoint for the package must be `./dist/esm/index.js`.
gen_enforced_field(WorkspaceCwd, 'module', './dist/esm/index.js').
gen_enforced_field(WorkspaceCwd, 'exports["."].import', './dist/esm/index.js').

gen_enforced_field(WorkspaceCwd, 'exports["./package.json"]', './package.json').

% The list of files included in the package must only include files generated
% during the build step.
Expand Down
32 changes: 21 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
"url": "https://github.com/MetaMask/utils.git"
},
"license": "ISC",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js",
"types": "./dist/types/index.d.ts"
},
"./package.json": "./package.json"
},
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
Expand All @@ -24,7 +32,8 @@
"build:cjs": "swc src --out-dir dist/cjs --config-file .swcrc.build.json --config module.type=commonjs",
"build:clean": "rimraf dist && yarn build",
"build:docs": "typedoc",
"build:esm": "swc src --out-dir dist/esm --config-file .swcrc.build.json --config module.type=es6",
"build:esm": "swc src --out-dir dist/esm --config-file .swcrc.build.json --config module.type=es6 && yarn build:esm:package",
"build:esm:package": "echo >dist/esm/package.json \"{\\\"type\\\":\\\"module\\\"}\"",
"build:source": "yarn build:esm && yarn build:cjs",
"build:types": "tsc --project tsconfig.build.json",
"lint": "yarn lint:eslint && yarn lint:constraints && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog",
Expand Down Expand Up @@ -55,24 +64,25 @@
"@lavamoat/allow-scripts": "^2.3.1",
"@lavamoat/preinstall-always-fail": "^1.0.0",
"@metamask/auto-changelog": "^3.1.0",
"@metamask/eslint-config": "^11.0.1",
"@metamask/eslint-config-jest": "^11.0.0",
"@metamask/eslint-config-nodejs": "^11.0.1",
"@metamask/eslint-config-typescript": "^11.0.0",
"@metamask/eslint-config": "^12.0.0",
"@metamask/eslint-config-jest": "^12.0.0",
"@metamask/eslint-config-nodejs": "^12.0.0",
"@metamask/eslint-config-typescript": "^12.0.0",
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.66",
"@types/jest": "^28.1.7",
"@types/node": "^17.0.23",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"depcheck": "^1.4.3",
"eslint": "^8.27.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.1.5",
"eslint-plugin-jsdoc": "^39.6.2",
"eslint-plugin-node": "^11.1.0",
"eslint": "^8.44.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jest": "^27.2.2",
"eslint-plugin-jsdoc": "^39.9.1",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.1.1",
"jest": "^29.2.2",
"jest-it-up": "^2.0.2",
"prettier": "^2.7.1",
Expand Down
9 changes: 5 additions & 4 deletions src/assert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert as assertSuperstruct, Struct } from 'superstruct';
import type { Struct } from 'superstruct';
import { assert as assertSuperstruct } from 'superstruct';

export type AssertionErrorConstructor =
| (new (args: { message: string }) => Error)
Expand Down Expand Up @@ -116,13 +117,13 @@ export function assert(
* Defaults to {@link AssertionError}.
* @throws If the value is not valid.
*/
export function assertStruct<T, S>(
export function assertStruct<Type, Schema>(
value: unknown,
struct: Struct<T, S>,
struct: Struct<Type, Schema>,
errorPrefix = 'Assertion failed',
// eslint-disable-next-line @typescript-eslint/naming-convention
ErrorWrapper: AssertionErrorConstructor = AssertionError,
): asserts value is T {
): asserts value is Type {
try {
assertSuperstruct(value, struct);
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion src/base64.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { is, size, string } from 'superstruct';

import { base64, Base64Options } from './base64';
import type { Base64Options } from './base64';
import { base64 } from './base64';

describe('base64', () => {
it.each([
Expand Down
7 changes: 4 additions & 3 deletions src/base64.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pattern, Struct } from 'superstruct';
import type { Struct } from 'superstruct';
import { pattern } from 'superstruct';

import { assert } from './assert';

Expand Down Expand Up @@ -26,8 +27,8 @@ export type Base64Options = {
* @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.
* @returns A superstruct validating base64.
*/
export const base64 = <T extends string, S>(
struct: Struct<T, S>,
export const base64 = <Type extends string, Schema>(
struct: Struct<Type, Schema>,
options: Base64Options = {},
) => {
const paddingRequired = options.paddingRequired ?? false;
Expand Down
3 changes: 2 additions & 1 deletion src/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assert } from './assert';
import { add0x, assertIsHexString, Hex, remove0x } from './hex';
import type { Hex } from './hex';
import { add0x, assertIsHexString, remove0x } from './hex';

// '0'.charCodeAt(0) === 48
const HEX_MINIMUM_NUMBER_CHARACTER = 48;
Expand Down
5 changes: 3 additions & 2 deletions src/coercers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Infer } from 'superstruct';
import {
bigint,
coerce,
create,
Infer,
instance,
number,
string,
Expand All @@ -12,7 +12,8 @@ import {

import { assert } from './assert';
import { bytesToHex, hexToBytes } from './bytes';
import { Hex, StrictHexStruct } from './hex';
import type { Hex } from './hex';
import { StrictHexStruct } from './hex';

const NumberLikeStruct = union([number(), bigint(), string(), StrictHexStruct]);
const NumberCoercer = coerce(number(), NumberLikeStruct, Number);
Expand Down
2 changes: 1 addition & 1 deletion src/hex.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectAssignable, expectNotAssignable } from 'tsd';

import { Hex } from '.';
import type { Hex } from '.';

// Valid hex strings:

Expand Down
2 changes: 1 addition & 1 deletion src/hex.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Hex } from './hex';
import {
Hex,
add0x,
assertIsHexString,
assertIsStrictHexString,
Expand Down
3 changes: 2 additions & 1 deletion src/hex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { keccak_256 as keccak256 } from '@noble/hashes/sha3';
import { is, pattern, string, Struct } from 'superstruct';
import type { Struct } from 'superstruct';
import { is, pattern, string } from 'superstruct';

import { assert } from './assert';
import { bytesToHex } from './bytes';
Expand Down
6 changes: 3 additions & 3 deletions src/json.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Infer, Struct } from 'superstruct';
import {
any,
array,
boolean,
coerce,
create,
define,
Infer,
integer,
is,
lazy,
Expand All @@ -17,12 +17,12 @@ import {
optional,
record,
string,
Struct,
union,
unknown,
} from 'superstruct';

import { AssertionErrorConstructor, assertStruct } from './assert';
import type { AssertionErrorConstructor } from './assert';
import { assertStruct } from './assert';

/**
* Any JSON-compatible value.
Expand Down
4 changes: 2 additions & 2 deletions src/keyring.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { TypedTransaction, TxData } from '@ethereumjs/tx';

import type { Eip1024EncryptedData } from './encryption-types';
import { Hex } from './hex';
import { Json } from './json';
import type { Hex } from './hex';
import type { Json } from './json';

/**
* A Keyring class.
Expand Down
3 changes: 2 additions & 1 deletion src/logging.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import debug, { Debugger } from 'debug';
import type { Debugger } from 'debug';
import debug from 'debug';

const globalLogger = debug('metamask');

Expand Down
8 changes: 2 additions & 6 deletions src/misc.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { expectAssignable, expectNotAssignable, expectType } from 'tsd';

import {
isObject,
hasProperty,
getKnownPropertyNames,
RuntimeObject,
} from './misc';
import type { RuntimeObject } from './misc';
import { isObject, hasProperty, getKnownPropertyNames } from './misc';

//=============================================================================
// isObject
Expand Down
2 changes: 1 addition & 1 deletion src/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { RuntimeObject } from '.';
import {
isNonEmptyArray,
isNullOrUndefined,
isObject,
hasProperty,
getKnownPropertyNames,
RuntimeObject,
isPlainObject,
calculateNumberSize,
isASCII,
Expand Down
4 changes: 2 additions & 2 deletions src/transaction-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Bytes } from './bytes';
import { Hex } from './hex';
import type { Bytes } from './bytes';
import type { Hex } from './hex';

export type Transaction =
| LegacyTransaction
Expand Down
3 changes: 1 addition & 2 deletions src/versions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SemVerRange, SemVerVersion } from './versions';
import {
assertIsSemVerRange,
assertIsSemVerVersion,
Expand All @@ -6,8 +7,6 @@ import {
isValidSemVerRange,
isValidSemVerVersion,
satisfiesVersionRange,
SemVerRange,
SemVerVersion,
} from './versions';

describe('assertIsSemVerVersion', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import {
valid as validSemVerVersion,
validRange as validSemVerRange,
} from 'semver';
import { is, refine, string, Struct } from 'superstruct';
import type { Struct } from 'superstruct';
import { is, refine, string } from 'superstruct';

import { assertStruct } from './assert';
import { Opaque } from './opaque';
import type { Opaque } from './opaque';

/**
* {@link https://codemix.com/opaque-types-in-javascript/ Opaque} type for SemVer ranges.
Expand Down
Loading

0 comments on commit d94d13c

Please sign in to comment.