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.
git clone https://github.com/aarongustafson/web-component-starter.git my-component
cd my-component
npm install
npm run setup
The setup wizard will:
my-awesome-component)If you prefer manual setup, see SETUP.md for detailed instructions.
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
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
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();
Includes:
Example:
import { describe, it, expect } from 'vitest';
describe('MyComponent', () => {
it('should render', () => {
const el = document.createElement('my-component');
expect(el).toBeInstanceOf(HTMLElement);
});
});
This template is configured to publish to npm using OpenID Connect (OIDC), which is more secure than using long-lived NPM tokens.
Initial Setup:
npm run test:run # Ensure tests pass
npm run lint # Ensure code is clean
npm publish # First publish must be manual
https://www.npmjs.com/package/@yourscope/your-component-name/access.github/workflows/publish.ymlnpm version to update version and create a tag: npm version patch (or minor/major)git push --follow-tagsThe GitHub Actions workflow (.github/workflows/publish.yml) will automatically publish to npm when you create a new version tag.
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
Works in all modern browsers supporting:
For legacy browsers, use polyfills.
Perfect for:
Based on best practices from:
MIT - See LICENSE
Contributions welcome! See CONTRIBUTING.md
Ready to build your web component? Run npm run setup to get started! π