ElMassimo / vite_ruby

⚡️ Vite.js in Ruby, bringing joy to your JavaScript experience
https://vite-ruby.netlify.app
MIT License
1.33k stars 122 forks source link

vite_rails: Improve default deployment and dev enviroment support #491

Open bamnet opened 2 months ago

bamnet commented 2 months ago

Rails 7.2+ ships with features like the ability to generate a devcontainer for development, Github Action to run tests and Rails 8 will ship with Kamal configured by default for deployment.

It would be helpful if vite_rails integrated into these systems automatically or (as a fallback) documented what steps users need to take in order to work with these new defaults.

At a first pass, I ran into trouble with:

There are some pointers in the existing docs, like a dependency on npx and some notes about devcontainers port forwarding, but I think it would be helpful to have very clear steps for Rails users as part of the vite_rails setup docs.

For example, I'm not sure if I should actually be running npm install in my Github Action or if there's a bug somewhere in vite:install_dependencies or other hooks which should automatically install the deps.

ElMassimo commented 2 months ago

Hi Brian!

The guide recommends running assets:precompile in the CI, which should install dependencies, although it's more robust to do it explicitly.

I'm not familiar with Kamal or Devcontainers, happy to include setup guides in the docs. Instructions should be concise, following the existing style of the docs. Alternatively, we can link to other resources that go into more detail of how to setup either.

mattbrictson commented 2 months ago

I have not used them personally, but I believe Kamal and Dev Containers are both based on Docker. When you run rails new and specify a Node-based build system, like --javascript esbuild, Rails will automatically inject the necessary Node steps into the generated Dockerfile, like this:

# Install JavaScript dependencies
ARG NODE_VERSION=20.17.0
ARG YARN_VERSION=1.22.22
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
    /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
    npm install -g yarn@$YARN_VERSION && \
    rm -rf /tmp/node-build-master

# Install node modules
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

Likewise, it adds a Node "feature" to .devcontainer/devcontainer.json:

"features": {
  "ghcr.io/devcontainers/features/node:1": {}
}

The problem is that Vite is not a supported option for rails new. For starting new Vite Rails apps, I recommend using rails new --javascript esbuild as a workaround to ensure the necessary Node pieces are in place, then proceed with the Vite installer.

The Vite Ruby docs could be updated to explain this, and/or mention specifically how to update the Docker files and GitHub actions workflow after the fact.