Skip to content

Instantly share code, notes, and snippets.

@rubys
Created December 20, 2021 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubys/b738bb9b7996a2a283d4d19edc096487 to your computer and use it in GitHub Desktop.
Save rubys/b738bb9b7996a2a283d4d19edc096487 to your computer and use it in GitHub Desktop.

Rails 7 + import maps + Lit component + TypeScript

This is a follow-on to Rails 7 + import maps + JSX proof of concept.

Overall flow:

  • Create project
    • rails new lit-ts-demo
    • Add sprocket-esbuild gem to bundle
    • Add monkey patch for importmap-rails to support sprocket transpilation
  • Prep for development of Lit elements
    • make a directory for javascript elements
      • pin the contents of the directory to the import map
      • link the directory to the manifest, specifying ts as the file extension
    • pin the lit and lit/decorators.js modules
  • Create and use the simple-greeting element
    • create simple-greeting.ts in the app/javascript/elements directory
    • import the simple-greeting element in the application
    • generate a Rails controller with a single view
    • make use of the simple-greeting element in the view

Start the server with ./bin/rails server, and visit http://localhost:3000/lit/demo

Shell script to do the above appears below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment