A feature-rich boilerplate for creating React and TypeScript node modules, complete with Rollup bundling, Jest testing environment, and ESLint & Prettier integration.
- React and TypeScript support
- Testing environment with Jest and React Testing Library
- Code formatting and linting with ESLint and Prettier
- TypeScript compiler configuration
- Automatic module type definitions
- Modern npm package format (2.0.0)
- ES Modules (ESM) and CommonJS support
.
βββ src/ # Source code
β βββ cjs/ # CommonJS format
β βββ esm/ # ES Modules format
β βββ types/ # Type definition files
βββ tests/ # Test files
βββ example/ # Example code
βββ types/ # Type definition files
react(>=16.8.0)react-dom(>=16.8.0)
typescript: Static type supportjest&@testing-library/react: Testing environmenteslint&prettier: Code quality managementsass: CSS preprocessor
This template follows the modern npm package format (2.0.0) with the following features:
- Dual Module Support: Supports both ES Modules and CommonJS formats
- TypeScript Integration: Full TypeScript support with declaration files
- Exports Field: Uses the new
exportsfield for better module resolution - Package Entry Points: Clear entry points for both ESM and CommonJS formats
The module is configured with the following settings in package.json:
{
"type": "module",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"types": "./lib/types/index.d.ts",
"exports": {
".": {
"types": "./lib/types/index.d.ts",
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js"
}
}
}The build process generates both ESM and CommonJS formats along with type definitions:
-
ESM Build:
- Generates
.jsfiles for ES modules - Uses
tsconfig.json - Output directory:
lib/esm/
- Generates
-
CommonJS Build:
- Generates
.jsfiles for CommonJS modules - Uses
tsconfig.cjs.json - Output directory:
lib/cjs/
- Generates
-
Type Definitions:
- Generated alongside both builds
- Located in
lib/types/
When publishing to npm, only the following files are included:
lib/directory containing:- ESM format (
.jsfiles inlib/esm/) - CommonJS format (
.jsfiles inlib/cjs/) - TypeScript declaration files (
.d.tsinlib/types/) - Source maps (if enabled)
- ESM format (
- Update the version in
package.json - Run
npm run buildto compile the latest changes - Run
npm publishto publish to npm registry
This repository is a template repository. Follow these steps to create a new React TypeScript module:
- Click "Use this template" - Create a new repository on GitHub
- Enter repository details - Name, description, and visibility settings
- Clone and customize - Clone your new repository and modify the following files:
package.json
{
"name": "your-module-name", // β Change this
"version": "1.0.0", // β Reset to initial version
"description": "Your module description", // β Change this
"author": "your-name" // β Change this
// ... keep the rest as is
}README.md
- Change the title to your module name
- Update the description to match your module's functionality
- Add usage examples and API documentation
src/index.ts
- Write your actual module code
npm install# Run linter
npm run lint
# Build
npm run build # Build both ESM and CommonJS
npm run build:esm # Build only ESM
npm run build:cjs # Build only CommonJS
# Run tests
npm run test
npm run test:watchThe library can be used in both ESM and CommonJS environments:
// ESM
import { something } from 'module-template';
// CommonJS
const { something } = require('module-template');Built files are generated in the lib directory:
lib/esm/index.js: ESM format JavaScript filelib/cjs/index.js: CommonJS format JavaScript filelib/types/index.d.ts: TypeScript type definitions
MIT