web-component-starter

Web Component Starter Template

A comprehensive, production-ready starter template for creating Web Components. This template is based on the architecture and best practices from my web components work, incorporating Google’s Custom Element Best Practices.

✨ Features

πŸš€ Quick Start

Use This Template

  1. Click β€œUse this template” on GitHub, or:
git clone https://github.com/aarongustafson/web-component-starter.git my-component
cd my-component
  1. Run the interactive setup:
npm install
npm run setup

The setup wizard will:

Manual Setup

If you prefer manual setup, see SETUP.md for detailed instructions.

πŸ“ What’s Included

web-component-starter/
β”œβ”€β”€ COMPONENT-NAME.js                    # Component implementation
β”œβ”€β”€ index.js                             # Main entry (class + auto-define)
β”œβ”€β”€ define.js                            # Auto-define only
β”œβ”€β”€ custom-elements.json                 # Custom Elements Manifest
β”œβ”€β”€ package.json                         # Package config with scripts
β”œβ”€β”€ LICENSE                              # MIT License
β”œβ”€β”€ README.md                            # This file (replaced after setup)
β”œβ”€β”€ README.tpl                           # Template for your component's README
β”œβ”€β”€ WEB-COMPONENTS-BEST-PRACTICES.md    # Best practices documentation
β”œβ”€β”€ .gitignore                           # Git ignore
β”œβ”€β”€ .npmignore                           # npm ignore
β”œβ”€β”€ .prettierrc                          # Prettier config
β”œβ”€β”€ .editorconfig                        # Editor config
β”œβ”€β”€ eslint.config.js                     # ESLint config
β”œβ”€β”€ vitest.config.js                     # Vitest config
β”œβ”€β”€ .github/
β”‚   β”œβ”€β”€ workflows/
β”‚   β”‚   β”œβ”€β”€ ci.yml                      # Continuous integration
β”‚   β”‚   └── publish.yml                 # Auto-publish to npm
β”‚   └── ISSUE_TEMPLATE/                 # Bug & feature templates
β”œβ”€β”€ scripts/
β”‚   └── setup.js                        # Interactive setup wizard (removed after setup)
β”œβ”€β”€ test/
β”‚   β”œβ”€β”€ setup.js                        # Test configuration
β”‚   └── COMPONENT-NAME.test.js          # Test suite
β”œβ”€β”€ demo/
β”‚   └── index.html                      # Live demo page
β”œβ”€β”€ SETUP.md                            # Manual setup guide (removed after setup)
└── CONTRIBUTING.md                     # Contribution guidelines

πŸ› οΈ Development

Available Scripts

npm run setup          # Interactive setup wizard
npm test               # Run tests in watch mode
npm run test:run       # Run tests once
npm run test:ui        # Open Vitest UI
npm run test:coverage  # Generate coverage report
npm run lint           # Lint with ESLint + Prettier
npm run format         # Auto-fix linting issues

Component Architecture

This template provides flexible import options:

Option 1: Manual registration

import { ComponentNameElement } from '@yourscope/component-name';

customElements.define('my-custom-name', ComponentNameElement);

Option 2: Guarded auto-define (browser environments only)

import '@yourscope/component-name/define.js';
// Registers the element when customElements is available

Prefer to control when registration happens? Call the helper directly:

import { defineComponentName } from '@yourscope/component-name/define.js';

defineComponentName();

πŸ§ͺ Testing

Includes:

Example:

import { describe, it, expect } from 'vitest';

describe('MyComponent', () => {
  it('should render', () => {
    const el = document.createElement('my-component');
    expect(el).toBeInstanceOf(HTMLElement);
  });
});

πŸ“¦ Publishing

This template is configured to publish to npm using OpenID Connect (OIDC), which is more secure than using long-lived NPM tokens.

Initial Setup:

  1. Publish your package to npm manually the first time:
    npm run test:run  # Ensure tests pass
    npm run lint      # Ensure code is clean
    npm publish       # First publish must be manual
    
  2. Configure OIDC on npm:
    • Visit your package’s access page: https://www.npmjs.com/package/@yourscope/your-component-name/access
    • Under β€œPublishing Access”, click β€œConfigure OIDC”
    • Add GitHub Actions as a trusted publisher with these settings:
      • Provider: GitHub
      • Organization/Username: Your GitHub username or organization
      • Repository: Your repository name
      • Workflow: .github/workflows/publish.yml
      • Environment: Leave blank (unless you use GitHub environments)
  3. Create a GitHub release to trigger automated publishing:
    • Use npm version to update version and create a tag: npm version patch (or minor/major)
    • Push with tags: git push --follow-tags
    • Or create a release through GitHub’s UI

The GitHub Actions workflow (.github/workflows/publish.yml) will automatically publish to npm when you create a new version tag.

Manual Publishing

If you prefer to publish manually without automation:

npm run test:run  # Ensure tests pass
npm run lint      # Ensure code is clean
npm publish       # Publish to npm

🌐 Browser Support

Works in all modern browsers supporting:

For legacy browsers, use polyfills.

πŸ“š Documentation

🎯 Use Cases

Perfect for:

πŸ™ Credits

Based on best practices from:

πŸ“„ License

MIT - See LICENSE

🀝 Contributing

Contributions welcome! See CONTRIBUTING.md


Ready to build your web component? Run npm run setup to get started! πŸš€