Skip to content

Commit

Permalink
fix: Add a project-local .envrc file.
Browse files Browse the repository at this point in the history
This fixes the issue described here:

#547 (comment)

It does so by configuring `direnv` to write its cache for this project
in `~/.cache/direnv/layouts`. This is a bit intrusive, but without
this workaround, `vite` commands search the local `.direnv` cache for
dependencies, and it adds about 10 seconds or more to every `vite`
command. Note that the fix comes from here:

https://github.com/direnv/direnv/wiki/Customizing-cache-location

Before falling back to this workaround, I also tried the following
Vite configuration tricks, but none of them worked:

https://vitejs.dev/config/server-options.html#server-watch
https://vitejs.dev/config/dep-optimization-options.html#optimizedeps-exclude

There are multiple (somewhat) related issues currently filed on Vite
which suggest that convincing Vite not to look everywhere in the
project root directory is a known issue:

https://github.com/vitejs/vite/pull/8778/files
vitejs/vite#7363
vitejs/vite#8341
  • Loading branch information
dhess committed Oct 10, 2022
1 parent d9a87b2 commit bb37048
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
13 changes: 13 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Prevent direnv from creating a local .direnv cache. Otherwise, 'vite'
# commands will be incredibly slow to start.

: ${XDG_CACHE_HOME:=$HOME/.cache}
declare -A direnv_layout_dirs
direnv_layout_dir() {
echo "${direnv_layout_dirs[$PWD]:=$(
local path="${PWD//[^a-zA-Z0-9]/-}"
echo "$XDG_CACHE_HOME/direnv/layouts/${path:1}"
)}"
}

use flake
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ result*

# direnv.
.direnv/
.envrc

# tsc
tsconfig.tsbuildinfo
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,7 @@ On the other hand, for production builds and CI, you can rest assured that TypeS

We've tried to configure the project such that LSP and other tooling should "just work" in your editor, but you still might need to do some configuration on your end to hook it up. At the very least, the Nix development shell provides binaries for the TypeScript language server and `eslint` support, so you shouldn't need to download these or otherwise install them in your environment. (However, note to Emacs LSP users: it may prompt you whether it should automatically download a helper, and in my experience, this is usually necessary. I haven't gotten to the bottom of why this is required. -- @dhess)

Probably the easiest way to ensure your editor sees the right environment is to add [`direnv`](https://direnv.net) support to your shell & editor, and then use [`nix-direnv`](https://github.com/nix-community/nix-direnv) to ensure that your editor sees the tools that the Nix shell provides. If you've got `nix-direnv` in your environment, you can do this by adding a file to the top-level project directory named `.envrc`, which contains just this one line:

```
use flake
```

If you need a tool that's not provided by the project's Nix development shell and you think others would benefit from it, as well, please make a PR!
Probably the easiest way to ensure your editor sees the right environment is to add [`direnv`](https://direnv.net) support to your shell & editor, and then use [`nix-direnv`](https://github.com/nix-community/nix-direnv) to ensure that your editor sees the tools that the Nix shell provides. The project includes an `.envrc` file which will do the right thing once you run `direnv allow` in the project root directory.

### CI & Chromatic

Expand Down

0 comments on commit bb37048

Please sign in to comment.