From d7620c8bd356032cfe2d20634c48ed7869177b80 Mon Sep 17 00:00:00 2001 From: Nick Novitski Date: Wed, 13 Jul 2022 20:52:41 -0700 Subject: [PATCH] Include fewer files in packages - Only include either one of `node_modules` or `.yarn/cache` directories - When a `files:` field is present in the package.json file, only include files matching those patterns --- src/tmpl/yarn-project.nix.in | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/tmpl/yarn-project.nix.in b/src/tmpl/yarn-project.nix.in index 983b2d9..414640b 100644 --- a/src/tmpl/yarn-project.nix.in +++ b/src/tmpl/yarn-project.nix.in @@ -137,15 +137,33 @@ let installPhase = '' runHook preInstall - mkdir -p $out/libexec $out/bin + mkdir -p "$out/libexec/$sourceRoot" "$out/bin" + + # Move the package contents to the output directory. + # - If the package.json has a `files` field, only files matching those patterns are copied + # - Otherwise all files are copied + yarn pack --out package.tgz + tar xzvf package.tgz --directory "$out/libexec/$name" --strip-components=1 + + cp .yarnrc* "$out/libexec/$name" + cp --recursive .yarn "$out/libexec/$name" + + # If the project uses the node-modules linker, then + # include the node_modules folder in the package. + if [ -d node_modules ]; then + cp --recursive node_modules "$out/libexec/$name" + fi - # Move the entire project to the output directory. - mv $PWD "$out/libexec/$name" cd "$out/libexec/$name" # Invoke a plugin internal command to setup binaries. yarn nixify install-bin $out/bin + # A package with node_modules doesn't need the cache + if [ -d node_modules ]; then + yarn cache clean + fi + runHook postInstall '';