Bilibili-Evolved/webpack/commit-hash.js
2021-12-05 12:25:18 +08:00

29 lines
927 B
JavaScript

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)))
})
}
}
}
}