-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathvariables.ts
148 lines (135 loc) · 3.64 KB
/
variables.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*!
* Jodit Editor (https://xdsoft.net/jodit/)
* Released under MIT see LICENSE.txt in the project root for license information.
* Copyright (c) 2013-2025 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/
import * as path from 'path';
function Bool(str: string | boolean | undefined): boolean {
return typeof str === 'boolean' ? str : str === 'true';
}
export type ES_TARGET = 'es5' | 'es2015' | 'es2018' | 'es2021';
export type Argv = {
WEBPACK_SERVE?: boolean;
filename?: (name: string) => string;
env: object;
mode?: 'production' | 'development';
isTest?: boolean;
generateTypes?: boolean;
uglify?: boolean;
fat?: boolean;
stat?: boolean;
exclude?: string;
excludePlugins?: string;
includePlugins?: string;
excludeLanguages?: string;
includeLanguages?: string;
es?: ES_TARGET;
outputFolder?: string;
progressFunction?: () => void;
};
export type Variables = {
argv: { filename?: (name: string) => string };
exclude: string[];
/**
* Path to root Jodit directory
*/
superDirname: string;
outputPath: string;
banner: string;
/**
* Path to current work directory
*/
dirname: string;
pkg: { version: string; homepage: string };
debug: boolean;
serve: boolean;
isTest: boolean;
onlyTS: boolean;
generateTypes: boolean;
isProd: boolean;
uglify: boolean;
stat: boolean;
excludeLanguages: string[];
includeLanguages: string[];
excludePlugins: string[];
includePlugins: string[];
progressFunction:
| ((percentage: number, msg: string, ...args: string[]) => void)
| false;
mode: 'production' | 'development';
ES: ES_TARGET;
ESNext: boolean;
ESModern: boolean;
/**
* Compose all plugins in the build
*/
fat: boolean;
port: number;
};
export const variables = (argv: Argv, dir: string): Variables => {
const pkg = require(path.resolve(dir, './package.json'));
const banner = `/*!
* ${pkg.name} - ${pkg.description}
* Author: ${pkg.author}
* Version: v${pkg.version}
* Url: ${pkg.homepage}
* License(s): ${pkg.license}
*/
`;
const debug = !argv || !argv.mode || !argv.mode.match(/production/);
const isTest = Bool(argv && argv.isTest);
const mode = (debug ? 'development' : argv.mode) ?? 'production';
const isProd = mode === 'production';
const uglify = Boolean(!debug && argv && Bool(argv.uglify));
const stat = Bool(argv.stat);
const exclude = (argv.exclude || '').split(/[,\s;]/);
const excludePlugins = (argv.excludePlugins || '').split(/[,\s;]/);
const includePlugins = (argv.includePlugins || '').split(/[,\s;]/);
const excludeLanguages = (argv.excludeLanguages || '').split(/[,\s;]/);
const includeLanguages = (argv.includeLanguages || '').split(/[,\s;]/);
const ES = argv.es as ES_TARGET;
if (!['es5', 'es2018', 'es2021', 'es2015'].includes(ES)) {
throw Error('Define correct ES target');
}
const ESNext = ES === 'es2021';
const ESModern = ['es2021', 'es2018', 'es2015'].includes(ES);
const dirname = dir;
const superDirname = path.resolve(__dirname, '..');
const outputFolder =
argv.outputFolder ||
`build/${ES}${includeLanguages.toString() !== 'en' ? '' : '.en'}/`;
const outputPath = path.join(dir, outputFolder);
return {
port: process.env.WEBPACK_DEV_PORT
? parseInt(process.env.WEBPACK_DEV_PORT)
: 2000,
argv,
onlyTS: false, // TODO
exclude,
generateTypes: Bool(argv.generateTypes),
fat: Bool(argv.fat),
serve: Boolean(argv.WEBPACK_SERVE),
superDirname,
outputPath,
banner,
dirname,
pkg,
debug,
isTest,
isProd,
uglify,
stat,
excludeLanguages,
includeLanguages,
excludePlugins,
includePlugins,
progressFunction:
typeof argv.progressFunction === 'function'
? argv.progressFunction
: false,
mode,
ES,
ESModern,
ESNext
};
};