├── .fatherrc.js ├── .github └── workflows │ └── publish.yml ├── .gitignore ├── .prettierrc ├── LICENSE ├── README.md ├── example ├── .umirc.js ├── package.json ├── pages │ └── index.js └── tsconfig.json ├── package.json └── src └── index.js /.fatherrc.js: -------------------------------------------------------------------------------- 1 | 2 | export default { 3 | target: 'node', 4 | cjs: 'babel', 5 | }; 6 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: npm-publish 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | 7 | jobs: 8 | publish: 9 | runs-on: ubuntu-latest 10 | 11 | name: 'publish npm' 12 | 13 | environment: npm 14 | 15 | steps: 16 | - uses: actions/checkout@main 17 | 18 | - name: Install and Build 19 | run: | 20 | npm install 21 | npm run build 22 | 23 | - name: 'Publish to the npm registry' 24 | uses: primer/publish@3.0.0 25 | env: 26 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 27 | NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} # 跟前面步骤中的 NPM_AUTH_TOKEN 保持一致 28 | with: 29 | default_branch: 'main' -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /yarn.lock 3 | /package-lock.json 4 | /example/dist 5 | /example/node_modules 6 | /example/pages/.umi 7 | /example/pages/.umi-production 8 | /lib 9 | example/yarn.lock 10 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all", 4 | "semi": false, 5 | "printWidth": 100, 6 | "overrides": [ 7 | { 8 | "files": ".prettierrc", 9 | "options": { "parser": "json" } 10 | } 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 shawerestart 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # umi-plugin-git-version 2 | 3 | [![NPM version](https://img.shields.io/npm/v/umi-plugin-git-version.svg?style=flat)](https://npmjs.org/package/umi-plugin-git-version) 4 | [![NPM downloads](http://img.shields.io/npm/dm/umi-plugin-git-version.svg?style=flat)](https://npmjs.org/package/umi-plugin-git-version) 5 | 6 | 此功能基于 [git-revision-webpack-plugin](https://github.com/pirelenito/git-revision-webpack-plugin)封装 7 | 8 | ## 使用方法 9 | 10 | 1. 安装 11 | 12 | ```bash 13 | npm install umi-plugin-git-version --save 14 | # or 15 | yarn add umi-plugin-git-version 16 | ``` 17 | 18 | 2. 使用 19 | 20 | 在umi@4中使用 21 | 22 | ```javascript 23 | // .umirc.js 或者 .umirc.ts 24 | // 添加 25 | export default { 26 | plugins: ['umi-plugin-git-version'], 27 | } 28 | ``` 29 | 在umi@3中使用 30 | 31 | ```javascript 32 | // do nothing, umi@3 will scan your node_modules and add to plugins 33 | ``` 34 | 35 | 3. 在页面中使用变量 36 | 37 | ```javascript 38 | __VERSION__: 当前版本号 39 | __COMMIT_HASH__: 当前提交hash值 40 | __BRANCH__: 当前提交分支 41 | __LAST_COMMIT_DATETIME__: 最后提交时间 42 | ``` 43 | 44 | ## LICENSE 45 | 46 | MIT 47 | -------------------------------------------------------------------------------- /example/.umirc.js: -------------------------------------------------------------------------------- 1 | export default { 2 | plugins: [ 3 | 'umi-plugin-git-version' 4 | ], 5 | } 6 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": ".umirc.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "MIT", 12 | "dependencies": { 13 | "umi-plugin-git-version": "^0.0.1" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /example/pages/index.js: -------------------------------------------------------------------------------- 1 | import { useState } from 'react' 2 | 3 | const index = () => { 4 | return ( 5 |
6 |

Page index

