nix/make-bundled: add minification option

Signed-off-by: Xe <me@christine.website>
This commit is contained in:
Xe 2022-12-09 00:12:21 -05:00
parent 89969955be
commit db3563f20f
2 changed files with 13 additions and 2 deletions

View file

@ -56,6 +56,7 @@
output = "bundled.js"; output = "bundled.js";
entrypoint = "./mod.ts"; entrypoint = "./mod.ts";
importMap = "./import_map.json"; importMap = "./import_map.json";
minify = true;
}; };
packages.bundled-wrapper = deno2nix.mkBundledWrapper { packages.bundled-wrapper = deno2nix.mkBundledWrapper {
pname = "example-bundled-wrapper"; pname = "example-bundled-wrapper";

View file

@ -11,6 +11,7 @@
lockfile, lockfile,
output ? "bundled.js", output ? "bundled.js",
outPath ? "dist", outPath ? "dist",
minify ? false,
entrypoint, entrypoint,
importMap ? null, importMap ? null,
additionalDenoFlags ? "", additionalDenoFlags ? "",
@ -19,7 +20,7 @@
in in
stdenv.mkDerivation { stdenv.mkDerivation {
inherit pname version entrypoint src; inherit pname version entrypoint src;
buildInputs = with pkgs; [deno jq]; buildInputs = with pkgs; [ deno jq nodePackages.uglify-js ];
buildPhase = '' buildPhase = ''
export DENO_DIR="/tmp/deno2nix" export DENO_DIR="/tmp/deno2nix"
@ -28,7 +29,7 @@ in
deno bundle \ deno bundle \
--lock="${lockfile}" \ --lock="${lockfile}" \
${ ${
if importMap != null if importMap != null
then "--import-map=\"$src/${importMap}\"" then "--import-map=\"$src/${importMap}\""
else "" else ""
@ -36,6 +37,15 @@ in
${additionalDenoFlags} \ ${additionalDenoFlags} \
"$src/${entrypoint}" \ "$src/${entrypoint}" \
"${output}" "${output}"
${
if minify
then ''
mv ${output} ${output}-non-min
uglifyjs ${output}-non-min -c -m > ${output}
''
else ""
}
''; '';
installPhase = '' installPhase = ''
mkdir -p $out/${outPath} mkdir -p $out/${outPath}