wrangler: reduce output size

The output size of `wrangler` is excessive as the entire `node_modules`
directory is copied into the output, alongside fixtures etc.

Instead use `pnpm deploy` to limit the dependencies (direct and
transitive) installed into `node_modules` to only those required for
`wrangler` at production time deployment.

Comparsion with nixpkgs version: 26.11.20260705.d407951:

- Before, du -hs /nix/store/20y9rbh92ix4zx0dxqirask23dv3r2xd-wrangler-4.93.0
  2.2G

- After, du -hs /nix/store/hspwm4nlr0y6zksylql1s3i3ngvsc272-wrangler-4.93.0
  357M
This commit is contained in:
magicquark
2026-07-07 04:34:34 +01:00
parent 2ef54b3bc3
commit 7370ea8c3c
+4 -15
View File
@@ -93,26 +93,15 @@ stdenv.mkDerivation (finalAttrs: {
done
'';
# I'm sure this is suboptimal but it seems to work. Points:
# - when build is run in the original repo, no specific executable seems to be generated; you run the resulting build with pnpm run start
# - this means we need to add a dedicated script - perhaps it is possible to create this from the workers-sdk dir, but I don't know how to do this
# - the build process builds a version of miniflare which is used by wrangler; for this reason, the miniflare package is copied also
# - pnpm stores all content in the top-level node_modules directory, but it is linked to from a node_modules directory inside wrangler
# - as they are linked via symlinks, the relative location of them on the filesystem should be maintained
# - Update: Now we're copying everything over due to broken symlink errors
installPhase = ''
runHook preInstall
mkdir -p $out/{bin,lib}
mv packages/~vitest-pool-workers packages/vitest-pool-workers
cp -r {fixtures,packages,node_modules} $out/lib
cp -r tools $out/lib/tools
rm -rf node_modules/typescript node_modules/eslint node_modules/prettier node_modules/bin node_modules/.bin node_modules/**/bin node_modules/**/.bin
rm -rf $out/lib/**/bin $out/lib/**/.bin
NODE_PATH_ARRAY=( "$out/lib/node_modules" "$out/lib/packages/wrangler/node_modules" )
pnpm config set --location=project injectWorkspacePackages true
pnpm --filter=wrangler --prod deploy $out/lib
makeWrapper ${lib.getExe nodejs} $out/bin/wrangler \
--inherit-argv0 \
--prefix-each NODE_PATH : "$${NODE_PATH_ARRAY[@]}" \
--add-flags $out/lib/packages/wrangler/bin/wrangler.js \
--set "NODE_PATH" $out/lib/node_modules \
--add-flags $out/lib/bin/wrangler.js \
--set-default SSL_CERT_FILE "${cacert}/etc/ssl/certs/ca-bundle.crt" # https://github.com/cloudflare/workers-sdk/issues/3264
runHook postInstall
'';