Skip to content

feat: add npm template support#118

Open
Huxpro wants to merge 2 commits intorstackjs:mainfrom
Huxpro:feature/npm-template-support
Open

feat: add npm template support#118
Huxpro wants to merge 2 commits intorstackjs:mainfrom
Huxpro:feature/npm-template-support

Conversation

@Huxpro
Copy link
Copy Markdown

@Huxpro Huxpro commented Apr 1, 2026

Add support for using npm packages as templates when creating new projects. This feature allows users to specify custom templates from npm registry with optional version control.

Key Features

  • ✅ Support multiple npm template formats (npm:, @scope/package, package-name)
  • ✅ Add --template-version flag for version specification
  • ✅ Implement smart caching mechanism (.temp-templates/)
  • ✅ Support flexible template structures (template/, templates/app/, root)
  • ✅ Isolated installation to prevent workspace conflicts
  • ✅ Export utility functions for downstream projects

Usage Examples

# Using npm package name
npm create rsbuild@latest my-project -- --template my-template-package

# Using scoped package
npm create rsbuild@latest my-project -- --template @scope/template-package

# Using explicit npm: prefix
npm create rsbuild@latest my-project -- --template npm:my-template-package

# With specific version
npm create rsbuild@latest my-project -- --template my-template-package --template-version 1.2.3

Implementation Details

Template Package Structure

The npm template package should have one of the following structures:

my-template-package/
├── template/              # Preferred
│   ├── package.json
│   └── src/
├── templates/
│   └── app/              # Alternative
└── (root)                # Fallback
    ├── package.json
    └── src/

Caching Strategy

  • Templates with latest version are always re-installed to ensure the latest version
  • Specific versions are cached in .temp-templates/ for faster reuse

API Exports

import {
  isNpmTemplate,
  resolveCustomTemplate,
  resolveNpmTemplate,
  sanitizeCacheKey,
} from 'create-rstack';

// Check if template input is an npm package
if (isNpmTemplate(templateInput)) {
  // Resolve npm template to local path
  const templatePath = resolveCustomTemplate(templateInput, version);
}

Testing

This feature has been successfully integrated and tested in:

Related

Inspired by sparkling's npm template implementation.

Checklist

  • Code follows project style guidelines
  • Documentation updated (README.md)
  • No breaking changes
  • Tested in downstream project (create-rspeedy)

Add support for using npm packages as templates when creating new
projects. This feature allows users to specify custom templates
from npm registry with optional version control.

Key features:
- Support multiple npm template formats (npm:, @scope/package, package-name)
- Add --template-version flag for version specification
- Implement smart caching mechanism (.temp-templates/)
- Support flexible template structures (template/, templates/app/, root)
- Isolated installation to prevent workspace conflicts
- Export utility functions for downstream projects

Example usage:
  npm create rsbuild@latest my-project -- --template my-template
  npm create rsbuild@latest my-project -- --template @scope/template
  npm create rsbuild@latest my-project -- --template my-template --template-version 1.2.3

API exports:
  - isNpmTemplate()
  - resolveCustomTemplate()
  - resolveNpmTemplate()
  - sanitizeCacheKey()

Inspired by sparkling's npm template implementation.
text,
} from '@clack/prompts';
import { determineAgent } from '@vercel/detect-agent';
import spawn from 'cross-spawn';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like #117 was removed from this branch 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants