打npm包的步骤
- 使用parcel编译,parcel build ./index.ts –no-source-maps –target node –bundle-node-modules
加上–no-minify可以设置不混淆
package 中配置name , version ,main ,files 等
下面是一个例子:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25{
"name": "mypackage",
"version": "0.0.1",
"main": "dist",
"files": [
"common",
"dist"
],
"scripts": {
"build": "parcel build ./index.ts --no-source-maps --target node --bundle-node-modules --no-minify",
"dev": "parcel watch ./index.ts --no-source-maps --target node --bundle-node-modules",
"clean": "rm -rf node_modules && rm -rf dist && rm -rf .cache"
},
"dependencies": {
},
"devDependencies": {
"@types/node": "^14.14.22",
"parcel-bundler": "^1.12.4",
"typescript": "^4.1.3"
},
"publishConfig": {
"registry": "https://registry.npm.alibaba-inc.com"
}
}到https://www.npmjs.com/注册 npm账号
本地登录 npm账号
npm login在仓库目录使用
npm publish发布包。
本地开发时,可以使用npm link功能来本地依赖库
- 在仓库目录中使用
npm link命令 - 再到调用代码的仓库使用
npm link mypackagename即可
另外,使用parcel watch ./index.ts --no-source-maps --target node --bundle-node-modules 可以让库中代码修改后自动编译,方便本地依赖开发。