提交 | 用户 | age
|
2f1fbf
|
1 |
/** |
V |
2 |
* 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 |
|
3 |
*/ |
759e53
|
4 |
import { GLOB_CONFIG_FILE_NAME, OUTPUT_DIR } from '../constant'; |
bb3b8f
|
5 |
import fs, { writeFileSync } from 'fs-extra'; |
de332a
|
6 |
import chalk from 'chalk'; |
bb3b8f
|
7 |
|
8a9ca4
|
8 |
import { getRootPath, getEnvConfig } from '../utils'; |
V |
9 |
import { getConfigFileName } from '../getConfigFileName'; |
759e53
|
10 |
|
V |
11 |
import pkg from '../../package.json'; |
bb3b8f
|
12 |
|
N |
13 |
function createConfig( |
|
14 |
{ |
|
15 |
configName, |
|
16 |
config, |
|
17 |
configFileName = GLOB_CONFIG_FILE_NAME, |
|
18 |
}: { configName: string; config: any; configFileName?: string } = { configName: '', config: {} } |
|
19 |
) { |
|
20 |
try { |
|
21 |
const windowConf = `window.${configName}`; |
2f1fbf
|
22 |
// Ensure that the variable will not be modified |
bb3b8f
|
23 |
const configStr = `${windowConf}=${JSON.stringify(config)}; |
N |
24 |
Object.freeze(${windowConf}); |
|
25 |
Object.defineProperty(window, "${configName}", { |
|
26 |
configurable: false, |
|
27 |
writable: false, |
|
28 |
}); |
759e53
|
29 |
`.replace(/\s/g, ''); |
8a9ca4
|
30 |
fs.mkdirp(getRootPath(OUTPUT_DIR)); |
V |
31 |
writeFileSync(getRootPath(`${OUTPUT_DIR}/${configFileName}`), configStr); |
bb3b8f
|
32 |
|
759e53
|
33 |
console.log(chalk.cyan(`✨ [${pkg.name}]`) + ` - configuration file is build successfully:`); |
V |
34 |
console.log(chalk.gray(OUTPUT_DIR + '/' + chalk.green(configFileName)) + '\n'); |
bb3b8f
|
35 |
} catch (error) { |
de332a
|
36 |
console.log(chalk.red('configuration file configuration file failed to package:\n' + error)); |
bb3b8f
|
37 |
} |
N |
38 |
} |
|
39 |
|
|
40 |
export function runBuildConfig() { |
|
41 |
const config = getEnvConfig(); |
8a9ca4
|
42 |
const configFileName = getConfigFileName(config); |
bb3b8f
|
43 |
createConfig({ config, configName: configFileName }); |
N |
44 |
} |