Package Exports
- npm-publish-demos
- npm-publish-demos/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (npm-publish-demos) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
概览
创建项目
执行npm init -y
生成package.json
文件.
项目目录
├── dist ##打包结果
| ├── index.d.ts
| ├── index.esm.mjs
| ├── index.js
| ├── index.modern.mjs
| └── index.umd.js
├── node_modules
| ├── @babel
| ├── @types
├── package.json
├── pnpm-lock.yaml
├── README.md
├── src
| └── index.ts # 源代码
└── tsconfig.json
安装依赖
用到什么就装什么.
pnpm install typescript
pnpm install ts-node # 编译ts文件[dev]
pnpm install microbundle # 打包ts文件
示例代码
export const add = (a: number, b: number) => {
return a + b;
}
export const minus = (a: number, b: number) => {
return a - b;
}
export const multiple = (a: number, b: number) => {
return a * b;
}
export const divide = (a: number, b: number) => {
return a / b;
}
export const stringToUpperCase = (str: string) => {
return str.toUpperCase();
}
package.json
{
"name": "npm-publish-demos",
"version": "1.0.2",
"description": "publish demo",
"source": "src/index.ts",
"main": "./dist/index.js",
"scripts": {
"dev": "microbundle watch",
"build": "microbundle --no-sourcemap"
},
"keywords": [
"example"
],
"author": "ryan.ke",
"license": "ISC",
"devDependencies": {
"microbundle": "^0.15.1",
"typescript": "^4.8.4"
},
"files": [
"dist"
]
}
登录npm
发布
执行npm publish
,发布完成.
使用
npm install npm-publish-demos
import { add } from 'npm-publish-demos';
const result = add(1, 2);
console.log('result', result); // 3