Skip to content

Commit 7f736a0

Browse files
authored
Use TypeScript Project References with PNPM Workspaces (#4137)
Given colocation of package and app sources, this repository lends itself well to engendering further mono-repository tooling. This PR proposes utilizing [TypeScript Project References](https://www.typescriptlang.org/docs/handbook/project-references.html) to source inter-repository packages locally from the repository rather than relying on published artifacts. **Implications:** - No longer must packages be published publicly before utilizing package changes in consuming applications. - All workspace enabled projects in the repository depend directly on the live code for colocated packages, rather than on published artifacts. - Package changes can now be "dogfooded" prior to planned public release. - LSP developer experience is improved such that "go-to" calls point directly to in-repository source rather than `node_modules/*`. - `test/*` files can now import code exactly as an artifact consumer would (e.g. `import * as _ from "@unionlabs/sdk"` rather than `import * as _ from "../src/index.js"`) which allows for unit tests to serve as usage examples. It is proposed to leverage PNPM over NPM to additionally improve mono-repository tooling: - PNPM should be preferred over NPM for the following reasons: - Mature handling for workspaces. - Defense against [phantom dependencies](https://rushjs.io/pages/advanced/phantom_deps/). - Defense against [NPM doppelgangers](https://rushjs.io/pages/advanced/npm_doppelgangers/). - Strict handling for peer dependencies. - PNPM workspaces allows for patches to be declared once and inherited by all workspace projects (without the need for `patch-package`). ~This PR additionally introduces, for `@unionlabs/sdk`, the same build system as `effect` which produces tree-shakeable module-based path imports in both CJS and ESM formatting.~ CJS determined not a necessary artifact target. `@unionlabs/sdk` build system been adjusted to leverage `tsc` enhanced with `babel` for annotating pure calls which should improve tree-shaking with certain bundlers (especially `webpack`). **Miscellaneous:** - Some packages (e.g. `effect` and `viem`) have been moved to `peerDependencies` where appropriate and gated by major version number such that consumers are not at risk of bundling multiple versions. - `"DOM"` added to `@unionlabs/client` TS config `"lib"` entry to ensure global `Crypto` API is well-defined, which was previously pointing to a `bun` stub. - [`dpdm`](https://github.com/acrazing/dpdm) added to `@unionlabs/sdk` to detect circular dependencies. - Path-based imports (e.g. `#/path/after/package/src`) have been removed in `@unionlabs/client` as it provides no benefit and causes issues with project references. - Project-speciifc Nix files (e.g. `app2/app2.nix`, `ts-sdk/ts-sdk.nix`) have been preserved—`buildPnpmPackage.nix` function is provided to make usage easier than traditional `bulidNpmPackage`. **Caveats:** - This does not touch `app` or `sentinel` projects—only projects required to develop `app2`. ~- Import paths (and thereby modules) must be well-defined in `@unionlabs/sdk`, which means removal of barrel files (good from a performance standpoint).~ (slated for future enhancement)
2 parents ccb2907 + 2ce471b commit 7f736a0

File tree

85 files changed

+9975
-15802
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+9975
-15802
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@ rustc-ice*
8686
.docs
8787
profiles/
8888
contracts.json
89+
dist/
90+
*.tsbuildinfo

app2/app2.nix

+34-20
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,58 @@
11
_: {
22
perSystem =
33
{
4-
lib,
54
pkgs,
65
pkgsUnstable,
76
ensureAtRepositoryRoot,
7+
lib,
88
...
99
}:
1010
let
11+
buildPnpmPackage = import ../tools/typescript/buildPnpmPackage.nix {
12+
inherit pkgs lib;
13+
};
1114
deps = with pkgsUnstable; [
1215
python3
1316
stdenv.cc
1417
pkg-config
1518
nodePackages_latest.nodejs
16-
nodePackages_latest."patch-package"
19+
pnpm_10
1720
];
18-
packageJSON = lib.importJSON ./package.json;
1921
in
2022
{
2123
packages = {
22-
app2 = pkgsUnstable.buildNpmPackage {
23-
npmDepsHash = "sha256-cgmgvbMV9lIFG4YEA5W2Vk3uvjUIp2BNWcQrEmrortE=";
24-
src = ./.;
25-
sourceRoot = "app2";
26-
npmFlags = [ "--legacy-peer-deps" ];
27-
pname = packageJSON.name;
28-
inherit (packageJSON) version;
29-
nativeBuildInputs = deps;
24+
app2 = buildPnpmPackage rec {
25+
packageJsonPath = ./package.json;
26+
extraSrcs = [
27+
../app2
28+
../typescript-sdk
29+
../ts-sdk
30+
];
31+
hash = "sha256-nR7H08cOxhjn1PgbvdBdDoLahV+xQCFw0Xpoi2oCfII=";
3032
buildInputs = deps;
33+
nativeBuildInputs = buildInputs;
34+
pnpmWorkspaces = [
35+
"app2"
36+
"@unionlabs/sdk"
37+
"@unionlabs/client"
38+
];
39+
buildPhase = ''
40+
runHook preBuild
41+
pnpm --filter=app2 prepare
42+
pnpm --filter=app2 build
43+
runHook postBuild
44+
'';
45+
checkPhase = ''
46+
pnpm --filter=app2 check
47+
'';
48+
doCheck = false; # TODO(ehegnes): enable checks
3149
installPhase = ''
3250
mkdir -p $out
33-
cp -r ./build/* $out
51+
cp -r ./app2/build/* $out
3452
'';
3553
doDist = false;
36-
NODE_OPTIONS = "--no-warnings";
3754
};
3855
};
39-
4056
apps = {
4157
app2-dev-server = {
4258
type = "app";
@@ -46,9 +62,8 @@ _: {
4662
text = ''
4763
${ensureAtRepositoryRoot}
4864
cd app2/
49-
50-
npm install
51-
npm run dev -- --host
65+
pnpm install
66+
pnpm run dev -- --host
5267
'';
5368
};
5469
};
@@ -60,9 +75,8 @@ _: {
6075
text = ''
6176
${ensureAtRepositoryRoot}
6277
cd app2/
63-
npx gql.tada generate-schema --tsconfig ./tsconfig.json --output "./src/generated/schema.graphql" "https://development.graphql.union.build/v1/graphql"
64-
65-
npx gql.tada generate-output --disable-preprocessing --tsconfig ./tsconfig.json --output ./src/generated/graphql-env.d.ts
78+
pnpm dlx gql.tada generate-schema --tsconfig ./tsconfig.json --output "./src/generated/schema.graphql" "https://development.graphql.union.build/v1/graphql"
79+
pnpm dlx gql.tada generate-output --disable-preprocessing --tsconfig ./tsconfig.json --output ./src/generated/graphql-env.d.ts
6680
'';
6781
};
6882
};

0 commit comments

Comments
 (0)