npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite_rb

v0.0.1-alpha1

Published

An npm package for integration with ViteRb

Downloads

1

Readme

Vite

WORK IN PROGRESS

If you would like to help support the future of this project, please consider sponsoring me so I can keep a regular stream of updates and fixes to this project.

https://github.com/sponsors/ParamagicDev

Please note, that this project is still in it's infancy. Feel free to file bug reports, issues, and feature requests.

Gem Version

This gem integrates the Vite JS module bundler into your Rails / Ruby application. It is inspired by gems such as breakfast / webpacker and this project started as a fork of Vite.

This is not meant to be a 1:1 replacement of Webpacker. ViteRb is actually just a wrapper around Vite using Rake / Thor and as a result can be used with any Rack based framework. Vite comes with first class support for Rails including Generators and autoloaded helpers.

How is Vite different?

Vite is unbundled during development to eliminate compilation times. Asset requests are cached with 304 headers. Bundling is saved for the final build process due to waterfall network requests that still cause some issues in production and can cause significant slowdown.

Vite uses the native ESM module spec. ESM Modules are fast, lightweight, and natively supported by all newer browsers ("evergreen browsers") For more reading on ESM modules, check out this link:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

Installation

Add this line to your application's Gemfile:

gem 'vite_rb', git: "https://github.com/ParamagicDev/vite_rb"

With Rails

rails vite:init

Which will install your yarn packages, create an initializer file, add config files, and create an app/vite directory similar to Webpacker.

Tasks

rails vite:dev # starts a dev server
rails vite:build # builds for production (is hooked onto
precompile)
rails assets:precompile # will build vite and the asset pipeline

Existing Rails app

When working with a new Rails app, it is important to switch any webpack require statements to ESM-based import. For example, consider the following javascript file:

// app/javascript/packs/application.js

- // Webpack
- require("@rails/ujs").start()
- require("turbolinks").start()
- require("@rails/activestorage").start()
- require("channels")
-
+ // app/vite/entrypoints/applications.js
+ import "@rails/ujs" // Autostarts
+ import Turbolinks from "turbolinks"
+ import ActiveStorage from "@rails/activestorage"
+ import "../channels"
+
+ Turbolinks.start()
+ ActiveStorage.start()

You may notice a require.context statement in your javascript to load channels. This runs via Node and is not browser compatible. Vite comes with a glob import syntax, you can read about it here: @TODO

// app/javascript/channels/index.js

// Load all the channels within this directory and all subdirectories.
// Channel files must be named *_channel.js.

- const channels = require.context('.', true, /_channel\.js$/)
- channels.keys().forEach(channels)
// @TODO

File Structure

Vite makes some assumptions about your file paths to provide helper methods.

tree -L 2 app/vite

app/vite/
├── assets/
│   └── picture.png
├── channels/
│   ├── consumer.js
│   └── index.js
├── entrypoints/
│   └── application.js
├── javascript/
│   └── index.js
└── stylesheets/
    └── index.css

Which upon build will output to look like this:

tree -L 1 public/Vites

public/vite/
├── assets/
├── channels/
├── entrypoints/
├── javascript/
├── css/

Helpers

Generic Helper

<%= vite_path %> will return the value of Vite.config.output_dir

Assets

Assets can be accessed via <%= vite_asset_path("name", **options) %> and accepts all the same params as #asset_path

Channels

Channels have no special helper.

Packs

Packs can be accessed via:

<%= javascript_vite_tag %> and works the same as #javascript_include_tag

packs are your "entrypoints" and where files get bundled to, very similar to Webpacker.

Javascript

Javascript files have no special helper.

Stylesheets

Stylesheets can be accessed via:

<%= stylesheet_vite_tag %> and works just like #stylesheet_link_tag. I recommend importing your css in your packs file if you plan on changing it to support HMR.

HMR

To enable HMR for your vite assets, put the following in the <head> of your document:

<%= vite_hmr_tag %>

Configuration

After running generator, the configuration file can be found in config/initializers/vite.rb

In addition, all related vite.config.js, and postcss.config.js can all be found in the config/vite directory.

Production

ViteRb hooks up to the rails assets:precompile and rails assets:clobber, so no special setup is required.

You can start vite_rb's compilation process manually by running

rails vite:compile

# OR

rails vite:build

Examples

Examples can be found in the /examples directory.

Converting from Webpack to Snowpack

  • require.context() is not currently supported

Instead, use the glob syntax from Vite

Issues

Not all packages may be compatible with Vite. Please check skypack.dev for ESM-compatible packages.

Changelog

See CHANGELOG.md

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ParamagicDev/vite_rb.

License

The gem is available as open source under the terms of the MIT License.

Roadmap

  • [ ] Add default file structure with init

  • [x] Support require.context (Glob importing supported by Vite)

  • [ ] Reading from production manifest

  • [ ] Parity with Webpacker helper methods

  • [ ] Add documentation / installation on Stimulus

  • [ ] Create an npm package to read a default config from and pin Vite versions

  • [ ] Add in End-to-end testing to confirm everything works as intended.

  • [ ] Make the config environment from NPM in Typescript so users can import types for good completion.