Generate a LitElement project faster than sunlight

Generate a LitElement project faster than sunlight

Get started with a fully-tooled LitElement project in less than 8 minutes!

ยท

7 min read

๐Ÿ‘‹ Hi people!

My name's Todd. I'm a software engineer with a passion for the web. This is my very first post on HN ๐ŸŽ‰ Any feedback is truly appreciated, even if you're a jerk about it ๐Ÿ˜‰

Today I want to show you the fastest (and simplest) way I've found to create a fully-tooled LitElement project from scratch. All with a single command.


TL;DR

For those of you just looking for a command to copy to get going, let me empower you to do so:

$ npm init @open-wc

LitElement Briefly

This forward-thinking project from the Polymer team makes it easy to create stupidly-fast, standards-based web components. It has a similar developer experience to most mainstream component frameworks like React, for example...

LitElement itself is a simply a lightweight base class based upon native web component spec.

LitElement components can be used anywhere you can use HTML and JS! Sharing components is a breeze, across projects or even entire organizations! This portability can serve your team well for years to come.


Despite your current framework of choice, LitElement is definitely worth checking out!


Motivation

While the official documentation is great overall, their "getting started" section is not exactly straightforward. Their suggested path to "Hello World" seemed rather wishy-washy and task-heavy for a truly quick start. This, coupled with the fact that they present both starting paths for Typescript and Javascript in tandem, made for a less-than-straightforward first foray into LitElement. I appreciate simplicity when trying new tech.

I want you to avoid the hassle altogether, and start building cool stuff today:
Enter Open-WC project generator.

NOTE: This generator is suggested by the official docs as an alternative start; however, at the time of this writing the link is currently broken ๐Ÿ˜ฒ As a concerned and empowered citizen of the web, I've submitted a PR to correct this issue).

The Generator

hello-lit.gif

First, open a terminal to your root development folder. Personally, I literally use a folder, development in my root (~/or home) directory (:

Next, run these magical incantations:

$ npm init @open-wc

This will download everything needed from npm and launch an interactive CLI.

"What's with the weird npm init? This command makes use of an initializer.
As mentioned in the npm docs:
npm init <initializer> can be used to set up a new or existing npm package.

In this case, before npm runs our command, it's transformed from npm init @open-wc into npx @open-wc/create.

This is uses npx to install our initiallizer, @open-wc, then npx will automatically execute the main bin.

Pretty neat, eh? I learned something new, and I hope you did too (:

After launching, It will ask a few questions:

  1. What would you like to do today?
     โœ… Scaffold a new project
     โ€บ Upgrade an existing project
    
  2. What would you like to scaffold?
     โœ… Application
     โ€บ Component
    
  3. What would you like to add?
     โ€บ Linting (es-lint & prettier)
     โ€บ Testing (web-test-runner)
     โ€บ Demoing (storybook)
     โœ… Building (rollup)
    
  4. Would you like to use typescript?
     โœ… Yes
     โ€บ No
    
  5. Would you like to scaffold examples files for?
    โœ… Building (rollup)
    
  6. What is the tag name of your application/web component?
    hello-lit
    
  7. Do you want to write this file structure to disk?
     โœ… Yes
     โ€บ No
    
  8. Do you want to install dependencies?
     โ€บ No
     โ€บ Yes, with yarn
     โœ… Yes, with npm
    

Get Crackin'

Now you've got everything you need to make a web application with LitElement! Let's go ahead and start a live-reload dev server. Just cd into your project directory and then run npm run start:

$ cd hello-lit && npm run start

Open your project in your IDE of choice and get crackin'! If you're using VS Code like me, run this in a new terminal instance in your project's main directory:

$ code .

Congrats! You've now made a vanilla web-component-based app (stub) from scratch! Go forth and make something awesome!

Take a peek at LitElement's official docs for an in-depth look at the full (and fully awesome) API.

Thanks

Thanks for reading my very first post. ๐ŸŽ‰ I hope you've found it useful! Any feedback is truly appreciated, even if you're a jerk about it (;

If your interested in building an app with LitElement and Redux, show me some love and follow me for updates!

Additional Resources