This Repo utilizes Nx for package management and Lit Element + open-wc standards for scalable, small, lightweight web component development
If you are someone looking to build out some kind of component library with Lit components, this should help you get started. A lot of the setup work has been done for you here, but you can choose to use this, or modify it as needed. I tried to follow similar patterns I have seen across multiple component libraries when I put this together.
- Lit for authoring components
- Storybook for prototyping/testing/sandboxing your components
- TypeScript is key here, But ships JS and TS assets when compiled with
tsc
. - Web Test Runner for unit testing
- Nx - if you have never used Nx before, it is a Monorepo management tool.
- Changesets for publishing and managing versioning of nested packages in a monorepo
- Markdown doc generation with the web-component-analyzer and JSDoc Parsing
- Nx Generator for generating a base component to start off that is tested and storybook-ready for development
- Verdaccio Registry Docker container for local package publishing and testing
- Accessible via
yarn verdaccio
- Accessible via
- Packages are pre-configured for publishing to an internal company registry, or to npm.
- Published Files are pre-configured and pretty fleshed out to only publish the assets you need, but that can be adjusted as needed.
- Tooling is pretty fleshed out as well with some nice linter rules and quality of life adjustments, but that can also be adjusted as needed.
- This project uses
yarn
but you can usenpm
, at the time when i built this, npm didn't have a good handle on workspaces likeyarn
did and i still enjoy yarn over npm.
Run this to start and install dependencies:
yarn install
- For commands and other info, see: Changesets README.md
- Commands are simplified by running
yarn changeset:add
to stage the change to be published.- Changesets uses the
publishConfig
key on the package.json for the destination registry/publish location. - All
package.json
files must have the exact same publishConfig value or changesets may fail to publish in a CI environment.
- Changesets uses the
yarn new-component:ts
yarn new-component:ts my-component-name
postnew-component:ts script will run after component is successfully created, adding it to all of the cached operations.
Components are served with Storybook when in development.
View your components in storybook here:
yarn start
- Components are bundled with tsc and running the following nx command.
yarn nx run-many --target=build
- This is simplified by running:
yarn build
- Simply running
yarn install
will link all of packages together utilizing the yarn workspace symlinking strategy. Yarn makes this very easy compared to npm.
This project uses yarn
for package management and dependencies.
All scripts are defined for the components in the ./tools/generators/web-component-ts/package.json
for TS components.
If you want to add any additional scripts prior to starting your project, add them there so that each newly generated package can utilize Nx
and run with the rest of the project.
- Package level, or component level scripts must be named the same across all packages to utilize the Nx functionality to manage packages from the highest level for the most ease.
- As an example, if you wanted to include a bundler in your scripts, you would do something like this on the package level:
"scripts": {
"bundle-my-code": "rollup -c",
}
- Root level scripts can be configured however you want to manage certain functions. Most commonly with Nx, you will want to use the same commands across multiple packages and execute them with a single command.
- If you configured the above script on multiple packages, to run it you would simply configure your top level script to be like this:
"scripts": {
"bundle": "yarn nx run-many --target=bundle-my-code",
}
- You would execute that by running this command from the project root:
yarn bundle
Scripts should not be run on the smaller package level. Everything in this project is designed to be run from the project root.
Scripts can be cached as well (see below) for faster times in a pipeline or in local project builds.
All scripts must start with a run command like this:
yarn <script name>
- To add your nested package script to a Cacheable item, you would add your script name to the
targetDefaults
Object in the nx.json;
"targetDefaults": {
"test": {
"inputs": ["default", "^default", "{workspaceRoot}/jest.preset.js"],
"cache": true
},
"lint": {
"inputs": ["default", "{workspaceRoot}/.eslintrc.json"],
"cache": true
},
"build": {
"cache": true
},
"format": {
"cache": true
},
"lint:fix": {
"cache": true
},
}
You can also just run yarn
and it will ask you which script you would like to run from a list.
start
runs your app for development, reloading on file changes and using Storybookstorybook:build
builds storybook for static site hosting i.e. github pages
build
builds your app and outputs it in yourdist
directory on eachpackage
utilizingNx
test
runs the test command on eachpackage
utilizingNx
and generates test coverage for each one.lint
runs the linter for eachpackage
with the --check flag utilizingNx
lint:fix
runs the linter for eachpackage
with the --fix flag utilizingNx
format
fixes linting and formatting errors withprettier
for eachproject
utilizingNx
Tooling for this project should be fairly straightforward. Most users can get going quickly. If you are using VS Code, you can install a few of these Extensions and it may make your life easier working with Nx and Lit Element.
Verdaccio is a lightweight private npm proxy registry built in Node.js
Using a private npm registry like Verdaccio is one of the Top 10 NPM Security Best Practices recommended by the Open Web Application Security Project (OWASP).
Locally there is a file created based on their example file. verdaccio-config.yaml. This will get copied into an ignored directory where the verdaccio container mountpoints are referencing. Without this file, the container will not run. If you accidentally delete this file, it can be redownloaded/copied here
yarn verdaccio
- Lit Element Extensions. I have all of these and they make my life much easier developing components in VS Code.
- Lit Snippets - Great for quickly scaffolding a component boilerplate
- lit-html - This is one that provides some syntax highlighting and intellisense for html inside of lit element. Very useful so it doesn't just look like a long template string.
- lit-plugin - This one provides more syntax highlighting outside of the html syntax.
- Formatting and Code normalization - Most of these are installed by default but ensure you have them
- Nx Console - Great for executing workspace commands. This project is setup to simplify these commands with built in
package.json
scripts. If you want this, use it at your own risk. - Ayu Mirage Color Scheme - An easy on the eyes color scheme for VS Code that makes code much more pleasant to look at.
- Material Icon Theme - Colorful icons that give your VS Code some character
- Peacock - Colorizes your VSCode windows. Great for those of us that have 15 windows open at a time. Assign a color and recognize that code window immediately.