Skip to content

Commit d94d13c

Browse files
authored
BREAKING: Synchronise package with module template to fix ESM build (#124)
* Synchronise package with module template to fix ESM build * Dedupe dependencies
1 parent 942bcd8 commit d94d13c

19 files changed

+537
-290
lines changed

constraints.pro

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,17 @@ gen_enforced_field(WorkspaceCwd, 'license').
6767

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

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

7476
% The module entrypoint for the package must be `./dist/esm/index.js`.
7577
gen_enforced_field(WorkspaceCwd, 'module', './dist/esm/index.js').
78+
gen_enforced_field(WorkspaceCwd, 'exports["."].import', './dist/esm/index.js').
79+
80+
gen_enforced_field(WorkspaceCwd, 'exports["./package.json"]', './package.json').
7681

7782
% The list of files included in the package must only include files generated
7883
% during the build step.

package.json

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
"url": "https://github.com/MetaMask/utils.git"
1212
},
1313
"license": "ISC",
14+
"exports": {
15+
".": {
16+
"import": "./dist/esm/index.js",
17+
"require": "./dist/cjs/index.js",
18+
"types": "./dist/types/index.d.ts"
19+
},
20+
"./package.json": "./package.json"
21+
},
1422
"main": "./dist/cjs/index.js",
1523
"module": "./dist/esm/index.js",
1624
"types": "./dist/types/index.d.ts",
@@ -24,7 +32,8 @@
2432
"build:cjs": "swc src --out-dir dist/cjs --config-file .swcrc.build.json --config module.type=commonjs",
2533
"build:clean": "rimraf dist && yarn build",
2634
"build:docs": "typedoc",
27-
"build:esm": "swc src --out-dir dist/esm --config-file .swcrc.build.json --config module.type=es6",
35+
"build:esm": "swc src --out-dir dist/esm --config-file .swcrc.build.json --config module.type=es6 && yarn build:esm:package",
36+
"build:esm:package": "echo >dist/esm/package.json \"{\\\"type\\\":\\\"module\\\"}\"",
2837
"build:source": "yarn build:esm && yarn build:cjs",
2938
"build:types": "tsc --project tsconfig.build.json",
3039
"lint": "yarn lint:eslint && yarn lint:constraints && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog",
@@ -55,24 +64,25 @@
5564
"@lavamoat/allow-scripts": "^2.3.1",
5665
"@lavamoat/preinstall-always-fail": "^1.0.0",
5766
"@metamask/auto-changelog": "^3.1.0",
58-
"@metamask/eslint-config": "^11.0.1",
59-
"@metamask/eslint-config-jest": "^11.0.0",
60-
"@metamask/eslint-config-nodejs": "^11.0.1",
61-
"@metamask/eslint-config-typescript": "^11.0.0",
67+
"@metamask/eslint-config": "^12.0.0",
68+
"@metamask/eslint-config-jest": "^12.0.0",
69+
"@metamask/eslint-config-nodejs": "^12.0.0",
70+
"@metamask/eslint-config-typescript": "^12.0.0",
6271
"@swc/cli": "^0.1.62",
6372
"@swc/core": "^1.3.66",
6473
"@types/jest": "^28.1.7",
6574
"@types/node": "^17.0.23",
6675
"@typescript-eslint/eslint-plugin": "^5.43.0",
6776
"@typescript-eslint/parser": "^5.43.0",
6877
"depcheck": "^1.4.3",
69-
"eslint": "^8.27.0",
70-
"eslint-config-prettier": "^8.5.0",
71-
"eslint-plugin-import": "^2.26.0",
72-
"eslint-plugin-jest": "^27.1.5",
73-
"eslint-plugin-jsdoc": "^39.6.2",
74-
"eslint-plugin-node": "^11.1.0",
78+
"eslint": "^8.44.0",
79+
"eslint-config-prettier": "^8.8.0",
80+
"eslint-plugin-import": "^2.27.5",
81+
"eslint-plugin-jest": "^27.2.2",
82+
"eslint-plugin-jsdoc": "^39.9.1",
83+
"eslint-plugin-n": "^15.7.0",
7584
"eslint-plugin-prettier": "^4.2.1",
85+
"eslint-plugin-promise": "^6.1.1",
7686
"jest": "^29.2.2",
7787
"jest-it-up": "^2.0.2",
7888
"prettier": "^2.7.1",

