Vite TypeScript 库 npm 包模板
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?
Use this template via GitHub's "Use this template" button or:
gh repo create --template="https://github.com/jasonsturges/vite-typescript-npm-package"Important: Check package name availability before you start to avoid renaming later:
npm search 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 developmentThis template builds multiple distribution formats:
dist/index.mjs) - Modern ES modules for importdist/index.cjs) - Node.js compatibility for require()dist/index.iife.js) - Browser global variable for CDN usagedist/index.d.ts) - Bundled TypeScript declaration file[!NOTE] Need UMD format? Add
"umd"to theformatsarray invite.config.tsand update thefileNamefunction to handle it.
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:
// src/utils/arrayUtils.ts
export const distinct = (array: T[] = []) => [...new Set(array)];Include that export in the top-level index.ts:
// src/index.ts
export { distinct } from "./utils/arrayUtils"Multiple strategies for development are available.
Vite's dev server provides real-time HMR updates:
npm startThis 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.
For development with npm link or when working with other projects:
npm run devVite detects changes and compiles all modules to dist/, including bundled type declarations.
To test your library in other projects before publishing:
From this library: Start watch mode and link the package
npm run dev
npm linkFrom your app: Link to this library
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.
Create a tarball to test the exact package that will be published:
npm packThis creates a [name].tgz tarball. Install it in your test app:
npm install /path/to/[name].tgzOnce development completes, unlink both projects:
From your app:
npm unlink "mylib"From your library:
npm unlinkIf you forget to unlink, manually clean up:
For yarn:
rm -rf ~/.config/yarn/link/mylibFor npm:
sudo npm rm --global "mylib"
npm ls --global --depth 0 # confirm removalOr simply reinstall dependencies in your app to clear symlinks:
rm -rf node_modules && npm installUpdate your package.json to the next version number and tag a release.
Add publishConfig to your package.json:
"publishConfig": {
"registry": "https://registry.npmjs.org/",
"access": "public"
}For private GitHub packages:
"publishConfig": {
"registry": "https://npm.pkg.github.com/@MyOrg"
}Before publishing, ensure a clean build:
rm -rf dist
npm run buildVerify your package name is available:
npm search Once ready to publish:
npm login
npm publish --access publicFor continuous integration with GitHub Actions, create .github/workflows/publish.yml
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 publishname: 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 publishObtain an "Automation" CI/CD access token from npm to bypass 2FA:
Add secrets to your repository:
NPM_TOKEN secretFor more information:
暂无开放 Issues,或尚未同步最近议题。