7 |
__VERSION__: {__VERSION__}
8 |
__COMMIT_HASH__: {__COMMIT_HASH__}
9 |
__BRANCH__: {__BRANCH__}
10 |
__LAST_COMMIT_DATETIME__: {__LAST_COMMIT_DATETIME__}
11 |
12 | ) 13 | } 14 | 15 | export default index 16 | -------------------------------------------------------------------------------- /example/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "build/dist", 6 | "sourceMap": true, 7 | "jsx": "react", 8 | "declaration": false, 9 | "module": "es2015", 10 | "moduleResolution": "node", 11 | "emitDecoratorMetadata": true, 12 | "experimentalDecorators": true, 13 | "importHelpers": true, 14 | "target": "es5", 15 | "typeRoots": ["node_modules/@types"], 16 | "lib": ["es2018", "dom"], 17 | "allowSyntheticDefaultImports": true, 18 | "rootDirs": ["/src", "/test", "/mock", "./typings"], 19 | "forceConsistentCasingInFileNames": true, 20 | "noImplicitReturns": true, 21 | "suppressImplicitAnyIndexErrors": true, 22 | "noUnusedLocals": true, 23 | "allowJs": true, 24 | "paths": { 25 | "@/*": ["./src/*"] 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "umi-plugin-git-version", 3 | "version": "0.0.14", 4 | "description": "get git version for umi base on git-revision-webpack-plugin", 5 | "authors": [ 6 | "Tung " 7 | ], 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/shawerestart/umi-plugin-git-version.git" 11 | }, 12 | "peerDependencies": { 13 | "umi": "3.x || 4.x" 14 | }, 15 | "main": "lib/index.js", 16 | "scripts": { 17 | "build": "father-build" 18 | }, 19 | "keywords": [ 20 | "React Git Version", 21 | "git-revision-webpack-plugin", 22 | "git-version", 23 | "react", 24 | "umi git version", 25 | "umijs", 26 | "umi4", 27 | "webpack", 28 | "webpack-plugin" 29 | ], 30 | "devDependencies": { 31 | "father": "^2.30.21", 32 | "father-build": "^1.8.0" 33 | }, 34 | "files": [ 35 | "lib", 36 | "src" 37 | ], 38 | "license": "MIT", 39 | "dependencies": { 40 | "@umijs/renderer-react": "^3.2.9", 41 | "git-revision-webpack-plugin": "^5.0.0" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const { GitRevisionPlugin } = require('git-revision-webpack-plugin') 2 | const gitRevisionPlugin = new GitRevisionPlugin({ 3 | versionCommand: ` describe --abbrev=0 --tags`, 4 | }) 5 | export default api => { 6 | const isUmi4 = typeof api.modifyAppData === 'function' 7 | 8 | api.describe({ 9 | key: 'gitReversionPlugin', 10 | config: { 11 | schema(joi) { 12 | return joi.string() 13 | }, 14 | onChange: api.ConfigChangeType.reload, 15 | }, 16 | enableBy: isUmi4 ? api.EnableBy.register : 'register', 17 | }) 18 | 19 | if (isUmi4) { 20 | api.modifyWebpackConfig((config, { webpack }) => { 21 | console.log(Array.isArray(config.plugins)) 22 | if (Array.isArray(config.plugins)) { 23 | config.plugins = [ 24 | ...config.plugins, 25 | gitRevisionPlugin, 26 | new webpack.DefinePlugin({ 27 | __VERSION__: JSON.stringify(gitRevisionPlugin.version()), 28 | __COMMIT_HASH__: JSON.stringify(gitRevisionPlugin.commithash()), 29 | __BRANCH__: JSON.stringify(gitRevisionPlugin.branch()), 30 | __LAST_COMMIT_DATETIME__: JSON.stringify(gitRevisionPlugin.lastcommitdatetime()), 31 | }), 32 | ] 33 | } 34 | return config 35 | }) 36 | } else { 37 | api.chainWebpack((config, { webpack }) => { 38 | // config.resolve.plugin('git-revision-webpack-plugin').use(gitRevisionPlugin); 39 | config 40 | .plugin('git-revision-webpack-plugin') 41 | .use(gitRevisionPlugin) 42 | .end() 43 | .plugin('git-revision-webpack-plugin') 44 | .use( 45 | new webpack.DefinePlugin({ 46 | __VERSION__: JSON.stringify(gitRevisionPlugin.version()), 47 | __COMMIT_HASH__: JSON.stringify(gitRevisionPlugin.commithash()), 48 | __BRANCH__: JSON.stringify(gitRevisionPlugin.branch()), 49 | __LAST_COMMIT_DATETIME__: JSON.stringify(gitRevisionPlugin.lastcommitdatetime()), 50 | }), 51 | ) 52 | return config 53 | }) 54 | } 55 | } 56 | --------------------------------------------------------------------------------