创建git仓库
新建一个github仓库,将初始化后的仓库clone到本地
npm init
在项目文件夹下,执行命令:
npm init
具体配置项:
package name: 项目/模块名称,长度必须小于等于214个字符,不能以"."(点)或者"_"(下划线)开头,不能包含大写字母。
version: 版本
description: 项目描述,帮助人们在使用npm search时找到这个包。
entry point: 入口文件
test command: 执行测试的时候使用的命令
git repository: git 仓库地址
keywords: 关键字,帮助人们找到你的包
author: 作者
license: 软件授权条款,让用户知道他们的使用权利和限制。
额外配置项:
homepage: 项目主页
types: typescript 类型文件访问路径
安装依赖包
安装typescript
npm install typescript --save-dev
安装react等依赖
npm install react --save
npm install @types/react --save-dev
npm install react-dom --save
npm install @types/react-dom --save-dev
.npmignore配置
npm发包时忽略哪些文件(我的设置是仅发布dist文件夹(编译输出文件夹)内的内容)
src/
demo/
docs/
package-lock.json
tsconfig.json
.env
logo.svg
tsconfig
{
"compilerOptions": {
"target": "es5", // 编译目标javascript版本
"lib": [ //编译时引入的 ES 功能库,包括:es5 、es6、es7、dom 等。// 如果未设置,则默认为: target 为 es5 时: ["dom", "es5", "scripthost"]
//target 为 es6 时: ["dom", "es6", "dom.iterable", "scripthost"]
"dom",
"dom.iterable",
"esnext"
],
"jsx": "react-jsx", /* Specify what JSX code is generated. */
// 装饰器相关配置
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
/* Modules */
"module": "commonjs", // 目标js模块系统
"rootDir": "src", /* Specify the root folder within your source files. */
"baseUrl": "src", /* Specify the base directory to resolve non-relative module names. *
/* Emit */
"declaration": true, // 生成声明文件
"outDir": "dist", // 编译输出目录
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
},
"exclude": [
"node_modules",
"dist",
]
}
重点配置
rootDir
baseUrl
declaration: true // 生成的声明文件的相对路径为package.json中type配置项的值
outDir
npm包构建
在package.json中配置,使用typescript编译项目
"scripts": {
"build": "tsc",
},
编译项目
npm run build
npm publish
注册npm
登录
npm login
发布项目
npm publish
peerDependencies和dependencies的区别
dependencies devDependencies peerDependencies三者的区别_胡聊前端的博客-CSDN博客_peerdependencies文章目录DependenciesDevDependenciesPeerDependencies参考Dependencies首先dependencies是大家最常用的,比如项目依赖了vue,那么vue就是dependencies,这里的依赖是会被最终构建到部署环境的// 将会保存到dependencies中npm install vue --save// 或者不写--save也可以,默认就会到dependencies中npm install vue// 再简单一些npm iDevDepenhttps://blog.csdn.net/weixin_43459866/article/details/112392975 dependencies、devDependencies、peerDependencies 区别 以及验证_阿豪-CSDN博客原文链接: dependencies、devDependencies、peerDependencies 区别 以及验证 ...https://blog.csdn.net/qq_35516360/article/details/122065329