百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
V

vite-typescript-npm-package

> 包管理
开源

Vite TypeScript 库 npm 包模板

99 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

Vite TypeScript 库 npm 包模板

Vite TypeScript NPM Package

Scaffold TypeScript npm packages using this template to bootstrap your next library.

Built with Vite for fast builds and development with HMR, plus vite-plugin-dts for bundled type definitions.

[!TIP] Looking for alternatives to this Vite-based template?

  • JavaScript version
  • tsup version
  • ESBuild version

Getting Started

Use this template via GitHub's "Use this template" button or:

bash
gh repo create  --template="https://github.com/jasonsturges/vite-typescript-npm-package"

Important: Check package name availability before you start to avoid renaming later:

bash
npm search 

Usage

The following tasks are available:

  • npm run build - Build production distributable (JS + types)
  • npm run dev - Watch mode to detect changes (rebuilds dist/ and types)
  • npm start - Vite dev server with HMR for local development

Output Formats

This template builds multiple distribution formats:

  • ESM (dist/index.mjs) - Modern ES modules for import
  • CommonJS (dist/index.cjs) - Node.js compatibility for require()
  • IIFE (dist/index.iife.js) - Browser global variable for CDN usage
  • Types (dist/index.d.ts) - Bundled TypeScript declaration file

[!NOTE] Need UMD format? Add "umd" to the formats array in vite.config.ts and update the fileName function to handle it.

Exports

Export everything from the top-level index.ts for inclusion in the build.

For example, if you have a utils/ folder with an arrayUtils.ts file:

typescript
// src/utils/arrayUtils.ts
export const distinct = (array: T[] = []) => [...new Set(array)];

Include that export in the top-level index.ts:

typescript
// src/index.ts
export { distinct } from "./utils/arrayUtils"

Development

Multiple strategies for development are available.

Local Development with HMR

Vite's dev server provides real-time HMR updates:

bash
npm start

This starts Vite's dev server hosting index.html for local development with hot module replacement. This file is not included in the production build. Only exports from src/index.ts are bundled into the library.

The template includes a React app example, which can be replaced with Vue, Solid.js, Svelte, or any framework.

For UI projects, consider adding Storybook to isolate component development.

Watch Mode Development

For development with npm link or when working with other projects:

bash
npm run dev

Vite detects changes and compiles all modules to dist/, including bundled type declarations.

Linked Project Development

To test your library in other projects before publishing:

Using Link

  1. From this library: Start watch mode and link the package

    bash
    npm run dev
    npm link
  2. From your app: Link to this library

    bash
    npm link "mylib"

A symlink is created in your app's node_modules/. Changes to your library are automatically rebuilt and reflected in your app.

Using Pack

Create a tarball to test the exact package that will be published:

bash
npm pack

This creates a [name].tgz tarball. Install it in your test app:

bash
npm install /path/to/[name].tgz

Development Cleanup

Once development completes, unlink both projects:

  • From your app:

    bash
    npm unlink "mylib"
  • From your library:

    bash
    npm unlink

If you forget to unlink, manually clean up:

For yarn:

bash
rm -rf ~/.config/yarn/link/mylib

For npm:

bash
sudo npm rm --global "mylib"
npm ls --global --depth 0  # confirm removal

Or simply reinstall dependencies in your app to clear symlinks:

bash
rm -rf node_modules && npm install

Release Publishing

Update your package.json to the next version number and tag a release.

Configuration

Add publishConfig to your package.json:

json
"publishConfig": {
  "registry": "https://registry.npmjs.org/",
  "access": "public"
}

For private GitHub packages:

json
"publishConfig": {
  "registry": "https://npm.pkg.github.com/@MyOrg"
}

Publishing

Before publishing, ensure a clean build:

bash
rm -rf dist
npm run build

Verify your package name is available:

bash
npm search 

Once ready to publish:

bash
npm login
npm publish --access public

Continuous Integration

For continuous integration with GitHub Actions, create .github/workflows/publish.yml

Public NPM Packages

yaml
name: Publish Package to npmjs
on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          registry-url: 'https://registry.npmjs.org'
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      - run: npm ci
      - run: npm run build
      - run: npm publish

Private GitHub Packages

yaml
name: Publish Package to GitHub Packages
on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          registry-url: "https://npm.pkg.github.com"
          scope: "@MyOrg"
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - run: npm ci
      - run: npm run build
      - run: npm publish

Setup Secrets

Obtain an "Automation" CI/CD access token from npm to bypass 2FA:

  1. Click your profile image � Access Tokens
  2. Generate a new Automation token

Add secrets to your repository:

  1. Repository � Settings � Security � Secrets and variables � Actions
  2. Add NPM_TOKEN secret

For more information:

  • Using secrets in GitHub Actions
  • Publish to npmjs and GPR with npm

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptboilerplatelibrarynpmpackage

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类包管理
定价开源

> 相关工具

N
npm
Node.js 默认包管理器
P
pnpm
高效节省磁盘的 Node 包管理器
I
is-gif
Check if a Buffer/Uint8Array is a GIF image