Skip to content

Commit

Permalink
Merge pull request tangly1024#2943 from dongzhenye/feat/version-manag…
Browse files Browse the repository at this point in the history
…ement

refactor: improve version number management
  • Loading branch information
tangly1024 authored Nov 13, 2024
2 parents 1ec3ef5 + b927542 commit 17ff870
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
3 changes: 1 addition & 2 deletions .env.local → .env.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# 环境变量 @see https://www.nextjs.cn/docs/basic-features/environment-variables
NEXT_PUBLIC_VERSION=4.7.7


# 可在此添加环境变量,去掉最左边的(# )注释即可
# Notion页面ID,必须
# NOTION_PAGE_ID=097e5f674880459d8e1b4407758dc4fb

# 非必须
# NEXT_PUBLIC_VERSION=
# NEXT_PUBLIC_PSEUDO_STATIC=
# NEXT_PUBLIC_REVALIDATE_SECOND=
# NEXT_PUBLIC_THEME=matery
Expand Down
34 changes: 34 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## 已知问题

1. (示例)版本号管理不规范
- 版本号直接写在环境变量中,容易出错
- 多处维护版本号,可能不一致

## 解决方案

1. (示例)将版本号管理从 `.env.local` 迁移到 `package.json`
- 统一从 `package.json` 读取版本号
- 使用 IIFE 优雅处理版本号获取逻辑
- 保持向后兼容,支持环境变量覆盖

## 改动收益

1. (示例)更规范的版本管理
- 统一从 `package.json` 读取
- 保持与 npm 生态一致
- 减少人为错误

## 具体改动

1. (示例)`blog.config.js`
- 移除原有的静态版本号配置
- 在文件末尾添加动态版本号获取逻辑
- 保持向后兼容,优先使用环境变量
- 添加错误处理和默认值

## 测试确认

- [x] 本地开发环境测试通过
- [x] 生产环境构建测试通过
- [x] 版本号正确显示
- [x] 环境变量配置正常工作
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ yarn-debug.log*
yarn-error.log*

# local env files
# .env.local # 版本号放在此环境变量中
.env.local
.env.development.local
.env.test.local
.env.production.local
Expand Down
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [Setup](#setup)
- [Creating new themes](#creating-new-themes)
- [Adding localizations](#adding-localizations)
- [Environment Variables](#environment-variables)

Thanks for considering to contribute!

Expand Down Expand Up @@ -42,6 +43,19 @@ localization! Follow these steps to add a new localization:
3. Add your language config to [lang.js][lang.js].
4. [Create a PR][pr] with your localization updates.

## Environment Variables

NotionNext uses environment variables for configuration. To set up your development environment:

1. Copy `.env.example` to `.env.local`
2. Fill in the required values in `.env.local`
3. Never commit `.env.local` to version control

The configuration priority is:
1. Notion Config Table (highest)
2. Environment Variables
3. blog.config.js (lowest)

[fork]: https://github.com/tangly1024/NotionNext/fork
[pr]: https://github.com/tangly1024/NotionNext/compare
[next.js]: https://github.com/vercel/next.js
Expand Down
10 changes: 9 additions & 1 deletion blog.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,15 @@ const BLOG = {
process.env.npm_lifecycle_event === 'export', // 在打包过程中默认开启缓存,开发或运行时开启此功能意义不大。
isProd: process.env.VERCEL_ENV === 'production' || process.env.EXPORT, // distinguish between development and production environment (ref: https://vercel.com/docs/environment-variables#system-environment-variables)
BUNDLE_ANALYZER: process.env.ANALYZE === 'true' || false, // 是否展示编译依赖内容与大小
VERSION: process.env.NEXT_PUBLIC_VERSION // 版本号
VERSION: (() => {
try {
// 优先使用环境变量,否则从package.json中获取版本号
return process.env.NEXT_PUBLIC_VERSION || require('./package.json').version
} catch (error) {
console.warn('Failed to load package.json version:', error)
return '1.0.0' // 缺省版本号
}
})()
}

module.exports = BLOG
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"post-build": "next-sitemap --config next-sitemap.config.js",
"export": "cross-env EXPORT=true next build && next-sitemap --config next-sitemap.config.js",
"bundle-report": "cross-env ANALYZE=true next build",
"build-all-in-dev": "cross-env VERCEL_ENV=production next build"
"build-all-in-dev": "cross-env VERCEL_ENV=production next build",
"version": "echo $npm_package_version"
},
"dependencies": {
"@clerk/localizations": "^3.0.4",
Expand Down

0 comments on commit 17ff870

Please sign in to comment.