我开始留言RyanZim的回答,因为他的技术是正确的。但是,我想给出一个稍微不同的方法。我们公司维护了很多开源项目,这就是我们如何为您提供建议。
- 继续像你一样开发你的项目。您的
.gitignore
文件应忽略您的dist目录(您的案例中的/build
)。
- 当您准备部署时,您需要构建代码,在package.json中打上您的版本号,标记更改并将构建的代码推送到github和npm。
主要想法是,我们希望在github中保留一份我们的构建代码以及该版本的“标记”。这使我们能够确切地看到对于任何特定版本推送到npm的内容。内置的代码不是主分支的一部分,但在标签下(有点像分支)存在只有。当用户报告错误并且他正在使用版本x.x.x时,您可以检出该确切版本并开始调试。修复此错误时,您会发布一个新的“修补程序”,并且您的用户在下次运行npm install
或npm update
时会收到更改。
我们已经创建了一套npm脚本来为我们完成大部分工作。下面是我们用什么(这正好在你的package.json):
"scripts": {
"build": "node build.js",
"preversion": "npm run build",
"version": "git commit -am \"Update dist for release\" && git checkout -b release && git add -f dist/",
"postversion": "git push --tags && git checkout master && git branch -D release && git push",
"release:pre": "npm version prerelease && npm publish",
"release:patch": "npm version patch && npm publish",
"release:minor": "npm version minor && npm publish",
"release:major": "npm version major && npm publish"
}
我知道这可能看起来混乱,所以让我解释一下。无论何时我们准备发布新代码,我们运行release:
命令之一。例如,当我们运行npm run release:minor
时,以下是按顺序运行的命令列表。我有注解,所以你可以看看会发生什么:
node build.js ## run the build code - you will want to run gulp instead
npm version minor ## bumps the version number in package.json and creates a new git tag
git commit -am "Update dist for release" ## commit the package.json change to git (with new version number) - we will push it at the end
git checkout -b release ## create a temporary "release" branch
git add -f dist/ ## force add our dist/ directory - you will want to add your build/ directory instead
npm publish ## push the code to npm
git push --tags ## push the built code and tags to github
git checkout master ## go back to the master branch
git branch -D release ## delete the temporary "release" branch
git push ## push the updated package.json to github
如果您有任何疑问,请询问。您可能希望按照稍微不同的顺序进行操作,因为情况稍有不同。请随时提问。这个代码在数十个项目中运行得非常好 - 我们每天都会多次发布新代码。
我一直认为NPM从git仓库直接获取包的代码... – Victor
不,你单独推送到npm。 –