deno2.nix/nix/make-bundled.nix
Xe db3563f20f nix/make-bundled: add minification option
Signed-off-by: Xe <me@christine.website>
2022-12-09 00:12:21 -05:00

55 lines
1.1 KiB
Nix

{
pkgs,
stdenv,
deno2nix,
lib,
...
}: {
pname,
version,
src,
lockfile,
output ? "bundled.js",
outPath ? "dist",
minify ? false,
entrypoint,
importMap ? null,
additionalDenoFlags ? "",
}: let
inherit (deno2nix.internal) mkDepsLink;
in
stdenv.mkDerivation {
inherit pname version entrypoint src;
buildInputs = with pkgs; [ deno jq nodePackages.uglify-js ];
buildPhase = ''
export DENO_DIR="/tmp/deno2nix"
mkdir -p $DENO_DIR
ln -s "${mkDepsLink lockfile}" $(deno info --json | jq -r .modulesCache)
deno bundle \
--lock="${lockfile}" \
${
if importMap != null
then "--import-map=\"$src/${importMap}\""
else ""
} \
${additionalDenoFlags} \
"$src/${entrypoint}" \
"${output}"
${
if minify
then ''
mv ${output} ${output}-non-min
uglifyjs ${output}-non-min -c -m > ${output}
''
else ""
}
'';
installPhase = ''
mkdir -p $out/${outPath}
install -t $out/${outPath} "${output}"
'';
}