diff --git a/jest.config.packages.js b/jest.config.packages.js index a7c6bc1e1..bb3ef91aa 100644 --- a/jest.config.packages.js +++ b/jest.config.packages.js @@ -77,19 +77,23 @@ module.exports = { // ], // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module - // Here we ensure that Jest resolves `@metamask/*` imports to the uncompiled source code for packages that live in this repo. - // NOTE: This must be synchronized with the `paths` option in `tsconfig.base.json`. + // Resolve workspace `@metamask/*` imports to uncompiled source. Paths are + // relative to the monorepo root, not to the consuming package. When a local + // `packages//src` directory does not exist, use the installed package. + // NOTE: This must be synchronized with the `paths` option in `tsconfig.packages.json`. moduleNameMapper: { '^@metamask/json-rpc-engine/v2$': [ '/../json-rpc-engine/src/v2/index.ts', ], '^@metamask/utils/node$': require.resolve('@metamask/utils/node'), - '^@metamask/(.+)$': [ - '/../$1/src', - // Some @metamask/* packages we are referencing aren't in this monorepo, - // so in that case use their published versions + '^@metamask/([^/]+)$': [ + '/../../packages/$1/src', '/../../node_modules/@metamask/$1', ], + '^@metamask/([^/]+)/(.+)$': [ + '/../../packages/$1/src/$2', + '/../../node_modules/@metamask/$1/$2', + ], }, // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader diff --git a/packages/bitcoin-wallet-snap/CHANGELOG.md b/packages/bitcoin-wallet-snap/CHANGELOG.md index 570452d8e..328ed5236 100644 --- a/packages/bitcoin-wallet-snap/CHANGELOG.md +++ b/packages/bitcoin-wallet-snap/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- Resolve workspace `@metamask/*` TypeScript types from the monorepo root instead of assuming sibling directories ([#167](https://github.com/MetaMask/internal-snaps/pull/167)) +- Set `baseUrl` in this package's `tsconfig.json` and restate the workspace `@metamask/*` path mapping relative to it, so the package keeps `baseUrl`-relative import ergonomics without losing correct workspace-source resolution ([#169](https://github.com/MetaMask/internal-snaps/pull/169)) + ## [2.0.1] ### Fixed diff --git a/packages/bitcoin-wallet-snap/snap.manifest.json b/packages/bitcoin-wallet-snap/snap.manifest.json index f594f203d..9e59617f5 100644 --- a/packages/bitcoin-wallet-snap/snap.manifest.json +++ b/packages/bitcoin-wallet-snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/internal-snaps.git" }, "source": { - "shasum": "jV649WZbbfbj3FpOMD5U/xDPuRD0t4F+pxCoy08a/O0=", + "shasum": "PpNYFDDadcYp9uuFWMdaURUB5G8vsXrwOETgpFEzUW4=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/bitcoin-wallet-snap/tsconfig.json b/packages/bitcoin-wallet-snap/tsconfig.json index a8a6abb0f..a2d47b2cf 100644 --- a/packages/bitcoin-wallet-snap/tsconfig.json +++ b/packages/bitcoin-wallet-snap/tsconfig.json @@ -1,19 +1,18 @@ { - "extends": "../../tsconfig.packages.json", + "extends": "../../tsconfig.snaps.json", "compilerOptions": { "baseUrl": "./", + "paths": { + "@metamask/*": ["../../packages/*/src"], + "@metamask/snap-networks-utils/*": [ + "../../packages/snap-networks-utils/src/*" + ] + }, "lib": ["ES2021", "DOM"], - "resolveJsonModule": true /* lets us import JSON modules from within TypeScript modules. */, - "jsx": "react-jsx", - "jsxImportSource": "@metamask/snaps-sdk", "exactOptionalPropertyTypes": false, "forceConsistentCasingInFileNames": true, "noErrorTruncation": true, - "noUncheckedIndexedAccess": true, - "skipLibCheck": true, - "module": "ESNext", - "moduleResolution": "bundler", - "types": ["jest"] + "noUncheckedIndexedAccess": true }, "include": ["**/*.ts", "**/*.tsx", "locales/*.json"] } diff --git a/packages/sample-snap/tsconfig.json b/packages/sample-snap/tsconfig.json index 6db6f2381..e4e53f917 100644 --- a/packages/sample-snap/tsconfig.json +++ b/packages/sample-snap/tsconfig.json @@ -1,11 +1,10 @@ { - "extends": "../../tsconfig.packages.json", + "extends": "../../tsconfig.snaps.json", "compilerOptions": { "baseUrl": "./", - "jsx": "react-jsx", - "skipLibCheck": true, - "jsxImportSource": "@metamask/snaps-sdk", - "types": ["jest"] + "paths": { + "@metamask/*": ["../../packages/*/src"] + } }, "include": ["**/*.ts", "**/*.tsx", "locales/*.json"] } diff --git a/packages/snap-networks-utils/CHANGELOG.md b/packages/snap-networks-utils/CHANGELOG.md index dac68b80a..f8b975e58 100644 --- a/packages/snap-networks-utils/CHANGELOG.md +++ b/packages/snap-networks-utils/CHANGELOG.md @@ -12,6 +12,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **BREAKING** Replace the logger utilities with a configurable `Logger` class that requires a log level and supports level filtering, per-instance prefixes, and method decorators. - Bump `@metamask/utils` from `^11.9.0` to `^11.11.9` ([#161](https://github.com/MetaMask/internal-snaps/pull/161)) +- Set `baseUrl` in this package's `tsconfig.json` and restate the workspace `@metamask/*` path mapping relative to it, so the package keeps `baseUrl`-relative import ergonomics without losing correct workspace-source resolution ([#169](https://github.com/MetaMask/internal-snaps/pull/169)) + +### Fixed + +- Assign optional `Logger` `prefix` and `decorators` only when they are provided so `exactOptionalPropertyTypes` consumers can typecheck against workspace source ([#167](https://github.com/MetaMask/internal-snaps/pull/167)) ## [1.0.0] diff --git a/packages/snap-networks-utils/src/logger/Logger.ts b/packages/snap-networks-utils/src/logger/Logger.ts index 57230867b..87dce9c41 100644 --- a/packages/snap-networks-utils/src/logger/Logger.ts +++ b/packages/snap-networks-utils/src/logger/Logger.ts @@ -128,8 +128,12 @@ export class Logger { assert(level, LogLevelStruct); this.#level = level; - this.#prefix = prefix; - this.#decorators = decorators; + if (prefix !== undefined) { + this.#prefix = prefix; + } + if (decorators !== undefined) { + this.#decorators = decorators; + } } /** @@ -146,7 +150,9 @@ export class Logger { return new Logger({ level: this.#level, prefix: this.#prefix ? `${this.#prefix} ${prefix}` : prefix, - decorators: this.#decorators, + ...(this.#decorators === undefined + ? {} + : { decorators: this.#decorators }), }); } diff --git a/packages/snap-networks-utils/tsconfig.json b/packages/snap-networks-utils/tsconfig.json index 464677940..7848658f2 100644 --- a/packages/snap-networks-utils/tsconfig.json +++ b/packages/snap-networks-utils/tsconfig.json @@ -2,6 +2,9 @@ "extends": "../../tsconfig.packages.json", "compilerOptions": { "baseUrl": "./", + "paths": { + "@metamask/*": ["../../packages/*/src"] + }, "skipLibCheck": true, "types": ["jest"] }, diff --git a/packages/solana-wallet-snap/CHANGELOG.md b/packages/solana-wallet-snap/CHANGELOG.md index ab6dd1dbf..2a9fb09b6 100644 --- a/packages/solana-wallet-snap/CHANGELOG.md +++ b/packages/solana-wallet-snap/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Resolve workspace `@metamask/*` TypeScript types from the monorepo root instead of assuming sibling directories ([#167](https://github.com/MetaMask/internal-snaps/pull/167)) +- No changes needed here for the per-package `baseUrl` alternative: this package already set its own `baseUrl` and restated the workspace `@metamask/*` path mapping relative to it, which is the exact pattern the rest of the monorepo adopts in this PR ([#169](https://github.com/MetaMask/internal-snaps/pull/169)) - Extract Snap-owned assets domain logic into `SnapAssetsAdapter`; `AssetsService` is a thin facade that delegates metadata, market data, fetch, persist, and account asset reads through the adapter (no Core routing yet). ([#121](https://github.com/MetaMask/internal-snaps/pull/121)) - Align `AssetsService` read API with `snap-networks-utils` / AssetsController shapes by adding `getAccountAssetByID`, `getAccountAssetsByIDs`, `getAccountAssetsByScope`, and `getAccountAssets`, and routing Keyring and Send through them (still Snap-owned storage). ([#120](https://github.com/MetaMask/internal-snaps/pull/120)) diff --git a/packages/solana-wallet-snap/snap.manifest.json b/packages/solana-wallet-snap/snap.manifest.json index 57d2ca99d..9ec50e91d 100644 --- a/packages/solana-wallet-snap/snap.manifest.json +++ b/packages/solana-wallet-snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/internal-snaps.git" }, "source": { - "shasum": "2c2WNzBfLdP/UYJdepbBVpyYrpjzuSob/iobC9JJRLo=", + "shasum": "ogadUr7kiFH3ZTJLvNrBtK3z9EpDy1UGFLA+DSe0XVU=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/solana-wallet-snap/tsconfig.json b/packages/solana-wallet-snap/tsconfig.json index 3eb63f359..7031f2018 100644 --- a/packages/solana-wallet-snap/tsconfig.json +++ b/packages/solana-wallet-snap/tsconfig.json @@ -1,20 +1,19 @@ { - "extends": "../../tsconfig.packages.json", + "extends": "../../tsconfig.snaps.json", "compilerOptions": { "baseUrl": "./", - "jsx": "react-jsx", - "jsxImportSource": "@metamask/snaps-sdk", - "resolveJsonModule": true, + "paths": { + "@metamask/*": ["../../packages/*/src"], + "@metamask/snap-networks-utils/*": [ + "../../packages/snap-networks-utils/src/*" + ] + }, + "lib": ["ES2023", "DOM"], + "target": "es2023", "exactOptionalPropertyTypes": true, "forceConsistentCasingInFileNames": true, "noErrorTruncation": true, - "noUncheckedIndexedAccess": true, - "skipLibCheck": true, - "lib": ["ES2023", "DOM"], - "target": "es2023", - "module": "preserve", - "moduleResolution": "bundler", - "types": ["jest"] + "noUncheckedIndexedAccess": true }, "include": ["**/*.ts", "**/*.tsx", "locales/*.json"] } diff --git a/packages/stellar-wallet-snap/CHANGELOG.md b/packages/stellar-wallet-snap/CHANGELOG.md index e6268ac1d..02d715a89 100644 --- a/packages/stellar-wallet-snap/CHANGELOG.md +++ b/packages/stellar-wallet-snap/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Resolve workspace `@metamask/*` TypeScript types from the monorepo root instead of assuming sibling directories ([#167](https://github.com/MetaMask/internal-snaps/pull/167)) +- Set `baseUrl` in this package's `tsconfig.json` and restate the workspace `@metamask/*` path mapping relative to it, so the package keeps `baseUrl`-relative import ergonomics without losing correct workspace-source resolution ([#169](https://github.com/MetaMask/internal-snaps/pull/169)) - This package was migrated from [snap-stellar-wallet](https://github.com/MetaMask/snap-stellar-wallet). See the source repository for the original [changelog](https://github.com/MetaMask/snap-stellar-wallet/blob/main/packages/snap/CHANGELOG.md) ([#161](https://github.com/MetaMask/internal-snaps/pull/161)) ### Fixed diff --git a/packages/stellar-wallet-snap/tsconfig.json b/packages/stellar-wallet-snap/tsconfig.json index df5a8f6b6..131d44905 100644 --- a/packages/stellar-wallet-snap/tsconfig.json +++ b/packages/stellar-wallet-snap/tsconfig.json @@ -1,19 +1,16 @@ { - "extends": "../../tsconfig.packages.json", + "extends": "../../tsconfig.snaps.json", "compilerOptions": { - "resolveJsonModule": true /* lets us import JSON modules from within TypeScript modules. */, - "jsx": "react-jsx", - "jsxImportSource": "@metamask/snaps-sdk", + "baseUrl": "./", + "paths": { + "@metamask/*": ["../../packages/*/src"] + }, + "lib": ["ES2023", "DOM"], + "target": "es2023", "exactOptionalPropertyTypes": false, "forceConsistentCasingInFileNames": true, "noErrorTruncation": true, - "noUncheckedIndexedAccess": true, - "skipLibCheck": true, - "lib": ["ES2023", "DOM"], - "target": "es2023", - "module": "preserve", - "moduleResolution": "bundler", - "types": ["jest"] + "noUncheckedIndexedAccess": true }, "include": ["**/*.ts", "**/*.tsx", "locales/*.json"] } diff --git a/packages/tron-wallet-snap/CHANGELOG.md b/packages/tron-wallet-snap/CHANGELOG.md index 733239f5e..c2d969352 100644 --- a/packages/tron-wallet-snap/CHANGELOG.md +++ b/packages/tron-wallet-snap/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Resolve workspace `@metamask/*` TypeScript types from the monorepo root instead of assuming sibling directories ([#167](https://github.com/MetaMask/internal-snaps/pull/167)) +- Set `baseUrl` in this package's `tsconfig.json` and restate the workspace `@metamask/*` path mapping relative to it, so the package keeps `baseUrl`-relative import ergonomics without losing correct workspace-source resolution ([#169](https://github.com/MetaMask/internal-snaps/pull/169)) - Extract shared asset util functions and inject `SnapAssetsAdapter` from `context` into `AssetsService` ([#143](https://github.com/MetaMask/internal-snaps/pull/143)) - Rename `getByKeyringAccountId` to `getAccountAssets` (with essential-asset synthesis) and update keyring callers ([#143](https://github.com/MetaMask/internal-snaps/pull/143)) diff --git a/packages/tron-wallet-snap/snap.manifest.json b/packages/tron-wallet-snap/snap.manifest.json index fe36b7959..317ca5920 100644 --- a/packages/tron-wallet-snap/snap.manifest.json +++ b/packages/tron-wallet-snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/internal-snaps.git" }, "source": { - "shasum": "MbqwOXbHFI83/qWOj9zDSXJizgEt5oQ+QnpHq0g/sls=", + "shasum": "Dgq9UwYX7J/Mo+Ubvm4cwZGPlea3fC/CPtq7jJhReec=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/tron-wallet-snap/tsconfig.json b/packages/tron-wallet-snap/tsconfig.json index df5a8f6b6..e555d015e 100644 --- a/packages/tron-wallet-snap/tsconfig.json +++ b/packages/tron-wallet-snap/tsconfig.json @@ -1,19 +1,19 @@ { - "extends": "../../tsconfig.packages.json", + "extends": "../../tsconfig.snaps.json", "compilerOptions": { - "resolveJsonModule": true /* lets us import JSON modules from within TypeScript modules. */, - "jsx": "react-jsx", - "jsxImportSource": "@metamask/snaps-sdk", + "baseUrl": "./", + "paths": { + "@metamask/*": ["../../packages/*/src"], + "@metamask/snap-networks-utils/*": [ + "../../packages/snap-networks-utils/src/*" + ] + }, + "lib": ["ES2023", "DOM"], + "target": "es2023", "exactOptionalPropertyTypes": false, "forceConsistentCasingInFileNames": true, "noErrorTruncation": true, - "noUncheckedIndexedAccess": true, - "skipLibCheck": true, - "lib": ["ES2023", "DOM"], - "target": "es2023", - "module": "preserve", - "moduleResolution": "bundler", - "types": ["jest"] + "noUncheckedIndexedAccess": true }, "include": ["**/*.ts", "**/*.tsx", "locales/*.json"] } diff --git a/scripts/create-package/package-template/tsconfig.json b/scripts/create-package/package-template/tsconfig.json index 025ba2ef7..fabfb3eca 100644 --- a/scripts/create-package/package-template/tsconfig.json +++ b/scripts/create-package/package-template/tsconfig.json @@ -1,8 +1,5 @@ { "extends": "../../tsconfig.packages.json", - "compilerOptions": { - "baseUrl": "./" - }, "references": [], "include": ["../../types", "./src"] } diff --git a/tsconfig.packages.json b/tsconfig.packages.json index a655abc1f..5710af502 100644 --- a/tsconfig.packages.json +++ b/tsconfig.packages.json @@ -5,14 +5,23 @@ "extends": "./tsconfig.base.json", "compilerOptions": { /** - * Here we ensure that TypeScript resolves `@metamask/*` imports to the - * uncompiled source code for packages that live in this repo. + * Resolve `@metamask/*` imports to uncompiled source for packages in this + * repo. Paths are relative to this file (the monorepo root). + * + * A local candidate is used only when `packages//src` exists. + * Published `@metamask/*` dependencies fall through to `node_modules`. + * TypeScript path patterns allow only one `*`, so workspace packages + * with extra entry points need an explicit subpath mapping. * * NOTE: This must be synchronized with the `moduleNameMapper` option in - * `jest.config.packages.js`. + * `jest.config.packages.js`. Consuming configs should not set `baseUrl` + * unless they also re-declare these paths relative to that `baseUrl`. */ "paths": { - "@metamask/*": ["../*/src"] + "@metamask/*": ["./packages/*/src"], + "@metamask/snap-networks-utils/*": [ + "./packages/snap-networks-utils/src/*" + ] } } } diff --git a/tsconfig.snaps.json b/tsconfig.snaps.json new file mode 100644 index 000000000..8fe9ffbfd --- /dev/null +++ b/tsconfig.snaps.json @@ -0,0 +1,28 @@ +{ + /** + * Shared TypeScript settings for Snap packages. + * + * Snaps are bundled with `mm-snap` and are not part of the root + * `tsc --build` graph, so `composite` is disabled here. That lets + * workspace `@metamask/*` path mappings resolve to sibling `src/` + * directories without requiring project-reference build artifacts. + * + * Do not set `baseUrl` here. Each package sets its own `baseUrl` (for + * package-relative ergonomics, e.g. `src/...` imports) and must restate + * the `@metamask/*` workspace mapping from `tsconfig.packages.json` + * relative to that `baseUrl`, since inherited `paths` resolve against + * whichever `baseUrl` is present in the final merged config, not the + * config file where `paths` was declared. + */ + "extends": "./tsconfig.packages.json", + "compilerOptions": { + "composite": false, + "jsx": "react-jsx", + "jsxImportSource": "@metamask/snaps-sdk", + "module": "preserve", + "moduleResolution": "bundler", + "resolveJsonModule": true, + "skipLibCheck": true, + "types": ["jest"] + } +}