Inject commit hash to registry

This commit is contained in:
the1812 2021-12-05 12:25:18 +08:00
parent 39df9d6ca7
commit 5c80c0af46
4 changed files with 32 additions and 1 deletions

View File

@ -2,7 +2,7 @@
## 搭建开发环境
- 需要安装 [Node.js](https://nodejs.org/en/download/), [Visual Studio Code](https://code.visualstudio.com/) 和 [yarn](https://yarnpkg.com/getting-started/install#global-install).
- 需要安装 [Node.js](https://nodejs.org/en/download/) (>= 14.0), [Visual Studio Code](https://code.visualstudio.com/) 和 [yarn](https://yarnpkg.com/getting-started/install#global-install).
- 将项目 Fork 至自己账户后, 克隆至本地
```powershell

View File

@ -193,6 +193,8 @@ export interface ComponentMetadata extends FunctionalMetadata {
i18n?: Record<string, LanguagePack>
/** 作者信息 */
author?: Author | Author[]
/** 编译时的 commit hash, 由 Babel 注入, 不需要手动填写 */
commitHash?: string
}
/** 用户组件的非函数基本信息, 用于直接保存为 JSON */
export type UserComponentMetadata = Omit<ComponentMetadata, keyof FunctionalMetadata>

28
webpack/commit-hash.js Normal file
View File

@ -0,0 +1,28 @@
const { compilationInfo } = require('./compilation-info')
const nodePath = require('path')
module.exports = function (babel) {
const { types } = babel
return {
visitor: {
ExportNamedDeclaration(path, state) {
const { filename } = state.file.opts
const isFromRegistry = filename.startsWith(nodePath.resolve('./registry'))
const isEntryFile = nodePath.basename(filename) === 'index.ts'
if (!(isFromRegistry && isEntryFile)) {
return
}
const { node } = path
node.declaration?.declarations?.forEach(d => {
if (!['component', 'plugin'].includes(d.id?.name)) {
return
}
if (d.init.type !== 'ObjectExpression') {
return
}
d.init.properties.push(types.objectProperty(types.identifier('commitHash'), types.stringLiteral(compilationInfo.commitHash)))
})
}
}
}
}

View File

@ -14,6 +14,7 @@ const babelLoader = {
],
plugins: [
['@babel/plugin-proposal-class-properties'],
'./webpack/commit-hash.js',
],
},
}