const { loadEnv } = require('vite') const tencentMapKeyPlaceholder = '__REPLACE_WITH_TENCENT_MAP_WEB_KEY__' const tencentMapKeyPattern = /^[A-Z0-9]{5}(?:-[A-Z0-9]{5}){4,6}$/i const isUsableTencentMapKey = (value) => ( tencentMapKeyPattern.test(value) && value !== tencentMapKeyPlaceholder ) const resolveH5BuildPolicy = ({ args = [], projectRoot, environment = process.env, loadProductionEnv = (root) => loadEnv('production', root, '') }) => { const flags = new Set(args) const requireTencentMapKey = flags.has('--require-tencent-map-key') const allowPlaceholder = flags.has('--allow-placeholder') if (requireTencentMapKey && allowPlaceholder) { throw new Error('H5 build cannot require a Tencent map key and allow its placeholder at the same time.') } if (!requireTencentMapKey) { return { allowPlaceholder, requireTencentMapKey, tencentMapKey: '' } } const productionEnv = loadProductionEnv(projectRoot) const tencentMapKey = ( environment.VITE_TENCENT_MAP_KEY || productionEnv.VITE_TENCENT_MAP_KEY || '' ).trim() if (!isUsableTencentMapKey(tencentMapKey)) { throw new Error([ 'Production H5 build requires a well-formed non-placeholder VITE_TENCENT_MAP_KEY.', 'Inject it through CI or an ignored .env.production.local file.' ].join(' ')) } return { allowPlaceholder, requireTencentMapKey, tencentMapKey } } const isTextBuildFile = (filePath) => /\.(?:js|mjs|html|json|css|txt|svg|xml|map)$/i.test(filePath) module.exports = { isTextBuildFile, isUsableTencentMapKey, resolveH5BuildPolicy, tencentMapKeyPlaceholder }