src/assert.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { assert as assertSuperstruct, Struct } from 'superstruct';
1+
import type { Struct } from 'superstruct';
2+
import { assert as assertSuperstruct } from 'superstruct';
23

34
export type AssertionErrorConstructor =
45
| (new (args: { message: string }) => Error)
@@ -116,13 +117,13 @@ export function assert(
116117
* Defaults to {@link AssertionError}.
117118
* @throws If the value is not valid.
118119
*/
119-
export function assertStruct<T, S>(
120+
export function assertStruct<Type, Schema>(
120121
value: unknown,
121-
struct: Struct<T, S>,
122+
struct: Struct<Type, Schema>,
122123
errorPrefix = 'Assertion failed',
123124
// eslint-disable-next-line @typescript-eslint/naming-convention
124125
ErrorWrapper: AssertionErrorConstructor = AssertionError,
125-
): asserts value is T {
126+
): asserts value is Type {
126127
try {
127128
assertSuperstruct(value, struct);
128129
} catch (error) {

src/base64.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { is, size, string } from 'superstruct';
22

3-
import { base64, Base64Options } from './base64';
3+
import type { Base64Options } from './base64';
4+
import { base64 } from './base64';
45

56
describe('base64', () => {
67
it.each([

src/base64.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { pattern, Struct } from 'superstruct';
1+
import type { Struct } from 'superstruct';
2+
import { pattern } from 'superstruct';
23

34
import { assert } from './assert';
45

@@ -26,8 +27,8 @@ export type Base64Options = {
2627
* @param options - Optional options to specialize base64 validation. See {@link Base64Options} documentation.
2728
* @returns A superstruct validating base64.
2829
*/
29-
export const base64 = <T extends string, S>(
30-
struct: Struct<T, S>,
30+
export const base64 = <Type extends string, Schema>(
31+
struct: Struct<Type, Schema>,
3132
options: Base64Options = {},
3233
) => {
3334
const paddingRequired = options.paddingRequired ?? false;

src/bytes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { assert } from './assert';
2-
import { add0x, assertIsHexString, Hex, remove0x } from './hex';
2+
import type { Hex } from './hex';
3+
import { add0x, assertIsHexString, remove0x } from './hex';
34

45
// '0'.charCodeAt(0) === 48
56
const HEX_MINIMUM_NUMBER_CHARACTER = 48;

src/coercers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import type { Infer } from 'superstruct';
12
import {
23
bigint,
34
coerce,
45
create,
5-
Infer,
66
instance,
77
number,
88
string,
@@ -12,7 +12,8 @@ import {
1212

1313
import { assert } from './assert';
1414
import { bytesToHex, hexToBytes } from './bytes';
15-
import { Hex, StrictHexStruct } from './hex';
15+
import type { Hex } from './hex';
16+
import { StrictHexStruct } from './hex';
1617

1718
const NumberLikeStruct = union([number(), bigint(), string(), StrictHexStruct]);
1819
const NumberCoercer = coerce(number(), NumberLikeStruct, Number);

src/hex.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expectAssignable, expectNotAssignable } from 'tsd';
22

3-
import { Hex } from '.';
3+
import type { Hex } from '.';
44

55
// Valid hex strings:
66

src/hex.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import type { Hex } from './hex';
12
import {
2-
Hex,
33
add0x,
44
assertIsHexString,
55
assertIsStrictHexString,

src/hex.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { keccak_256 as keccak256 } from '@noble/hashes/sha3';
2-
import { is, pattern, string, Struct } from 'superstruct';
2+
import type { Struct } from 'superstruct';
3+
import { is, pattern, string } from 'superstruct';
34

45
import { assert } from './assert';
56
import { bytesToHex } from './bytes';

0 commit comments

Comments
 (0)