2020-10-29 23:01:11 +08:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2021-02-08 23:59:47 +08:00
|
|
|
import { GLOB_CONFIG_FILE_NAME, OUTPUT_DIR } from '../constant';
|
2020-10-13 01:40:21 +08:00
|
|
|
import fs, { writeFileSync } from 'fs-extra';
|
2021-02-04 22:00:25 +08:00
|
|
|
import chalk from 'chalk';
|
2020-10-13 01:40:21 +08:00
|
|
|
|
2021-02-04 22:00:25 +08:00
|
|
|
import { getCwdPath, getEnvConfig } from '../utils';
|
2020-10-29 23:01:11 +08:00
|
|
|
import { getShortName } from '../getShortName';
|
2020-10-13 01:40:21 +08:00
|
|
|
|
2021-02-08 23:59:47 +08:00
|
|
|
import pkg from '../../package.json';
|
|
|
|
|
2020-10-13 01:40:21 +08:00
|
|
|
function createConfig(
|
|
|
|
{
|
|
|
|
configName,
|
|
|
|
config,
|
|
|
|
configFileName = GLOB_CONFIG_FILE_NAME,
|
|
|
|
}: { configName: string; config: any; configFileName?: string } = { configName: '', config: {} }
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const windowConf = `window.${configName}`;
|
2020-10-29 23:01:11 +08:00
|
|
|
// Ensure that the variable will not be modified
|
2020-10-13 01:40:21 +08:00
|
|
|
const configStr = `${windowConf}=${JSON.stringify(config)};
|
|
|
|
Object.freeze(${windowConf});
|
|
|
|
Object.defineProperty(window, "${configName}", {
|
|
|
|
configurable: false,
|
|
|
|
writable: false,
|
|
|
|
});
|
2021-02-08 23:59:47 +08:00
|
|
|
`.replace(/\s/g, '');
|
|
|
|
fs.mkdirp(getCwdPath(OUTPUT_DIR));
|
|
|
|
writeFileSync(getCwdPath(`${OUTPUT_DIR}/${configFileName}`), configStr);
|
2020-10-13 01:40:21 +08:00
|
|
|
|
2021-02-08 23:59:47 +08:00
|
|
|
console.log(chalk.cyan(`✨ [${pkg.name}]`) + ` - configuration file is build successfully:`);
|
|
|
|
console.log(chalk.gray(OUTPUT_DIR + '/' + chalk.green(configFileName)) + '\n');
|
2020-10-13 01:40:21 +08:00
|
|
|
} catch (error) {
|
2021-02-04 22:00:25 +08:00
|
|
|
console.log(chalk.red('configuration file configuration file failed to package:\n' + error));
|
2020-10-13 01:40:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function runBuildConfig() {
|
|
|
|
const config = getEnvConfig();
|
|
|
|
const configFileName = getShortName(config);
|
|
|
|
createConfig({ config, configName: configFileName });
|
|
|
|
}
|