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.

30 lines
1.0KB

  1. import { GLOB_CONFIG_FILE_NAME, GLOB_CONFIG_NAME, OUTPUT_DIR } from '../constant'
  2. import fs, { writeFileSync } from 'fs-extra'
  3. import chalk from 'chalk'
  4. import { getEnvConfig, getRootPath } from '../utils'
  5. function createConfig(option) {
  6. const { config, configName, configFileName } = option
  7. try {
  8. const windowConf = `window.${configName}`
  9. const configStr = `${windowConf}=${JSON.stringify(config)};
  10. Object.freeze(${windowConf});
  11. Object.defineProperty(window, "${configName}", {
  12. configurable: false,
  13. writable: false,
  14. });
  15. `.replace(/\s/g, '')
  16. fs.mkdirp(getRootPath(OUTPUT_DIR))
  17. writeFileSync(getRootPath(`${OUTPUT_DIR}/${configFileName}`), configStr)
  18. } catch (error) {
  19. console.log(chalk.red('configuration file configuration file failed to package:\n' + error))
  20. }
  21. }
  22. export function runBuildConfig() {
  23. const config = getEnvConfig()
  24. const configName = GLOB_CONFIG_NAME
  25. const configFileName = GLOB_CONFIG_FILE_NAME
  26. createConfig({ config, configName, configFileName })
  27. }