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

esbuild-rails

v1.0.7

Published

Esbuild plugin for Rails applications

Downloads

179,931

Readme

npm version

🛤 esbuild-rails

Esbuild Rails plugin for easy imports of Stimulus controllers, ActionCable channels, and other Javascript.

This package is designed to be used with jsbundling-rails.

⚙️ Installation

Install with npm or yarn

yarn add esbuild-rails
npm i esbuild-rails

Copy examples/esbuild.config.mjs to your git repository or use the following example config:

const path = require('path')
const rails = require('esbuild-rails')

require("esbuild").build({
  entryPoints: ["application.js"],
  bundle: true,
  outdir: path.join(process.cwd(), "app/assets/builds"),
  absWorkingDir: path.join(process.cwd(), "app/javascript"),
  plugins: [rails()],
}).catch(() => process.exit(1))

Use npm to add it as the build script (requires npm >= 7.1)

npm set-script build "node esbuild.config.js"

or add it manually in package.json

"scripts": {
  "build": "node esbuild.config.mjs"
}

🧑‍💻 Usage

Import a folder using globs:

import "./src/**/*"

Import Stimulus controllers and register them:

import { Application } from "@hotwired/stimulus"
const application = Application.start()

import controllers from "./**/*_controller.js"
controllers.forEach((controller) => {
  application.register(controller.name, controller.module.default)
})

Importing Stimulus controllers from parent folders (ViewComponents, etc)

To import Stimulus controllers from parents in other locations, create an index.js in the folder that registers controllers and import the index.js location.

For example, we can import Stimulus controller for ViewComponents by creating an app/components/index.js file and importing that in your main Stimulus controllers index.

// app/javascript/controllers/index.js
import { application } from "./application"

// Import app/components/index.js
import "../../components"
// app/components/index.js
import { application } from "../javascript/controllers/application"

import controllers from "./**/*_controller.js"
controllers.forEach((controller) => {
  application.register(controller.name, controller.module.default)
})

Import ActionCable channels:

import "./channels/**/*_channel.js"

jQuery with esbuild:

yarn add jquery
// app/javascript/jquery.js
import jquery from 'jquery';
window.jQuery = jquery;
window.$ = jquery;
//app/javascript/application.js
import "./jquery"

Why does this work? import in Javascript is hoisted, meaning that import is run before the other code regardless of where in the file they are. By splitting the jQuery setup into a separate import, we can guarantee that code runs first. Read more here.

jQuery UI with esbuild:

Follow the jQuery steps above.

Download jQuery UI custom build and add it to app/javascript/jquery-ui.js

import "./jquery-ui"

$(function() {
  $(document).tooltip()
  $("#dialog").dialog()
})

A custom build is required because jQueryUI does not support ESM.

🙏 Contributing

If you have an issue you'd like to submit, please do so using the issue tracker in GitHub. In order for us to help you in the best way possible, please be as detailed as you can.

📝 License

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