Blame view

build/script/buildConf.ts 1.52 KB
vben authored
1
2
3
/**
 * Generate additional configuration files when used for packaging. The file can be configured with some global variables, so that it can be changed directly externally without repackaging
 */
4
5
import { GLOB_CONFIG_FILE_NAME } from '../constant';
import fs, { writeFileSync } from 'fs-extra';
6
import chalk from 'chalk';
7
8
import { getCwdPath, getEnvConfig } from '../utils';
vben authored
9
import { getShortName } from '../getShortName';
10
11
12
13
14
15
16
17
18
19

function createConfig(
  {
    configName,
    config,
    configFileName = GLOB_CONFIG_FILE_NAME,
  }: { configName: string; config: any; configFileName?: string } = { configName: '', config: {} }
) {
  try {
    const windowConf = `window.${configName}`;
20
    const outDir = 'dist';
vben authored
21
    // Ensure that the variable will not be modified
22
23
24
25
26
27
28
29
30
31
32
    const configStr = `${windowConf}=${JSON.stringify(config)};

      Object.freeze(${windowConf});
      Object.defineProperty(window, "${configName}", {
        configurable: false,
        writable: false,
      });
    `;
    fs.mkdirp(getCwdPath(outDir));
    writeFileSync(getCwdPath(`${outDir}/${configFileName}`), configStr);
33
34
    console.log(chalk.cyan('✨ configuration file is build successfully:'));
    console.log(chalk.gray(outDir + '/' + chalk.green(configFileName)) + '\n');
35
  } catch (error) {
36
    console.log(chalk.red('configuration file configuration file failed to package:\n' + error));
37
38
39
40
41
42
43
44
  }
}

export function runBuildConfig() {
  const config = getEnvConfig();
  const configFileName = getShortName(config);
  createConfig({ config, configName: configFileName });
}