diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 81be1bbfa..f8d17c989 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 diff --git a/src/components/types.ts b/src/components/types.ts index 71f7935bc..bcda76c1d 100644 --- a/src/components/types.ts +++ b/src/components/types.ts @@ -193,6 +193,8 @@ export interface ComponentMetadata extends FunctionalMetadata { i18n?: Record /** 作者信息 */ author?: Author | Author[] + /** 编译时的 commit hash, 由 Babel 注入, 不需要手动填写 */ + commitHash?: string } /** 用户组件的非函数基本信息, 用于直接保存为 JSON */ export type UserComponentMetadata = Omit diff --git a/webpack/commit-hash.js b/webpack/commit-hash.js new file mode 100644 index 000000000..59e43f181 --- /dev/null +++ b/webpack/commit-hash.js @@ -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))) + }) + } + } + } +} diff --git a/webpack/ts-loader.js b/webpack/ts-loader.js index bdfd3e369..eb04d3404 100644 --- a/webpack/ts-loader.js +++ b/webpack/ts-loader.js @@ -14,6 +14,7 @@ const babelLoader = { ], plugins: [ ['@babel/plugin-proposal-class-properties'], + './webpack/commit-hash.js', ], }, }