You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.8 KiB
47 lines
1.8 KiB
/** |
|
* @program: kicc-ui |
|
* @description: 用于打包时生成额外的配置文件,该文件可以配置一些全局变量,这样就可以直接在外部更改,无需重新打包 |
|
* @author: wangxiang4 |
|
* @create: 2022/4/6 |
|
*/ |
|
|
|
import { GLOB_CONFIG_FILE_NAME, OUTPUT_DIR } from '../constant'; |
|
import fs, { writeFileSync } from 'fs-extra'; |
|
import colors from 'picocolors'; |
|
import { getRootPath, getEnvConfig } from '../utils'; |
|
import { getConfigFileName } from '../getConfigFileName'; |
|
import pkg from '../../package.json'; |
|
|
|
/** 创建外部配置文件 */ |
|
function createConfig({ |
|
configName, |
|
config, |
|
configFileName = GLOB_CONFIG_FILE_NAME |
|
}: { configName: string; config: any; configFileName?: string } = { configName: '', config: {} }) { |
|
try { |
|
const windowConf = `window.${configName}`; |
|
// 确保变量不会被修改 |
|
const configStr = `${windowConf}=${JSON.stringify(config)}; |
|
Object.freeze(${windowConf}); |
|
Object.defineProperty(window, "${configName}", { |
|
configurable: false, |
|
writable: false, |
|
}); |
|
`.replace(/\s/g, ''); |
|
fs.mkdirp(getRootPath(OUTPUT_DIR())); |
|
// 将配置信息追加到_app.config.js文件中 |
|
writeFileSync(getRootPath(`${OUTPUT_DIR()}/${configFileName}`), configStr); |
|
|
|
console.log(colors.cyan(`✨ [${pkg.name}]`) + ' - configuration file is build successfully:'); |
|
console.log(colors.gray(OUTPUT_DIR() + '/' + colors.green(configFileName)) + '\n'); |
|
} catch (error) { |
|
console.log(colors.red('configuration file configuration file failed to package:\n' + error)); |
|
} |
|
} |
|
|
|
/** 运行构建外部配置文件 */ |
|
export function runBuildConfig() { |
|
const config = getEnvConfig(); |
|
const configFileName = getConfigFileName(config); |
|
createConfig({ config, configName: configFileName }); |
|
}
|
|
|