Add production build

This commit is contained in:
Yannik Rödel 2021-09-28 13:06:38 +02:00
parent 53242ef352
commit 70b351ef75
3 changed files with 69 additions and 40 deletions

2
.gitignore vendored
View file

@ -10,3 +10,5 @@ node_modules/
# Environment
.env
# `nix build` output
/result

View file

@ -26,5 +26,14 @@ for changes.
This repository also contains a [Nix flake](https://nixos.wiki/wiki/Flakes)
which contains the full development environment. `nix develop` will open a
shell with all required tools you don't need to run `npm install` anymore.
When node dependencies get updated (which changes `package.json`), make sure to
run `./_nix/update.sh` to update the npm lockfile and the Nix environment.
When Node dependencies are updated (this will change `package.json`), make sure
to run `./_nix/update.sh` to update the npm lockfile and the Nix environment.
The flake also contains a second package for building the production output:
```shell
nix build .#angestoepselt-site
```
The final derivation contains a `www` folder that can be served.

View file

@ -17,47 +17,65 @@
dontNpmInstall = true;
};
in {
# This package isn't actually the fully-built site, but rather a derivation
# that contains the relevant programs (with correctly set up environment)
# to develop and build the site. It can either be used with `nix develop`
# see the repository's readme for details or compiled with `nix build`.
# The latter will output a folder which contains node and npm binaries that
# can be used in an IDE.
packages.${system}.angestoepselt-site-dev = pkgs.runCommand "angestoepselt-site-dev" {
buildInputs = [ nodejs pkgs.makeWrapper ];
packages.${system} = {
angestoepselt-site = pkgs.stdenv.mkDerivation {
name = "angestoepselt-site";
src = self;
shellHook = ''
export NODE_PATH=${nodeDependencies}/lib/node_modules
export PATH="${nodeDependencies}/bin:$PATH"
buildInputs = [ nodejs nodeDependencies ];
echo ""
echo " To start editing content, run:"
echo ""
echo "npm run build:styles"
echo "npm run dev:site"
echo ""
echo " The site will be available under http://localhost:8080/ for"
echo " local development and rebuilds automatically when content"
echo " changes."
echo ""
buildPhase = ''
npm run build
'';
installPhase = ''
mkdir -p "$out"
cp -r _site "$out/www"
'';
};
# This package isn't actually the fully-built site, but rather a
# derivation that contains the relevant programs (with correctly set up
# environment) to develop and build the site. It can either be used with
# `nix develop` see the repository's readme for details or compiled
# with `nix build`. The latter will output a folder which contains node
# and npm binaries that can be used in an IDE.
angestoepselt-site-dev = pkgs.runCommand "angestoepselt-site-dev" {
buildInputs = [ nodejs pkgs.makeWrapper ];
shellHook = ''
export NODE_PATH=${nodeDependencies}/lib/node_modules
export PATH="${nodeDependencies}/bin:$PATH"
echo ""
echo " To start editing content, run:"
echo ""
echo "npm run build:styles"
echo "npm run dev:site"
echo ""
echo " The site will be available under http://localhost:8080/ for"
echo " local development and rebuilds automatically when content"
echo " changes."
echo ""
'';
} ''
mkdir -p "$out"
wrap() {
makeWrapper "${nodejs}/bin/$1" "$out/bin/$1" \
--prefix PATH : "${nodejs}/bin" \
--prefix PATH : "${nodeDependencies}/bin" \
--set-default NODE_PATH "${nodeDependencies}/lib/node_modules"
}
wrap node
wrap npm
wrap npx
unset -f wrap
'';
} ''
mkdir -p "$out"
};
wrap() {
makeWrapper "${nodejs}/bin/$1" "$out/bin/$1" \
--prefix PATH : "${nodejs}/bin" \
--prefix PATH : "${nodeDependencies}/bin" \
--set-default NODE_PATH "${nodeDependencies}/lib/node_modules"
}
wrap node
wrap npm
wrap npx
unset -f wrap
'';
defaultPackage.${system} = self.packages.${system}.angestoepselt-site;
defaultPackage.${system} = self.packages.${system}.angestoepselt-site-dev;
};
}